Skip to content

Optimize Rolling/TimeRolling attribute dispatch (~3x speedup)#1788

Merged
MaxHalford merged 4 commits intomainfrom
optimize-rolling-getattr
Apr 2, 2026
Merged

Optimize Rolling/TimeRolling attribute dispatch (~3x speedup)#1788
MaxHalford merged 4 commits intomainfrom
optimize-rolling-getattr

Conversation

@MaxHalford
Copy link
Copy Markdown
Member

@MaxHalford MaxHalford commented Apr 2, 2026

Summary

  • Replaced __getattribute__ with __getattr__ in BaseRolling, so own-attribute lookups (self.obj, self.window, etc.) bypass the proxy entirely. The old implementation used a try/except on every attribute access — including the wrapper's own attributes — which accounted for ~50% of Rolling.update wall time.
  • Cached window_size as a plain _window_size int to avoid a property + deque attribute lookup per update.
  • Local-variable caching of self.window in the update hot path.

Benchmark (50k ticks, SMA window=20)

Before After
Rolling(Mean) 1.52 µs/update 0.48 µs/update
Rolling(Var) 1.87 µs/update 0.94 µs/update
Rolling(Mean+Var) (BBANDS) 4.05 µs/update 1.71 µs/update

MaxHalford and others added 3 commits April 2, 2026 13:28
Replace __getattribute__ with __getattr__ in BaseRolling so that
own-attribute lookups (self.obj, self.window, etc.) bypass the proxy
entirely. Cache window_size as a plain int and reduce repeated
attribute lookups in Rolling.update.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@MaxHalford MaxHalford requested a review from smastelini as a code owner April 2, 2026 11:31
Copy link
Copy Markdown
Member

@smastelini smastelini left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@MaxHalford MaxHalford merged commit bc2afaf into main Apr 2, 2026
1 check passed
@MaxHalford MaxHalford deleted the optimize-rolling-getattr branch April 2, 2026 13:08
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.

2 participants