Skip to content

Only visit unresolved GOT entries in reportUndefinedSymbols - #28303

Merged
sbc100 merged 9 commits into
emscripten-core:mainfrom
ryanking13:pending-got
Nov 23, 2026
Merged

sbc100 merged 9 commits into
emscripten-core:mainfrom
ryanking13:pending-got

Conversation

@ryanking13

@ryanking13 ryanking13 commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

A small performance improvement for reportUndefinedSymbols.

Previously, it loops through all the GOT table to see if the symbol is undefined. However, this is inefficient since we already know which symbols are undefined at this point. However, if we introduce a new mapping to track just the undefined symbols, we can avoid this full pass.

This PR update it to store the list of undefined symbols to unresolvedSymbols set and make reportUndefinedSymbols visit that set instead of looping through the entire GOT table.

- for (var [symName, entry] of Object.entries(GOT)) {
+ for (var symName of unresolvedSymbols) {

Impact

In Pyodide, main module (CPython interpreter) has 8,836 GOT entries. Only 280 of them were "undefined symbols" that are resolved by reportUndefinedSymbols.

So, when loading the main module, it reduces looping through 8,836 entries to 280.

When loading the side module, the impact is bigger. For example, when loading numpy (a side module) in Pyodide, it has 13 side modules (.so) files, which has ~43 undefined symbols in total. Previously, each module should look at all 8000+ GOT entries, but now they only look at 43 symbols.

The time spent for reportUndefinedSymbols is reduced as follows in Pyodide with this change:

before after (ms)
reportUndefinedSymbols (during Pyodide startup) 6.20 3.03
reportUndefinedSymbols (during loading numpy) 25.84 6.79

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request optimizes reportUndefinedSymbols in src/lib/libdylink.js by introducing a $pendingGOT Set to track and iterate only over GOT entries created without a value, rather than iterating over all GOT entries. A critical issue was identified where unresolved strong symbols are unconditionally deleted from pendingGOT at the end of the loop. If ERROR_ON_UNDEFINED_SYMBOLS is disabled, these symbols will remain undefined but will no longer be tracked or reported in subsequent calls. It is recommended to only delete the symbol from pendingGOT if it has been successfully resolved.

Comment thread src/lib/libdylink.js Resolved
Comment thread src/lib/libdylink.js
#if DYLINK_DEBUG
dbg('reportUndefinedSymbols');
#endif
for (var [symName, entry] of Object.entries(GOT)) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is a single linear pass over the whole symbol table really the expensive that we want to allocate a completely new data structure and tracking mechanism for pendingGOT?

Seems like extra complexity, that maybe isn't worth the cost? How expensive is the Object.entries pass here?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is especially useful when an application loads side modules a lot, since this function is called everytime a side module is loaded and the size of GOT table will keep increase.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I forgot this was O(all symbols) not just O(new symbols)

@sbc100

sbc100 commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

I feel like we already know which symbols are undefined is overstating it. How about However, if we introduce a new mapping to track just the undefined symbols, we can avoid this full pass.

@sbc100

sbc100 commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Sorry, I see you already posted the numbers.

Can you ask, how many symbols are in the GOT in both examples? Are talking hundreds of thousands?

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those numbers do look good.

lgtm, but can you add the size of the GOT in both case the PR description.

@ryanking13

Copy link
Copy Markdown
Contributor Author

Can you ask, how many symbols are in the GOT in both examples? Are talking hundreds of thousands?

@sbc100 Ah sorry, I thought I added that in the description but just realized that I didn't. I updated the number of GOT entries in Pyodide. How about now?

Comment thread src/lib/libdylink.js Resolved
@sbc100
sbc100 enabled auto-merge (squash) November 19, 2026 04:36
@sbc100

sbc100 commented Nov 19, 2026

Copy link
Copy Markdown
Collaborator

I enabled auto-merge, but it looks like you need to do one more codesize rebase.

auto-merge was automatically disabled November 19, 2026 05:18

Head branch was pushed to by a user without write access

@ryanking13

Copy link
Copy Markdown
Contributor Author

I did codesize rebase. Could you please trigger auto merge again? Thanks!

@sbc100
sbc100 enabled auto-merge (squash) November 20, 2026 17:51
@sbc100

sbc100 commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

Sorry, one more time.

auto-merge was automatically disabled November 22, 2026 11:21

Head branch was pushed to by a user without write access

@ryanking13

Copy link
Copy Markdown
Contributor Author

Done!

Comment thread src/lib/libdylink.js Resolved

@sbc100 sbc100 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm % final comment

Comment thread src/lib/libdylink.js
}
}
unresolvedSymbols.delete(symName);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of deleting each element (and duplicating the .delete line), could use just call unresolvedSymbols.clear() at the end of the loop?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I think it makes sense. The edge case would be when reportUndefinedSymbols throws (which will mess up the runtime state anyways?). When it throws, the unresolvedSymbols will remain uncleared but it would be harmless except that it keeps sitting in the memory.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, exactly.

@sbc100
sbc100 merged commit 1fb9d4e into emscripten-core:main Nov 23, 2026
44 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants