Repository navigation
Only visit unresolved GOT entries in reportUndefinedSymbols - #28303
Conversation
There was a problem hiding this comment.
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.
| #if DYLINK_DEBUG | ||
| dbg('reportUndefinedSymbols'); | ||
| #endif | ||
| for (var [symName, entry] of Object.entries(GOT)) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Good point. I forgot this was O(all symbols) not just O(new symbols)
|
I feel like |
|
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
left a comment
There was a problem hiding this comment.
Those numbers do look good.
lgtm, but can you add the size of the GOT in both case the PR description.
@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? |
|
I enabled auto-merge, but it looks like you need to do one more codesize rebase. |
Head branch was pushed to by a user without write access
|
I did codesize rebase. Could you please trigger auto merge again? Thanks! |
|
Sorry, one more time. |
Head branch was pushed to by a user without write access
|
Done! |
| } | ||
| } | ||
| unresolvedSymbols.delete(symName); | ||
| } |
There was a problem hiding this comment.
Instead of deleting each element (and duplicating the .delete line), could use just call unresolvedSymbols.clear() at the end of the loop?
There was a problem hiding this comment.
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.
A small performance improvement for
reportUndefinedSymbols.Previously, it loops through all the
GOTtable 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
unresolvedSymbolsset and makereportUndefinedSymbolsvisit that set instead of looping through the entire GOT table.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
reportUndefinedSymbolsis reduced as follows in Pyodide with this change:reportUndefinedSymbols(during Pyodide startup)reportUndefinedSymbols(during loading numpy)