-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Preserve cached values in Circuit more granularly #7649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
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
✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.37%. Comparing base (97781c7) to head (d347a26).
Additional details and impacted files
@@ Coverage Diff @@ ## main #7649 +/- ## ======================================= Coverage 99.37% 99.37% ======================================= Files 1078 1078 Lines 96162 96230 +68 ======================================= + Hits 95559 95627 +68 Misses 603 603
☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.
🚀 New features to boost your workflow:
- ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
...ts, update tests accordingly.
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.
Uh oh!
There was an error while loading. Please reload this page.
This PR improves the resilience of the cached member fields in Circuit. Most importantly,
_placement_cache, which is used to speed upCircuit.append()from O(N) to O(1), is made to be resilient and consistent acrossinsertand batch inserts,(削除) freeze/unfreeze cycles (削除ここまで),addandmuloperations, andcopy. Prior to this PR, performing any of those actions on a circuit would delete the cache, andappendwould 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._placement_cacheare now immutable types, socopydoesn't have to make copies.Moment.with_operations(*ops)gave a 15% speedup in the common case oflen(ops)==1.Overall, the performance of
appendandinsertindividually 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 placeself._mutated()everywhere.(削除) The biggest open question I have is whether building the placement cache inUpdate: it does not, and that functionality has been reverted.Circuit.from_moments()justifies the extra latency. If not, that could be removed, and it would only affect the freeze/unfreeze cycle case. (削除ここまで)Fixes #7464