Preserve cached values in Circuit more granularly
This PR improves the resilience of the cached member fields in Circuit. Most importantly, _placement_cache, which is used to speed up Circuit.append() from O(N) to O(1), is made to be resilient and consistent across insert and batch inserts, add and mul operations, and copy. Prior to this PR, performing any of those actions on a circuit would delete the cache, and append would fall back to O(N) behavior from that point on.
Summary of changes:
-
_PlacementCachehas new methodsinsert_moment(index, count)andput(index, mop), that update the key indices to account for new moments or operations.Circuithas new corresponding_insert_moments(...)and_put_ops(...)methods to modify the circuit and update the cache atomically, and logic strewn aroundCircuithas been updated to use those methods when possible. -
Circuit.copy()now preserves all cache values. - All cached values except
_placement_cacheare now immutable types, socopydoesn't have to make copies. - A simple optimization to
Moment.with_operations(*ops)gave a 15% speedup in the common case oflen(ops)==1.
Overall, the performance of append and insert individually are unaffected by this change; measured time is equal, but the performance when mixing inserts and appends is improved from O(N) to O(1). It also cleans up the code a bit IMO, with less need to place self._mutated() everywhere.
Fixes #7464
Okay, I lean toward not making the change in _from_moments. I see a lot of places across the codebase that uses _from_moments or unfreeze to make a quick copy of a circuit, edit some operations (which would destroy the cache anyway), and then copy the result back or re-freeze it. Therefore _from_moments needs to be as fast as possible.
At some point, it would be possible to add a parameter to allow each use case to choose whether to create the cache or not, but that would be a separate PR.
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 99.37%. Comparing base (ad37bb7) to head (8bac7ec).
Additional details and impacted files
@@ Coverage Diff @@
## main #7649 +/- ##
==========================================
- Coverage 99.38% 99.37% -0.01%
==========================================
Files 1090 1090
Lines 98248 98317 +69
==========================================
+ Hits 97643 97706 +63
- Misses 605 611 +6
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
Updated PR to undo the change to _from_moments and also Circuit(*moments) constructor to be safe. You can click on the diff link to see how it affects tests. Basically just the freeze cycles don't retain the cache, and of course manually calling from_moments creates a circuit without a placement cache.