Optimize segment tree operations by inlining method calls - #37
Merged
Conversation
Expand update/push/all_apply call sites directly into the hot loops (set/get/prod/apply/max_right/min_left) and cache attribute lookups (self.d, self.op, etc.) as locals, removing per-call method dispatch and attribute-lookup overhead. Behavior is unchanged; verified against the previous implementation with randomized stress tests across both classes and all public methods. Benchmarked on the repo's existing workloads: segtree ~1.14x faster, lazysegtree ~1.31x faster.
📊 Performance Benchmark Results⏱️ Execution time (mean, lower is better)
💾 Peak memory (lower is better)
Base = target branch, PR = this branch. Negative delta = improvement. ✅ improvement ≥ 1%, |
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.
Summary
This PR optimizes the lazy segment tree and segment tree implementations by inlining frequently called methods and caching instance attributes as local variables. This reduces method call overhead and improves cache locality, resulting in better performance for tree operations.
Key Changes
lazysegtree.py:
push()method calls throughout all methods (set,get,prod,apply_point,apply,max_right,min_left) by directly expanding the push logic inlineupdate()method calls by directly computingd[k] = op(d[2*k], d[2*k+1])all_apply()method calls in theapply()methodd,lz,mapping,composition,identity,op,size,log) as local variables at the start of each method to reduce attribute lookup overheadsegtree.py:
update()method calls by directly computingd[k] = op(d[2*k], d[2*k+1])d,op,size) as local variables in methods that use them frequently (__init__,set,prod,max_right,min_left)Implementation Details
push()in lazy segment tree methods eliminates repeated method call overhead while maintaining the same logicself.d→d) which is particularly beneficial in tight loopshttps://claude.ai/code/session_01MhJ83qTzfEG4xQBrHgRTgY