Skip to content

[wasm-split] Remove module elements in bulk (NFC) - #8990

Open
aheejin wants to merge 3 commits into
wasm_split_owning_modulesfrom
wasm_split_remove_opt
Open

[wasm-split] Remove module elements in bulk (NFC)#8990
aheejin wants to merge 3 commits into
wasm_split_owning_modulesfrom
wasm_split_remove_opt

Conversation

@aheejin

@aheejin aheejin commented Aug 12, 2026

Copy link
Copy Markdown
Member

Previously we removed module elements one by one within a loop. But because Module stores a module element in both a map and a vector, removing a single module element using removeModuleElement is O(N), because it needs to shift all vector elements after it:

binaryen/src/wasm/wasm.cpp

Lines 1970 to 1979 in 302396a

template<typename Vector, typename Map>
void removeModuleElement(Vector& v, Map& m, Name name) {
m.erase(name);
for (size_t i = 0; i < v.size(); i++) {
if (v[i]->name == name) {
v.erase(v.begin() + i);
break;
}
}
}

This removes module elements in bulk using removeModuleElements, which does the shifting only once.

binaryen/src/wasm/wasm.cpp

Lines 2004 to 2018 in 302396a

template<typename Vector, typename Map, typename Elem>
void removeModuleElements(Vector& v,
Map& m,
std::function<bool(Elem* elem)> pred) {
for (auto it = m.begin(); it != m.end();) {
if (pred(it->second)) {
it = m.erase(it);
} else {
it++;
}
}
v.erase(
std::remove_if(v.begin(), v.end(), [&](auto& e) { return pred(e.get()); }),
v.end());
}

Combining with #8986, acx_gallery's running time improved by 50.3% (30s -> 15s), and essentials by 60.8% (230s -> 90s). (for Jul 2026 version)

I guess the main reason for the running time increase in #8441 was this O(N) removeModuleElement called within a loop after all.

Previously we removed module elements one by one within a loop. But
because `Module` stores a module element in both a map and a vector,
removing a single module element using `removeModuleElement` is O(N),
because it needs to shift all vector elements after it:
https://github.com/WebAssembly/binaryen/blob/302396a676433152a32375a81d71e74687c97a1b/src/wasm/wasm.cpp#L1970-L1979

This removes module elements in bulk using `removeModuleElements`, which
does the shifting only once.
https://github.com/WebAssembly/binaryen/blob/302396a676433152a32375a81d71e74687c97a1b/src/wasm/wasm.cpp#L2004-L2018

Combining with #8986, acx_gallery's running time improved by 50.3% (30s
-> 15s), and essentials by 60.8% (230s -> 90s). (for Jul 2026 version)

I guess the main reason for the running time increase in #8441 was this
O(N) `removeModuleElement` called within a loop after all.
@aheejin
aheejin requested a review from a team as a code owner August 12, 2026 03:05
@aheejin
aheejin requested review from tlively and removed request for a team August 12, 2026 03:05

@tlively tlively left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice!

Comment thread src/ir/module-splitting.cpp Outdated
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