Skip to content

perf: cache the hash calculation for Operand#109

Closed
arirubinstein wants to merge 1 commit intoopen-policy-agent:mainfrom
arirubinstein:eng/ari/cache-operand-hash
Closed

perf: cache the hash calculation for Operand#109
arirubinstein wants to merge 1 commit intoopen-policy-agent:mainfrom
arirubinstein:eng/ari/cache-operand-hash

Conversation

@arirubinstein
Copy link
Copy Markdown
Contributor

@arirubinstein arirubinstein commented Feb 26, 2026

What code changed, and why?

Operand: Pre-computes the hash value to avoid repeated hashing of OpType (String-backed RawRepresentable) and Value in hot paths like InvocationKey construction.

Every InvocationKey.init call (on every evalCall) now hashes the args array by combining pre-computed ints instead of rehashing String-backed enums. [Operand].hash(into:) goes from O(n * string_hash) to O(n * int_combine)

Comment thread Sources/IR/IR.swift Outdated
@arirubinstein arirubinstein force-pushed the eng/ari/cache-operand-hash branch 3 times, most recently from ca7951a to f728d07 Compare February 27, 2026 16:58
koponen
koponen previously approved these changes Feb 27, 2026
Copy link
Copy Markdown
Contributor

@koponen koponen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

Copy link
Copy Markdown
Contributor

@shomron shomron left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @arirubinstein - I have concerns with the mutability of the type leading to subtle bugs where the cached hash value no longer maps to its identity.

Comment thread Sources/IR/IR.swift Outdated
Comment on lines +668 to +669
hasher.combine(type)
hasher.combine(value)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Given that type and value are public and var/mutable, I do not think it is safe to cache the hash value and assume it will never change.
  2. Changes like this should be accompanied with unit tests.

Signed-off-by: Ari Rubinstein <arirubinstein@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants