Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memoize hashCode results to improve runtime performance when checking policies. #7154

Merged
merged 1 commit into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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