MCPcopy Index your code
hub / github.com/RustPython/RustPython / _lru_cache_wrapper

Function _lru_cache_wrapper

Lib/functools.py:600–714  ·  view source on GitHub ↗
(user_function, maxsize, typed, _CacheInfo)

Source from the content-addressed store, hash-verified

598 return decorating_function
599
600def _lru_cache_wrapper(user_function, maxsize, typed, _CacheInfo):
601 # Constants shared by all lru cache instances:
602 sentinel = object() # unique object used to signal cache misses
603 make_key = _make_key # build a key from the function arguments
604 PREV, NEXT, KEY, RESULT = 0, 1, 2, 3 # names for the link fields
605
606 cache = {}
607 hits = misses = 0
608 full = False
609 cache_get = cache.get # bound method to lookup a key or return None
610 cache_len = cache.__len__ # get cache size without calling len()
611 lock = RLock() # because linkedlist updates aren't threadsafe
612 root = [] # root of the circular doubly linked list
613 root[:] = [root, root, None, None] # initialize by pointing to self
614
615 if maxsize == 0:
616
617 def wrapper(*args, **kwds):
618 # No caching -- just a statistics update
619 nonlocal misses
620 misses += 1
621 result = user_function(*args, **kwds)
622 return result
623
624 elif maxsize is None:
625
626 def wrapper(*args, **kwds):
627 # Simple caching without ordering or size limit
628 nonlocal hits, misses
629 key = make_key(args, kwds, typed)
630 result = cache_get(key, sentinel)
631 if result is not sentinel:
632 hits += 1
633 return result
634 misses += 1
635 result = user_function(*args, **kwds)
636 cache[key] = result
637 return result
638
639 else:
640
641 def wrapper(*args, **kwds):
642 # Size limited caching that tracks accesses by recency
643 nonlocal root, hits, misses, full
644 key = make_key(args, kwds, typed)
645 with lock:
646 link = cache_get(key)
647 if link is not None:
648 # Move the link to the front of the circular queue
649 link_prev, link_next, _key, result = link
650 link_prev[NEXT] = link_next
651 link_next[PREV] = link_prev
652 last = root[PREV]
653 last[NEXT] = root[PREV] = link
654 link[PREV] = last
655 link[NEXT] = root
656 hits += 1
657 return result

Callers 2

lru_cacheFunction · 0.85
decorating_functionFunction · 0.85

Calls 1

RLockClass · 0.90

Tested by

no test coverage detected