Skip to content

Add _fast_cache to Simulation for O(1) repeated variable lookups#436

Open
nikhilwoodruff wants to merge 4 commits intomasterfrom
perf/fast-cache
Open

Add _fast_cache to Simulation for O(1) repeated variable lookups#436
nikhilwoodruff wants to merge 4 commits intomasterfrom
perf/fast-cache

Conversation

@nikhilwoodruff
Copy link
Contributor

Adds a flat dict[(variable_name, str(period)), array] at the Simulation level, checked at the very top of calculate() before the tracer, np.random.seed and the full _calculate() machinery. Only active when map_to=None and decode_enums=False (the inner-loop hot path used by formulas calling dependencies).

Invalidation mirrors the existing holder cache: purge_cache_of_invalid_values() removes invalidated entries, delete_arrays() clears the relevant key(s), and clone() gets a fresh empty cache so parent and clone simulations don't share state.

getattr/hasattr guards mean subclasses like StubSimulation that bypass __init__ work without modification.

nikhilwoodruff and others added 4 commits December 10, 2025 19:24
Adds a flat dict[tuple[str,str], array] at the Simulation level, checked at the
top of calculate() before tracer, random seed and _calculate() machinery. Only
active when map_to=None and decode_enums=False (the inner-loop hot path).

Invalidation mirrors the existing holder cache:
- purge_cache_of_invalid_values() removes invalidated entries
- delete_arrays() removes the relevant key(s)
- clone() gets a fresh empty cache to prevent cross-simulation sharing

Uses getattr/hasattr guards so StubSimulation and other test subclasses that
bypass __init__ work without modification.

Co-Authored-By: Claude <noreply@anthropic.com>
@nikhilwoodruff
Copy link
Contributor Author

Good catch — the fast path was skipping tracer.record_calculation_start() even when trace=True. Added and not self.trace to the guard so FullTracer sees all calculations as before. No perf impact for the normal (non-trace) path.

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.

1 participant

Comments