Skip to content

Commit ac8d5aa

Browse files
arirubinsteinkoponen
authored andcommitted
perf: cache InvocationKey hash values at construction time
Signed-off-by: Ari Rubinstein <arirubinstein@users.noreply.github.com>
1 parent 329dc35 commit ac8d5aa

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

Sources/Rego/IREvaluator.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,26 @@ let localIdxData = Local(1)
1212
struct InvocationKey: Hashable {
1313
let funcName: String
1414
let args: [IR.Operand]
15+
private let cachedHashValue: Int
16+
17+
init(funcName: String, args: [IR.Operand]) {
18+
self.funcName = funcName
19+
self.args = args
20+
var hasher = Hasher()
21+
hasher.combine(funcName)
22+
hasher.combine(args)
23+
self.cachedHashValue = hasher.finalize()
24+
}
25+
26+
func hash(into hasher: inout Hasher) {
27+
hasher.combine(cachedHashValue)
28+
}
29+
}
30+
31+
extension InvocationKey: Equatable {
32+
public static func == (lhs: InvocationKey, rhs: InvocationKey) -> Bool {
33+
lhs.cachedHashValue == rhs.cachedHashValue && lhs.funcName == rhs.funcName && lhs.args == rhs.args
34+
}
1535
}
1636

1737
/// MemoCache is a memoization cache of plan invocations

0 commit comments

Comments
 (0)