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

Replacing lru_cache with cache, post-3.8 #2912

Merged
merged 2 commits into from
Oct 15, 2024

Conversation

HalfWhitt
Copy link
Contributor

@HalfWhitt HalfWhitt commented Oct 15, 2024

Now that Toga's dropping 3.8 support, I remembered a spot in images where I used functools.lru_cache because cache wasn't added until 3.9.

From the docs on cache:

Returns the same as lru_cache(maxsize=None), creating a thin wrapper around a dictionary lookup for the function arguments. Because it never needs to evict old values, this is smaller and faster than lru_cache() with a size limit.

So in that case it's a one-to-one swap. But I noticed one other place too, in platform. There it's maxsize=1 instead of None, which could behave differently — only keeping the most recent call instead of all calls — except that get_platform_factory() accepts no arguments, so it can only ever cache one value anyway. So setting it to None makes the result potentially smaller and faster without changing behavior, and that in turn is the same as cache.

PR Checklist:

  • All new features have been tested
  • All new features have been documented
  • I have read the CONTRIBUTING.md file
  • I will abide by the code of conduct

Copy link
Member

@freakboy3742 freakboy3742 left a comment

Choose a reason for hiding this comment

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

A small cleanup, but a nice one - thanks!

@freakboy3742 freakboy3742 merged commit a1dfaec into beeware:main Oct 15, 2024
40 checks passed
@HalfWhitt HalfWhitt deleted the remove-lru-cache branch October 15, 2024 23:31
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