Skip to content

Optimize segment tree operations by inlining method calls - #37

Merged
shakayami merged 1 commit into
masterfrom
claude/lazysegtree-segtree-inline-scxv7d
Jul 30, 2026
Merged

Optimize segment tree operations by inlining method calls#37
shakayami merged 1 commit into
masterfrom
claude/lazysegtree-segtree-inline-scxv7d

Conversation

@shakayami

Copy link
Copy Markdown
Owner

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:

  • Inlined push() method calls throughout all methods (set, get, prod, apply_point, apply, max_right, min_left) by directly expanding the push logic inline
  • Inlined update() method calls by directly computing d[k] = op(d[2*k], d[2*k+1])
  • Inlined all_apply() method calls in the apply() method
  • Cached instance attributes (d, lz, mapping, composition, identity, op, size, log) as local variables at the start of each method to reduce attribute lookup overhead

segtree.py:

  • Inlined update() method calls by directly computing d[k] = op(d[2*k], d[2*k+1])
  • Cached instance attributes (d, op, size) as local variables in methods that use them frequently (__init__, set, prod, max_right, min_left)

Implementation Details

  • The inlining of push() in lazy segment tree methods eliminates repeated method call overhead while maintaining the same logic
  • Local variable caching reduces the cost of attribute lookups (e.g., self.dd) which is particularly beneficial in tight loops
  • All functional behavior remains identical; this is purely a performance optimization
  • The changes make the code more verbose but significantly faster for operations on large trees

https://claude.ai/code/session_01MhJ83qTzfEG4xQBrHgRTgY

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.
This was linked to issues Jul 30, 2026
@github-actions

Copy link
Copy Markdown

📊 Performance Benchmark Results

⏱️ Execution time (mean, lower is better)

Benchmark Base PR Delta
acl_math 2.006s 2.009s +0.1%
acl_string 241.78ms 242.04ms +0.1%
convolution 870.59ms 888.54ms +2.1%
dsu 672.12ms 680.93ms +1.3%
fenwicktree 1.116s 1.138s +2.0%
fps 610.21ms 604.28ms -1.0%
lazysegtree 2.471s 1.742s -29.5% ✅
maxflow 773.55ms 769.44ms -0.5%
mincostflow 45.17ms 46.30ms +2.5%
prime_fact 2.020s 2.034s +0.7%
scc 319.36ms 319.48ms +0.0%
segtree 746.26ms 610.49ms -18.2% ✅
two_sat 1.303s 1.289s -1.1% ✅

💾 Peak memory (lower is better)

Benchmark Base PR Delta
acl_math 4.9KiB 4.9KiB +0.0%
acl_string 13.5MiB 13.5MiB +0.0%
convolution 16.1MiB 16.1MiB +0.0%
dsu 40.4MiB 40.4MiB +0.0%
fenwicktree 8.9MiB 8.9MiB +0.0%
fps 3.9MiB 3.9MiB +0.0%
lazysegtree 9.8MiB 9.8MiB +0.0%
maxflow 97.0MiB 97.0MiB +0.0%
mincostflow 16.3MiB 16.3MiB +0.0%
prime_fact 8.5KiB 8.5KiB +0.0%
scc 25.1MiB 25.1MiB +0.0%
segtree 6.0MiB 6.0MiB +0.0%
two_sat 174.0MiB 174.0MiB +0.0%

Base = target branch, PR = this branch. Negative delta = improvement. ✅ improvement ≥ 1%, ⚠️ regression > 5%. "n/a" base entries mean the target branch has no benchmark suite yet.

@shakayami
shakayami merged commit e012739 into master Jul 30, 2026
20 checks passed
@shakayami
shakayami deleted the claude/lazysegtree-segtree-inline-scxv7d branch July 30, 2026 15:07
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.

lazysegtree segtree

2 participants