Skip to content

Commit da5deee

Browse files
committed
Cache symbol lists used by LLD_REPORT_UNDEFINED. NFC
This means that the JS libraries only only need to be processed when there is cache miss. The cost of processing the JS libraries is about 300ms on my machine which is about 30% of the link time for hello world. When there is cache hit this cost is reduced to 3ms. This change is in prepartion for switching this mode on my default in. See: #16003
1 parent 58a52ab commit da5deee

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

emcc.py

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,7 @@ def ensure_archive_index(archive_file):
500500
run_process([shared.LLVM_RANLIB, archive_file])
501501

502502

503-
@ToolchainProfiler.profile_block('JS symbol generation')
504-
def get_all_js_syms():
503+
def generate_js_symbols():
505504
# Runs the js compiler to generate a list of all symbols available in the JS
506505
# libraries. This must be done separately for each linker invokation since the
507506
# list of symbols depends on what settings are used.
@@ -516,6 +515,44 @@ def get_all_js_syms():
516515
if shared.is_c_symbol(name):
517516
name = shared.demangle_c_symbol_name(name)
518517
library_syms.add(name)
518+
return library_syms
519+
520+
521+
@ToolchainProfiler.profile_block('JS symbol generation')
522+
def get_all_js_syms():
523+
# To avoid the cost of calling generate_js_symbols each time an executable is
524+
# linked we cache symbol lists for the N most recently used configs.
525+
# We define a cache hit as when the settings and `--js-library` contents are
526+
# identical.
527+
input_data = json.dumps(settings.dict(), sort_keys=True, indent=2) + '\n'
528+
for jslib in settings.JS_LIBRARIES:
529+
if os.path.abspath(jslib):
530+
jslib = utils.path_from_root('src', jslib)
531+
input_data += read_file(jslib)
532+
cache_filename = None
533+
num_cache_entries = 20
534+
535+
with cache.lock('js_symbol_lists'):
536+
oldest_timestamp = 0
537+
for i in range(num_cache_entries):
538+
input_file = cache.get_path(f'js_symbol_list_{i}.inputs')
539+
list_file = cache.get_path(f'js_symbol_list_{i}.txt')
540+
if not os.path.exists(input_file) or not os.path.exists(list_file):
541+
cache_filename = list_file
542+
break
543+
timestamp = os.path.getmtime(input_file)
544+
if timestamp < oldest_timestamp or not oldest_timestamp:
545+
oldest_timestamp = timestamp
546+
cache_filename = list_file
547+
if read_file(input_file) == input_data:
548+
# Cache hit, read the symbol list from the list_file
549+
return read_file(list_file).splitlines()
550+
551+
# Cache miss. Generate a new symbol list and write to the the cache
552+
library_syms = generate_js_symbols()
553+
554+
write_file(cache_filename, '\n'.join(library_syms) + '\n')
555+
write_file(shared.replace_suffix(cache_filename, '.inputs'), input_data)
519556

520557
return library_syms
521558

0 commit comments

Comments
 (0)