perf: use interned name in `generic_getattr_opt` by sharifhsn · Pull Request #8129 · RustPython/RustPython · GitHub
Skip to content

perf: use interned name in generic_getattr_opt#8129

Open
sharifhsn wants to merge 1 commit into
RustPython:mainfrom
sharifhsn:perf/getattr-cached-hash
Open

perf: use interned name in generic_getattr_opt#8129
sharifhsn wants to merge 1 commit into
RustPython:mainfrom
sharifhsn:perf/getattr-cached-hash

Conversation

@sharifhsn

@sharifhsn sharifhsn commented Jun 18, 2026

Copy link
Copy Markdown

Summary

generic_getattr_opt looked up the instance dict by name_str.as_wtf8(), which required recomputing the SipHash from the raw bytes on every attribute access. Now name_str: &Py<PyStr> implements DictKey, which returns the hash PyStr already 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

  • Refactor
    • Optimized attribute lookup performance by improving the efficiency of dictionary lookups during attribute resolution.

… 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
@coderabbitai

coderabbitai Bot commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

@ShaharNaveh ShaharNaveh requested a review from youknowone June 18, 2026 07:45

@youknowone youknowone left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

the patch makes sense.
please check our AI policy and check the checkbox

@youknowone

Copy link
Copy Markdown
Member

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