cachetools icon indicating copy to clipboard operation
cachetools copied to clipboard

Refactor default lock

Open bmwant opened this issue 1 year ago • 1 comments
trafficstars

Description

Make cached and cachedmethod functions more concise by introducing default no-op NoLock class thus eliminating duplication in elif lock is None: branches.

Testing

import timeit
setup = """
from cachetools import cached
import time

@cached(cache={})
def dummy_func():
    time.sleep(0.1)
    return 10 ** 10
"""

s = """
dummy_func()
"""

print(timeit.timeit(setup=setup, stmt=s, number=100_000))
# 0.14263679101713933
# 0.15603854099754244

There is no significant overhead with running no-op locking/unlocking (only 1/100th of a second) and taking into account that caching is always applied to already "heavy" functions it's negligible.

bmwant avatar Aug 18 '24 13:08 bmwant

@tkem let me know what do you think about this change, in my opinion it improves readability and makes it easier to maintain/update in the future.

bmwant avatar Aug 18 '24 13:08 bmwant

@bmwant: Thanks for your interest. You may note that something similar was used in v1.0:

https://github.com/tkem/cachetools/blob/03bf4174c85cef51ab07e92212a9d7838a27093b/cachetools/decorators.py

This was changed on purpose, to make the differences more explicit and also for a (minor) performance gain in case you do not need locks (don't pay for what you don't use).

The next major release will probably add some more options regarding locking/synchronization, which also does not fit nicely with this, IMHO:

https://github.com/tkem/cachetools/blob/4b4bdb8f1bd150cb24459b3896b0e875dc392506/src/cachetools/_decorators.py

tkem avatar Mar 03 '25 13:03 tkem

And apologies for me taking so long to answer!

tkem avatar Mar 03 '25 13:03 tkem