Skip to content

Commit de65035

Browse files
committed
Optimize llvm-nm invocations from Emscripten. In python creating a multiprocessing pool is *extremely* slow, and can take 500ms-1 second. If there are only few files to llvm-nm, do those sequentially since a single llvm-nm only takes about 10-20ms.
1 parent 6711e3f commit de65035

File tree

1 file changed

+28
-5
lines changed

1 file changed

+28
-5
lines changed

tools/building.py

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,7 @@ def make_paths_absolute(f):
364364
return os.path.abspath(f)
365365

366366

367-
# Runs llvm-nm in parallel for the given list of files.
368-
# The results are populated in nm_cache
369-
# multiprocessing_pool: An existing multiprocessing pool to reuse for the operation, or None
370-
# to have the function allocate its own.
371-
def parallel_llvm_nm(files):
367+
def multithreaded_llvm_nm(files):
372368
with ToolchainProfiler.profile_block('parallel_llvm_nm'):
373369
pool = get_multiprocessing_pool()
374370
object_contents = pool.map(g_llvm_nm_uncached, files)
@@ -380,6 +376,33 @@ def parallel_llvm_nm(files):
380376
return object_contents
381377

382378

379+
def singlethreaded_llvm_nm(files):
380+
with ToolchainProfiler.profile_block('singlethreaded_llvm_nm'):
381+
object_contents = []
382+
for file in files:
383+
obj = llvm_nm_uncached(file)
384+
if obj.returncode != 0:
385+
logger.debug('llvm-nm failed on file ' + file + ': return code ' + str(obj.returncode) + ', error: ' + obj.output)
386+
nm_cache[file] = obj
387+
object_contents += [obj]
388+
return object_contents
389+
390+
391+
# Runs llvm-nm for the given list of files.
392+
# The results are populated in nm_cache
393+
def parallel_llvm_nm(files):
394+
# Python multiprocessing pool (get_multiprocessing_pool() and pool.map() functions) have a lot of overhead,
395+
# so run the llvm-nm calls sequentially if it will be faster than waiting for the pool to come up.
396+
heuristic_multiprocessing_pool_startup_time_msecs = 700
397+
# Individual llvm-nm completes very fast
398+
heuristic_single_threaded_llvm_nm_execution_time_msecs = 10
399+
400+
if heuristic_multiprocessing_pool_startup_time_msecs + len(files) * heuristic_single_threaded_llvm_nm_execution_time_msecs / get_num_cores() < len(files) * heuristic_single_threaded_llvm_nm_execution_time_msecs:
401+
return multithreaded_llvm_nm(files)
402+
else:
403+
return singlethreaded_llvm_nm(files)
404+
405+
383406
def read_link_inputs(files):
384407
with ToolchainProfiler.profile_block('read_link_inputs'):
385408
# Before performing the link, we need to look at each input file to determine which symbols

0 commit comments

Comments
 (0)