Skip to content

Commit

Permalink
Caching RawEntity hashCode to improve overhead.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 346436643
  • Loading branch information
alxmrs authored and arcs-c3po committed Dec 9, 2020
1 parent b29d853 commit 80caf63
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions java/arcs/core/data/RawEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ data class RawEntity(
}
)

// Cached `hashCode` value.
private var hashCode: Int = UNINITIALIZED_HASH

/** Iterates over of all field data (both singletons and collections). */
val allData: Sequence<Map.Entry<FieldName, Any?>>
get() = sequence {
Expand All @@ -64,8 +67,42 @@ data class RawEntity(
expirationTimestamp
)

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false

other as RawEntity

if (id != other.id) return false
if (singletons != other.singletons) return false
if (collections != other.collections) return false
if (creationTimestamp != other.creationTimestamp) return false
if (expirationTimestamp != other.expirationTimestamp) return false

return true
}

/** Computes and caches `hashCode`. */
override fun hashCode(): Int {
if (UNINITIALIZED_HASH == hashCode) {
var result = id.hashCode()
result = 31 * result + singletons.hashCode()
result = 31 * result + collections.hashCode()
result = 31 * result + creationTimestamp.hashCode()
result = 31 * result + expirationTimestamp.hashCode()

// If the hash happens to be the sentinel value, choose a different value.
if (UNINITIALIZED_HASH == result) {
result = 1
}
hashCode = result
}
return hashCode
}

companion object {
const val NO_REFERENCE_ID = "NO REFERENCE ID"
const val UNINITIALIZED_TIMESTAMP: Long = -1
const val UNINITIALIZED_HASH: Int = 0
}
}

0 comments on commit 80caf63

Please sign in to comment.