Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make use of intern! macro for attribute names used internally #2273

Merged
merged 4 commits into from
Apr 4, 2022

Conversation

adamreichold
Copy link
Member

Since we do use several attribute names internally, I looked into whether some of them could be candidates for interning. Personally, I do not think this is obvious as while identifiers like __name__ will most likely already be part of Python's string interner dictionary, they do not seem to be used in a hot path of PyO3.

I therefore split this into separate commits to solicit feedback on whether we want to include any of them:

  1. __qualname__ used in PyType::name. Here, I am not sure how often production code using PyO3 actually calls this.
  2. __all__ and __name__ used in the implementation of PyModule. This seems to be limited to initialization, but the strings are almost surely interned already by other code.
  3. The the attribute names used in getattr calls emitted by the FromPyObject derive macro. I think this has the largest possible benefit as this could end up being used in every Python-to-Rust call. But I am also not sure whether enabling this unconditionally is a good idea. (Since the names are given via code and not generated dynamically, this should be ok?)

Copy link
Member

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Sneaky idea that should be nice for performance! It makes me a little uneasy if we know we might have to regress this for PEP 489 combatibility later, however it's still a nice win for the meanwhile.

src/types/module.rs Outdated Show resolved Hide resolved
@davidhewitt davidhewitt mentioned this pull request Apr 4, 2022
@birkenfeld
Copy link
Member

Related idea: add a comment to getattr/setattr recommending intern!?

@adamreichold
Copy link
Member Author

Related idea: add a comment to getattr/setattr recommending intern!?

Add a comment to the methods of Py and PyAny. Would be glad for feedback on the wording. I think linking the PyDict example given at intern!'s docs is sufficient or should I add a getattr-specific one?

Copy link
Member

@birkenfeld birkenfeld left a comment

Choose a reason for hiding this comment

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

I think an example would not go amiss.

src/instance.rs Outdated Show resolved Hide resolved
src/instance.rs Outdated Show resolved Hide resolved
@adamreichold
Copy link
Member Author

I think an example would not go amiss.

Added a single example to each of the four cases.

@davidhewitt davidhewitt changed the base branch from intern-macro to main April 4, 2022 23:41
@davidhewitt davidhewitt merged commit 9774a7c into main Apr 4, 2022
@davidhewitt davidhewitt deleted the dog-food-intern branch April 4, 2022 23:41
@birkenfeld
Copy link
Member

Thanks a lot!

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.

3 participants