Skip to content

Optimize cached_method when wrapping no-arg methods #6381

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

Merged
merged 1 commit into from
Dec 13, 2023

Conversation

maffoo
Copy link
Contributor

@maffoo maffoo commented Dec 13, 2023

Previously, the first time a no-arg cached method was called the decorator would make three calls: hasattr, object.__setattr__, getattr, and when called subsequently it would make two calls: hasattr, getattr. Here we refactor the implementation to use a sentinel value so that on the first call we only make two calls: getattr, object.__setattr__, and when called subsequently we only make a single getattr call.

Previously, the first time a no-arg cached method was called the
decorator would make three calls: `hasattr`, `object.__setattr__`,
`getattr`, and when called subsequently it would make two calls:
`hasattr`, `getattr`. Here we refactor the implementation to use a
sentinel value so that on the first call we only make two calls:
`getattr`, `object.__setattr__`, and when called subsequently we only
make a single `getattr` call.
@maffoo maffoo requested review from vtomole, cduck and a team as code owners December 13, 2023 04:31
@maffoo maffoo requested a review from viathor December 13, 2023 04:31
Copy link

codecov bot commented Dec 13, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (ec727ca) 97.81% compared to head (fbb6635) 97.80%.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6381      +/-   ##
==========================================
- Coverage   97.81%   97.80%   -0.01%     
==========================================
  Files        1111     1111              
  Lines       96948    96951       +3     
==========================================
- Hits        94825    94824       -1     
- Misses       2123     2127       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@maffoo maffoo merged commit 2f3c1e2 into main Dec 13, 2023
@maffoo maffoo deleted the u/maffoo/cached-no-arg branch December 13, 2023 05:29
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
Previously, the first time a no-arg cached method was called the
decorator would make three calls: `hasattr`, `object.__setattr__`,
`getattr`, and when called subsequently it would make two calls:
`hasattr`, `getattr`. Here we refactor the implementation to use a
sentinel value so that on the first call we only make two calls:
`getattr`, `object.__setattr__`, and when called subsequently we only
make a single `getattr` call.
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