Skip to content

Commit 483cdcd

Browse files
authored
Micro-optimize preRun/postRun handling. NFC (#22671)
1 parent 104f746 commit 483cdcd

File tree

78 files changed

+97
-97
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+97
-97
lines changed

src/library.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1947,10 +1947,8 @@ addToLibrary({
19471947

19481948
$callRuntimeCallbacks__internal: true,
19491949
$callRuntimeCallbacks: (callbacks) => {
1950-
while (callbacks.length > 0) {
1951-
// Pass the module as the first argument.
1952-
callbacks.shift()(Module);
1953-
}
1950+
// Pass the module as the first argument.
1951+
callbacks.forEach((f) => f(Module));
19541952
},
19551953

19561954
#if SHRINK_LEVEL == 0 || ASYNCIFY == 2

src/postamble.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ if (ENVIRONMENT_IS_WORKER) {
2727
{{{ exportRuntime() }}}
2828

2929
var calledRun;
30+
var calledPrerun;
3031

3132
#if STANDALONE_WASM && MAIN_READS_PARAMS
3233
var mainArgs = undefined;
@@ -46,7 +47,7 @@ function callMain() {
4647
#endif
4748
#if ASSERTIONS
4849
assert(runDependencies == 0, 'cannot call main when async dependencies remain! (listen on Module["onRuntimeInitialized"])');
49-
assert(__ATPRERUN__.length == 0, 'cannot call main when preRun functions remain to be called');
50+
assert(calledPrerun, 'cannot call main without calling preRun first');
5051
#endif
5152

5253
var entryFunction = {{{ getEntryFunction() }}};
@@ -191,22 +192,25 @@ function run() {
191192
}
192193
#endif
193194

194-
preRun();
195+
if (!calledPrerun) {
196+
calledPrerun = 1;
197+
preRun();
195198

196-
// a preRun added a dependency, run will be called later
197-
if (runDependencies > 0) {
199+
// a preRun added a dependency, run will be called later
200+
if (runDependencies > 0) {
198201
#if RUNTIME_DEBUG
199-
dbg('run() called, but dependencies remain, so not running');
202+
dbg('run() called, but dependencies remain, so not running');
200203
#endif
201-
return;
204+
return;
205+
}
202206
}
203207

204208
function doRun() {
205209
// run may have just been called through dependencies being fulfilled just in this very frame,
206210
// or while the async setStatus time below was happening
207211
if (calledRun) return;
208-
calledRun = true;
209-
Module['calledRun'] = true;
212+
calledRun = 1;
213+
Module['calledRun'] = 1;
210214

211215
if (ABORT) return;
212216

src/preamble.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -195,11 +195,10 @@ function preRun() {
195195
assert(!ENVIRONMENT_IS_PTHREAD); // PThreads reuse the runtime from the main thread.
196196
#endif
197197
#if expectToReceiveOnModule('preRun')
198-
if (Module['preRun']) {
199-
if (typeof Module['preRun'] == 'function') Module['preRun'] = [Module['preRun']];
200-
while (Module['preRun'].length) {
201-
addOnPreRun(Module['preRun'].shift());
202-
}
198+
var preRuns = Module['preRun'];
199+
if (preRuns) {
200+
if (typeof preRuns == 'function') preRuns = [preRuns];
201+
preRuns.forEach(addOnPreRun);
203202
}
204203
#endif
205204
callRuntimeCallbacks(__ATPRERUN__);
@@ -289,11 +288,10 @@ function postRun() {
289288
#endif
290289

291290
#if expectToReceiveOnModule('postRun')
292-
if (Module['postRun']) {
293-
if (typeof Module['postRun'] == 'function') Module['postRun'] = [Module['postRun']];
294-
while (Module['postRun'].length) {
295-
addOnPostRun(Module['postRun'].shift());
296-
}
291+
var postRuns = Module['postRun'];
292+
if (postRuns) {
293+
if (typeof postRuns == 'function') postRuns = [postRuns];
294+
postRuns.forEach(addOnPostRun);
297295
}
298296
#endif
299297

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8466
1+
8470
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20855
1+
20801
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8450
1+
8452
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20823
1+
20769
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
9488
1+
9497
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
24699
1+
24645
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8426
1+
8432

0 commit comments

Comments
 (0)