Skip to content

dlsym enumerates every export of the library on each call (Object.keys(lib.exports).indexOf) #28124

Closed

Description

@basvandijk

_dlsym_js (src/lib/libdylink.js) starts every lookup with

newSymIndex = Object.keys(lib.exports).indexOf(symbol);
if (newSymIndex == -1 || lib.exports[symbol].stub) { ... }

For RTLD_DEFAULT on a main module, lib.exports is wasmImports, which holds every export of the main module. Object.keys builds an array of all N keys (V8 also sorts them, since an object with that many properties is in dictionary mode), and indexOf scans it, so one dlsym call costs O(N log N) in the number of exported symbols, whether the symbol is found or not. The index is only used in one place: when addFunction allocates a new table slot and other threads must replay the insertion by index (_dlsym_catchup_js, from #18590). In 6.0.13 every exported function of the main module is already in the table when dlsym runs (updateGOT adds them at startup), so that branch is not even reached, yet the scan is paid every time.

Measurements (Emscripten 6.0.13, Node 26.13.0, emcc -O2 -pthread -sMAIN_MODULE=1 -sEXIT_RUNTIME=1, a program with N exported functions calling dlsym(RTLD_DEFAULT, name) 100 times; ms per call; the native column is the same program built with clang -O2 -rdynamic against glibc):

exports hit miss native hit
1,000 0.50 0.41 0.001
10,000 1.72 1.58 0.001
50,000 8.13 8.26 0.005
100,000 23.9 23.3 0.007
250,000 80.4 81.4 0.016

Real-world case: Lean 4's lean.js is a main module with 247,834 exports (its interpreter resolves compiled code by symbol name at run time). Starting it on an empty file makes 2,630 dlsym calls (module initializers and imported parser constants), each 65-130 ms (median 75 ms) on an AMD EPYC 7302 core; they take 210 s of the 236 s run, and most of the roughly two minutes each test of its CI job needs.

Proposed fix: test membership with Object.prototype.hasOwnProperty.call(lib.exports, symbol) (same answer as Object.keys(...).indexOf(symbol) != -1 for these plain objects) and compute the enumeration index only in the branch that allocates a table slot, right before it is stored into symbolIndex. That keeps the thread catch-up protocol unchanged and makes the common path O(1). With that change the 250,000-export program's dlsym takes 0.009 ms per call instead of 80.9 ms (10,000 exports: 0.008 ms instead of 1.70 ms; measured with the same program built by the patched and the unpatched main), and Lean's 2,630 startup lookups take 17 ms in total instead of 210 s, so lean starts in 20 s instead of 236 s with no change on Lean's side. I have the change ready as a PR if you want it.

Repro (generates bench_<N>.c; build and run with the command above):

dlsym-bench.py
import os, sys
outdir = sys.argv[1]
os.makedirs(outdir, exist_ok=True)
for n in map(int, sys.argv[2:]):
    lines = ['#include <dlfcn.h>', '#include <stdio.h>', '#include <time.h>', '']
    lines += [f'int sym_{i}(int x) {{ return x + {i}; }}' for i in range(n)]
    lines += ['',
              'static double now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; }',
              'int main(void) {',
              f'    const int n = {n}, rounds = 100;',
              '    char name[64];',
              '    int found = 0, missing = 0;',
              '    double t0 = now_ms();',
              '    for (int r = 0; r < rounds; r++) {',
              '        snprintf(name, sizeof name, "sym_%d", (int)((long long)r * n / rounds));',
              '        found += dlsym(RTLD_DEFAULT, name) != NULL;',
              '    }',
              '    double t1 = now_ms();',
              '    for (int r = 0; r < rounds; r++) {',
              '        snprintf(name, sizeof name, "nosuch_%d", r);',
              '        missing += dlsym(RTLD_DEFAULT, name) == NULL;',
              '    }',
              '    double t2 = now_ms();',
              '    printf("exports=%d hit: %.3f ms/call (%d found)  miss: %.3f ms/call (%d missing)\\n",',
              '           n, (t1 - t0) / rounds, found, (t2 - t1) / rounds, missing);',
              '    return (found == rounds && missing == rounds) ? 0 : 1;',
              '}', '']
    with open(os.path.join(outdir, f'bench_{n}.c'), 'w') as f:
        f.write('\n'.join(lines))

Activity

  1. added a commit that references this issue on Nov 2, 2026
    9c2d68c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions