perf: use interned name in generic_getattr_opt#8129
Conversation
… getattr generic_getattr_opt looked up the instance dict by name_str.as_wtf8() -- the &Wtf8 content, whose DictKey::key_hash recomputes the SipHash from the raw bytes on every attribute access. Look it up by name_str (the &Py<PyStr>) instead: its DictKey returns the hash PyStr already caches, and adds a pointer-equality fast path for the key compare. (generic_setattr already passed the PyStr, so writes were never affected.) Free -- no memory added; the hash cache already exists. Benchmarks (interleaved A/B vs main, median): attribute-read loop -17.3%, pystone -3.0%, OOP method loop -7.9%; neutral on non-attribute code. Verified: 12 attribute/descriptor/class/dataclass test modules pass; clippy clean. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Assisted-by: Claude:claude-opus-4
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughIn ChangesInstance dict lookup optimization in generic_getattr_opt
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
generic_getattr_optlooked up the instance dict byname_str.as_wtf8(), which required recomputing the SipHash from the raw bytes on every attribute access. Nowname_str: &Py<PyStr>implementsDictKey, which returns the hashPyStralready caches, and adds a pointer-equality fast path for the key compare, which is much faster than using.as_wtf8().Benchmarks vs main, median: attribute-read loop -17.3%, pystone -3.0%, OOP method loop -7.9%; neutral on non-attribute code.
Assisted-by: Claude:claude-opus-4-8
Summary by CodeRabbit