Skip to content

Commit cb06fb7

Browse files
authored
Use functools.lru_cache to implement memoize (NFC) (#25052)
In Python 3.9 this can be replaced by `functools.cache`, but at least this can be a one-liner now.
1 parent 4693f65 commit cb06fb7

File tree

1 file changed

+3
-14
lines changed

1 file changed

+3
-14
lines changed

tools/utils.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import os
77
import shutil
88
import sys
9-
from functools import wraps
9+
import functools
1010
from pathlib import Path
1111

1212
from . import diagnostics
@@ -112,19 +112,8 @@ def delete_contents(dirname, exclude=None):
112112
delete_file(entry)
113113

114114

115-
# TODO(sbc): Replace with functools.cache, once we update to python 3.7
116-
def memoize(func):
117-
results = {}
118-
119-
@wraps(func)
120-
def helper(*args, **kwargs):
121-
assert not kwargs
122-
key = (func.__name__, args)
123-
if key not in results:
124-
results[key] = func(*args)
125-
return results[key]
126-
127-
return helper
115+
# TODO(sbc): Replace with functools.cache, once we update to python 3.9
116+
memoize = functools.lru_cache(maxsize=None)
128117

129118

130119
# TODO: Move this back to shared.py once importing that file becoming side effect free (i.e. it no longer requires a config).

0 commit comments

Comments
 (0)