Skip to content

Commit 32fc8e4

Browse files
bartvinmantaci
authored andcommitted
Cache has_relation_precedence_rules as bool flag to eliminate method call overhead in get_progress_potential (PR #10163)
## Summary `has_relation_precedence_rules()` was called 3.3M+ times per compile via `get_progress_potential()`, each time evaluating `bool(self.freeze_dependents)` on an empty set. Cache the result as a public bool attribute `has_freeze_dependents` set once in `add_freeze_dependent()`. Access it directly in `get_progress_potential()` with an explicit `int()` cast for clarity. Split from #10100. ## Benchmark results (10 runs avg, dedicated benchmark machine) | Benchmark | master | with opt | Delta | | -------------- | -----: | -------: | -----: | | athonet_mpn | 14.82s | 15.26s | +3.0% | | connect_demo | 7.95s | 8.14s | +2.4% | | connect_infra | 16.56s | 16.66s | +0.6% | | inmanta_infra | 14.97s | 14.74s | -1.5% | | systemtenant | 12.05s | 11.28s | -6.4% | | **Total** | 66.35s | 66.08s | -0.4% | Impact is most visible on systemtenant (-6.4%) which has heavy scheduler speculation. The method call elimination saves ~0.43s per compile on models with 3.3M+ `get_progress_potential` calls. ## Test plan - [x] CI passes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
1 parent 9de55dc commit 32fc8e4

3 files changed

Lines changed: 8 additions & 3 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
description: Cache has_relation_precedence_rules as bool flag to eliminate method call overhead in get_progress_potential
2+
change-type: patch
3+
destination-branches: [master, iso8, iso9]

src/inmanta/ast/attribute.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ def __init__(self, entity: "Entity", value_type: "Type", name: str, location: Lo
183183
self.source_annotations = []
184184
self.target_annotations = []
185185
self.freeze_dependents: set[RelationAttribute] = set()
186+
self.has_freeze_dependents: bool = False
186187

187188
def __str__(self) -> str:
188189
return f"{self.get_entity().get_full_name()}.{self.name}"
@@ -234,10 +235,11 @@ def add_freeze_dependent(self, successor: "RelationAttribute") -> None:
234235
be frozen before `successor`.
235236
"""
236237
self.freeze_dependents.add(successor)
238+
self.has_freeze_dependents = True
237239

238240
def has_relation_precedence_rules(self) -> bool:
239241
"""
240242
Return true iff a relation precedence rule exists that defines that this Attribute should
241243
be frozen before another Attribute.
242244
"""
243-
return bool(self.freeze_dependents)
245+
return self.has_freeze_dependents

src/inmanta/execute/runtime.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@ def __str__(self) -> str:
782782
def get_progress_potential(self) -> int:
783783
# Ensure that relationships with a relation precedence rule cannot end up in the zerowaiters queue
784784
# of the scheduler. We know the order in which those types can be frozen safely.
785-
return super().get_progress_potential() + int(self.attribute.has_relation_precedence_rules())
785+
return super().get_progress_potential() + int(self.attribute.has_freeze_dependents)
786786

787787

788788
class OptionVariable(DelayedResultVariable["Instance"], RelationAttributeVariable):
@@ -851,7 +851,7 @@ def __str__(self) -> str:
851851
return f"OptionVariable {self.myself} {self.attribute} = {self.value}"
852852

853853
def get_progress_potential(self) -> int:
854-
return super().get_progress_potential() + int(self.attribute.has_relation_precedence_rules())
854+
return super().get_progress_potential() + int(self.attribute.has_freeze_dependents)
855855

856856

857857
WaiterSet = NewType("WaiterSet", Set["Waiter"])

0 commit comments

Comments
 (0)