From 75839f97c80559b32aac0d59a990f51da61b5240 Mon Sep 17 00:00:00 2001 From: Heejin Ahn Date: Tue, 27 Jan 2026 23:21:27 +0000 Subject: [PATCH] [wasm-split] "Move" secondary functions `moveSecondaryFunctions`, despite the name, actually copies functions to secondary modules and removes them from the primary module. This prevents this overhead by actually moving the functions to secondary modules. This reduces the running time on acx_gallery (provided by @biggs0125) by 31%. --- - Before this PR: Time: 16.4s Task breakdown: ``` Task Total Time (ms) Percentage --------------------------------------------------------------------------- moveSecondaryFunctions 5341.1700 43.94% writeModule_secondary 2960.8319 24.36% shareImportableItems 1568.6400 12.91% writeModule_primary 765.7240 6.30% exportImportCalledPrimaryFunctions 762.9670 6.28% indirectReferencesToSecondaryFunctions 362.3300 2.98% classifyFunctions 204.4540 1.68% indirectCallsToSecondaryFunctions 139.2110 1.15% setupTablePatching 45.2461 0.37% thunkExportedSecondaryFunctions 3.0373 0.02% initExportedPrimaryFuncs 0.9982 0.01% --------------------------------------------------------------------------- Overall Total 12154.6095 100.00% ``` - After this PR: Time: 11.3s ``` Task Total Time (ms) Percentage --------------------------------------------------------------------------- writeModule_secondary 2908.4728 43.52% shareImportableItems 1562.7800 23.39% exportImportCalledPrimaryFunctions 786.6140 11.77% writeModule_primary 751.3420 11.24% indirectReferencesToSecondaryFunctions 250.7260 3.75% indirectCallsToSecondaryFunctions 149.7940 2.24% classifyFunctions 126.4520 1.89% moveSecondaryFunctions 106.0680 1.59% setupTablePatching 38.9292 0.58% initExportedPrimaryFuncs 0.8816 0.01% thunkExportedSecondaryFunctions 0.4937 0.01% --------------------------------------------------------------------------- Overall Total 6682.5533 100.00% ``` We can see the time spent in `moveSecondaryFunctions`, which used to take nearly half of the running time (after #8221), is now negligible. --- src/ir/module-splitting.cpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/src/ir/module-splitting.cpp b/src/ir/module-splitting.cpp index 9ffa4784da1..04730386c1e 100644 --- a/src/ir/module-splitting.cpp +++ b/src/ir/module-splitting.cpp @@ -515,13 +515,26 @@ void ModuleSplitter::exportImportFunction(Name funcName, void ModuleSplitter::moveSecondaryFunctions() { // Move the specified functions from the primary to the secondary modules. + std::unordered_map> movedFunctions; + auto& functions = primary.functions; + for (auto& func : functions) { + if (allSecondaryFuncs.count(func->name)) { + movedFunctions[func->name] = std::move(func); + } + } + functions.erase( + std::remove_if(functions.begin(), + functions.end(), + [](const std::unique_ptr& func) { return !func; }), + functions.end()); + primary.updateFunctionsMap(); + for (auto& funcNames : config.secondaryFuncs) { auto secondary = initSecondary(primary); for (auto funcName : funcNames) { if (allSecondaryFuncs.count(funcName)) { - auto* func = primary.getFunction(funcName); - ModuleUtils::copyFunction(func, *secondary); - primary.removeFunction(funcName); + assert(movedFunctions.count(funcName)); + secondary->addFunction(std::move(movedFunctions[funcName])); funcToSecondaryIndex[funcName] = secondaries.size(); } }