From 34ed1322ba5e6b7546aafe2cfeaeab9eb0d45fcc Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Fri, 17 Mar 2023 23:34:35 +0700 Subject: [PATCH] Avoid unnecessary comparison --- .../mc/otm/network/FieldSynchronizer.kt | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/network/FieldSynchronizer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/network/FieldSynchronizer.kt index be238eedf..055351d11 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/network/FieldSynchronizer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/network/FieldSynchronizer.kt @@ -643,6 +643,7 @@ class FieldSynchronizer(private val callback: Runnable, private val alwaysCallCa override fun observe(): Boolean { check(!isRemoved) { "Field was removed" } + if (!isDirty && !codec.compare(remote, field)) { notifyEndpoints(this@Field) isDirty = true @@ -669,10 +670,6 @@ class FieldSynchronizer(private val callback: Runnable, private val alwaysCallCa return } - if (this.field == value) { - return - } - if (!isDirty && !codec.compare(remote, value)) { notifyEndpoints(this@Field) isDirty = true @@ -726,6 +723,9 @@ class FieldSynchronizer(private val callback: Runnable, private val alwaysCallCa override fun observe(): Boolean { check(!isRemoved) { "Field was removed" } + + val value = value + if (!isDirty && (remote == null || !codec.compare(remote ?: throw ConcurrentModificationException(), value))) { notifyEndpoints(this) isDirty = true @@ -746,7 +746,6 @@ class FieldSynchronizer(private val callback: Runnable, private val alwaysCallCa override fun write(stream: DataOutputStream, endpoint: Endpoint) { check(!isRemoved) { "Field was removed" } - val value = value codec.write(stream, value) isDirty = false } @@ -795,6 +794,8 @@ class FieldSynchronizer(private val callback: Runnable, private val alwaysCallCa override fun observe(): Boolean { check(!isRemoved) { "Field was removed" } + val value = value + if (!isDirty && !codec.compare(remote, value)) { notifyEndpoints(this) isDirty = true @@ -812,7 +813,6 @@ class FieldSynchronizer(private val callback: Runnable, private val alwaysCallCa override fun write(stream: DataOutputStream, endpoint: Endpoint) { check(!isRemoved) { "Field was removed" } - val value = value codec.write(stream, value) isDirty = false } @@ -835,8 +835,9 @@ class FieldSynchronizer(private val callback: Runnable, private val alwaysCallCa private var isRemote = false init { - if (observingBackingMap != null) + if (observingBackingMap != null) { observers.add(this) + } } private fun pushBacklog(key: Any?, value: (DataOutputStream) -> Unit) {