Skip to content

Commit

Permalink
Memoize hashCode results to improve runtime performance when checking…
Browse files Browse the repository at this point in the history
… policies.

PiperOrigin-RevId: 375791302
  • Loading branch information
jasonwyatt authored and arcs-c3po committed May 25, 2021
1 parent e4e3b90 commit 271256f
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
14 changes: 14 additions & 0 deletions java/arcs/core/data/AccessPath.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ typealias StoreId = String
* Examples of access paths are `input.app.packageName`, `store.rawText`, store.entites[i], etc.
*/
data class AccessPath(val root: Root, val selectors: List<Selector> = emptyList()) {
private val hashCode by lazy { 31 * root.hashCode() + selectors.hashCode() }

/**
* Constructs an [AccessPath] starting at a connection in [particle] identified
* by [connectionSpec] followed by [selectors].
Expand Down Expand Up @@ -104,4 +106,16 @@ data class AccessPath(val root: Root, val selectors: List<Selector> = emptyList(
}
return AccessPath(Root.HandleConnection(particle, root.connectionSpec), selectors)
}

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

if (root != other.root) return false
if (selectors != other.selectors) return false

return true
}

override fun hashCode(): Int = hashCode
}
44 changes: 44 additions & 0 deletions java/arcs/core/data/Recipe.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,26 @@ data class Recipe(
val spec: ParticleSpec,
val handleConnections: List<HandleConnection>
) {
private val hashCode by lazy { 31 * spec.hashCode() + handleConnections.hashCode() }

/** Representation of a handle connection in a particle. */
data class HandleConnection(
val spec: HandleConnectionSpec,
val handle: Handle,
val type: Type
)

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

if (spec != other.spec) return false
if (handleConnections != other.handleConnections) return false

return true
}

override fun hashCode(): Int = hashCode
}

/** Definition of a handle in a recipe. */
Expand All @@ -53,10 +67,40 @@ data class Recipe(
CREATE, USE, MAP, COPY, JOIN
}

private val hashCode by lazy {
var result = name.hashCode()
result = 31 * result + fate.hashCode()
result = 31 * result + type.hashCode()
result = 31 * result + (storageKey?.hashCode() ?: 0)
result = 31 * result + annotations.hashCode()
result = 31 * result + associatedHandles.hashCode()
result = 31 * result + id.hashCode()
result = 31 * result + tags.hashCode()
result
}

val capabilities: Capabilities
get() {
return Capabilities.fromAnnotations(annotations)
}

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

if (name != other.name) return false
if (fate != other.fate) return false
if (type != other.type) return false
if (storageKey != other.storageKey) return false
if (annotations != other.annotations) return false
if (associatedHandles != other.associatedHandles) return false
if (id != other.id) return false
if (tags != other.tags) return false

return true
}

override fun hashCode(): Int = hashCode
}
}

Expand Down
23 changes: 23 additions & 0 deletions java/arcs/core/data/Schema.kt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ data class Schema(
val refinementExpression: Expression<Boolean> = true.asExpr(),
val queryExpression: Expression<Boolean> = true.asExpr()
) {
private val hashCode by lazy {
var result = names.hashCode()
result = 31 * result + fields.hashCode()
result = 31 * result + hash.hashCode()
result = 31 * result + refinementExpression.hashCode()
result = 31 * result + queryExpression.hashCode()
result
}

/** Ensure instance is registered on construction. */
init {
Expand Down Expand Up @@ -71,6 +79,21 @@ data class Schema(
fun toString(options: Type.ToStringOptions) =
names.map { it.name }.plusElement(fields.toString(options)).joinToString(" ")

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

if (names != other.names) return false
if (fields != other.fields) return false
if (hash != other.hash) return false
if (refinementExpression != other.refinementExpression) return false
if (queryExpression != other.queryExpression) return false

return true
}

override fun hashCode(): Int = hashCode

data class Literal(
val names: Set<SchemaName>,
val fields: SchemaFields,
Expand Down

0 comments on commit 271256f

Please sign in to comment.