Repository navigation
Trunc fast - #1
Merged
Merged
Conversation
… a tuple
Storing strategies in a Vector{TruncationStrategy} (abstract eltype)
means the per-element loop in _apply!(::CompositeTruncation) is a
dynamic-dispatch boundary that prevents Julia from inlining the inner
coeff_clip! / weight_clip! calls into the outer truncate path.
Switching to a typed Tuple{S1, S2, ...} field lets the compiler fully
specialize the recursive tail-pop iteration in _apply!, inlining each
strategy filter loop into a single specialized truncate routine.
Measured on truncate!(rand(PauliSum{40}, n_paulis=100000), composite)
where composite = CompositeTruncation(CoeffTruncation, WeightTruncation):
~8-9x speedup (per @Btime A/B). End-to-end downstream demos see
comparable wall-time reductions on hot truncation paths.
Public API preserved:
- Variadic CompositeTruncation(s::TruncationStrategy...) unchanged.
- AbstractVector constructor added for convenience (converts to tuple).
- Field access .strategies returns a Tuple instead of a Vector;
callers that mutated this field (push!/pop!) would break, but reading
it and constructing via the variadic form are unchanged.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Welcome to Codecov 🎉Once you merge this PR into your default branch, you're all set! Codecov will compare coverage reports and display results in all future pull requests. Thanks for integrating Codecov - We've got you covered ☂️ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Type-parameterize
CompositeTruncation'sstrategiesfield as a typedTupleinstead ofVector{TruncationStrategy}.Why
The abstract
Vector{TruncationStrategy}eltype prevents the JIT from specializing the per-element loop in_apply!(::CompositeTruncation). Each_apply!(O, strategy)call is a dynamic-dispatch boundary that prevents Julia from inlining the innercoeff_clip!/weight_clip!calls.Switching to a typed
Tuple{S1, S2, ...}field lets the compiler fully specialize the recursive tail-pop iteration in_apply!, inlining each strategy's filter loop into a single specialized truncate routine.Measurement
@btimeA/B ontruncate!(rand(PauliSum{40}, n_paulis=100000), composite)wherecomposite = CompositeTruncation(CoeffTruncation(0.1), WeightTruncation(4)):Public API
CompositeTruncation(s::TruncationStrategy...)AbstractVector{<:TruncationStrategy}constructor.strategiesTupleinstead ofVector.strategies(push!/pop!)The mutating-field break is the only behavioral change. I don't think any caller in this repo (or in the few downstream users I'm aware of) does that, but worth flagging.
Tests
test/test_truncation.jlupdated to assert the new tuple field type. All 9075 tests pass.