Skip to content

Replacing lru_cache with cache, post-3.8#2912

Merged
freakboy3742 merged 2 commits into
beeware:mainfrom
HalfWhitt:remove-lru-cache
Oct 15, 2024
Merged

Replacing lru_cache with cache, post-3.8#2912
freakboy3742 merged 2 commits into
beeware:mainfrom
HalfWhitt:remove-lru-cache

Conversation

@HalfWhitt

@HalfWhitt HalfWhitt commented Oct 15, 2024

Copy link
Copy Markdown
Member

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

@freakboy3742 freakboy3742 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.

A small cleanup, but a nice one - thanks!

@freakboy3742 freakboy3742 merged commit a1dfaec into beeware:main Oct 15, 2024
@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