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

⚡ Speedup ModelPrivateAttr #5709

Merged
merged 1 commit into from
May 8, 2023
Merged

⚡ Speedup ModelPrivateAttr #5709

merged 1 commit into from
May 8, 2023

Conversation

lig
Copy link
Contributor

@lig lig commented May 7, 2023

Change Summary

This should speedup using private model attributes as using hasattr is significantly faster than getattr in case of an attribute doesn't not exist. It seems likely that ModelPrivateAttr would be used without a custom __set_name__ more often.

>>> import timeit
>>> timeit.timeit("hasattr(a, 'foo')", setup="a = type('A', (), {})")
0.17332394601544365
>>> timeit.timeit("try: getattr(a, 'foo')\nexcept AttributeError: pass", setup="a = type('A', (), {})")
0.38944376300787553

Related issue number

None

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • changes/<pull request or issue id>-<github username>.md file added describing change
    (see changes/README.md for details)
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer: @dmontagu

@lig lig marked this pull request as ready for review May 7, 2023 09:06
@lig
Copy link
Contributor Author

lig commented May 7, 2023

please review

Copy link
Contributor

@dmontagu dmontagu left a comment

Choose a reason for hiding this comment

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

LGTM

@dmontagu dmontagu merged commit 7fd643e into main May 8, 2023
42 checks passed
@dmontagu dmontagu deleted the speedup-model-private-attr branch May 8, 2023 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants