Skip to content

Commit 678f3bc

Browse files
authored
Use a single/shared implementation of @memoize. NFC (#23476)
1 parent 5023c38 commit 678f3bc

File tree

4 files changed

+20
-39
lines changed

4 files changed

+20
-39
lines changed

emcc.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
from tools import config
4646
from tools import cache
4747
from tools.settings import default_setting, user_settings, settings, MEM_SIZE_SETTINGS, COMPILE_TIME_SETTINGS
48-
from tools.utils import read_file, removeprefix
48+
from tools.utils import read_file, removeprefix, memoize
4949
from tools import feature_matrix
5050

5151
logger = logging.getLogger('emcc')
@@ -422,14 +422,8 @@ def get_clang_flags(user_args):
422422
return flags
423423

424424

425-
cflags = None
426-
427-
425+
@memoize
428426
def get_cflags(user_args):
429-
global cflags
430-
if cflags:
431-
return cflags
432-
433427
# Flags we pass to the compiler when building C/C++ code
434428
# We add these to the user's flags (newargs), but not when building .s or .S assembly files
435429
cflags = get_clang_flags(user_args)

tools/shared.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
from .toolchain_profiler import ToolchainProfiler
77

88
from enum import Enum, unique, auto
9-
from functools import wraps
109
from subprocess import PIPE
1110
import atexit
1211
import json
@@ -40,7 +39,7 @@
4039
logging.basicConfig(format='%(name)s:%(levelname)s: %(message)s', level=log_level)
4140
colored_logger.enable()
4241

43-
from .utils import path_from_root, exit_with_error, safe_ensure_dirs, WINDOWS, set_version_globals
42+
from .utils import path_from_root, exit_with_error, safe_ensure_dirs, WINDOWS, set_version_globals, memoize
4443
from . import cache, tempfiles
4544
from . import diagnostics
4645
from . import config
@@ -273,22 +272,6 @@ def get_npm_cmd(name):
273272
return cmd
274273

275274

276-
# TODO(sbc): Replace with functools.cache, once we update to python 3.7
277-
def memoize(func):
278-
called = False
279-
result = None
280-
281-
@wraps(func)
282-
def helper():
283-
nonlocal called, result
284-
if not called:
285-
result = func()
286-
called = True
287-
return result
288-
289-
return helper
290-
291-
292275
@memoize
293276
def get_clang_version():
294277
if not os.path.exists(CLANG_CC):

tools/utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import os
77
import shutil
88
import sys
9+
from functools import wraps
910
from pathlib import Path
1011

1112
from . import diagnostics
@@ -98,6 +99,21 @@ def delete_contents(dirname, exclude=None):
9899
delete_file(entry)
99100

100101

102+
# TODO(sbc): Replace with functools.cache, once we update to python 3.7
103+
def memoize(func):
104+
results = {}
105+
106+
@wraps(func)
107+
def helper(*args, **kwargs):
108+
assert not kwargs
109+
key = (func.__name__, args)
110+
if key not in results:
111+
results[key] = func(*args)
112+
return results[key]
113+
114+
return helper
115+
116+
101117
# TODO: Move this back to shared.py once importing that file becoming side effect free (i.e. it no longer requires a config).
102118
def set_version_globals():
103119
global EMSCRIPTEN_VERSION, EMSCRIPTEN_VERSION_MAJOR, EMSCRIPTEN_VERSION_MINOR, EMSCRIPTEN_VERSION_TINY

tools/webassembly.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import os
1414
import sys
1515

16+
from .utils import memoize
1617
from . import utils
1718

1819
sys.path.append(utils.path_from_root('third_party'))
@@ -55,19 +56,6 @@ def read_sleb(iobuf):
5556
return leb128.i.decode_reader(iobuf)[0]
5657

5758

58-
def memoize(method):
59-
60-
@wraps(method)
61-
def wrapper(self, *args, **kwargs):
62-
assert not kwargs
63-
key = (method.__name__, args)
64-
if key not in self._cache:
65-
self._cache[key] = method(self, *args, **kwargs)
66-
return self._cache[key]
67-
68-
return wrapper
69-
70-
7159
def once(method):
7260

7361
@wraps(method)

0 commit comments

Comments
 (0)