Skip to content

Commit c9bea5d

Browse files
committed
fix browser preloading
1 parent 89e4229 commit c9bea5d

File tree

2 files changed

+29
-20
lines changed

2 files changed

+29
-20
lines changed

JetStreamDriver.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,8 +2003,10 @@ let BENCHMARKS = [
20032003
"./Kotlin-compose/benchmark.js",
20042004
],
20052005
preload: {
2006-
wasmBinary: "./Kotlin-compose/build/compose-benchmarks-benchmarks.wasm",
2007-
wasmSkikoBinary: "./Kotlin-compose/build/skiko.wasm",
2006+
skikoJsModule: "./Kotlin-compose/build/skiko.mjs",
2007+
skikoWasmBinary: "./Kotlin-compose/build/skiko.wasm",
2008+
composeJsModule: "./Kotlin-compose/build/compose-benchmarks-benchmarks.uninstantiated.mjs",
2009+
composeWasmBinary: "./Kotlin-compose/build/compose-benchmarks-benchmarks.wasm",
20082010
inputImageCompose: "./Kotlin-compose/build/compose-multiplatform.png",
20092011
inputImageCat: "./Kotlin-compose/build/example1_cat.jpg",
20102012
inputImageComposeCommunity: "./Kotlin-compose/build/example1_compose-community-primary.png",

Kotlin-compose/benchmark.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -38,24 +38,31 @@ globalThis.URL = URL;
3838
// first iteration / instantiation measurement.
3939
// The downside is that this doesn't test streaming Wasm instantiation, which we
4040
// are willing to accept.
41-
let preload = {};
41+
let preload = { /* Initialized in init() below due to async. */ };
42+
const originalFetch = globalThis.fetch ?? function(url) {
43+
throw new Error("no fetch available");
44+
}
4245
globalThis.fetch = async function(url) {
4346
// DEBUG
4447
// console.log('fetch', url);
45-
if (!preload[url]) {
46-
throw new Error('Unexpected fetch: ' + url);
48+
49+
// Redirect some paths to cached/preloaded resources.
50+
if (preload[url]) {
51+
return {
52+
ok: true,
53+
status: 200,
54+
arrayBuffer() { return preload[url]; },
55+
async blob() {
56+
return {
57+
size: preload[url].byteLength,
58+
async arrayBuffer() { return preload[url]; }
59+
}
60+
},
61+
};
4762
}
48-
return {
49-
ok: true,
50-
status: 200,
51-
arrayBuffer() { return preload[url]; },
52-
async blob() {
53-
return {
54-
size: preload[url].byteLength,
55-
async arrayBuffer() { return preload[url]; }
56-
}
57-
},
58-
};
63+
64+
// This should only be called in the browser, where fetch() is available.
65+
return originalFetch(url);
5966
};
6067
globalThis.WebAssembly.instantiateStreaming = async function(m,i) {
6168
// DEBUG
@@ -116,8 +123,8 @@ class Benchmark {
116123
// console.log("init");
117124

118125
preload = {
119-
'skiko.wasm': await getBinary(wasmSkikoBinary),
120-
'./compose-benchmarks-benchmarks.wasm': await getBinary(wasmBinary),
126+
'skiko.wasm': await getBinary(skikoWasmBinary),
127+
'./compose-benchmarks-benchmarks.wasm': await getBinary(composeWasmBinary),
121128
'./composeResources/compose_benchmarks.benchmarks.generated.resources/drawable/compose-multiplatform.png': await getBinary(inputImageCompose),
122129
'./composeResources/compose_benchmarks.benchmarks.generated.resources/drawable/example1_cat.jpg': await getBinary(inputImageCat),
123130
'./composeResources/compose_benchmarks.benchmarks.generated.resources/files/example1_compose-community-primary.png': await getBinary(inputImageComposeCommunity),
@@ -128,8 +135,8 @@ class Benchmark {
128135
// We patched `skiko.mjs` to not immediately instantiate the `skiko.wasm`
129136
// module, so that we can move the dynamic JS import here but measure
130137
// WebAssembly compilation and instantiation as part of the first iteration.
131-
this.skikoInstantiate = (await dynamicImport('Kotlin-compose/build/skiko.mjs')).default;
132-
this.mainInstantiate = (await dynamicImport('Kotlin-compose/build/compose-benchmarks-benchmarks.uninstantiated.mjs')).instantiate;
138+
this.skikoInstantiate = (await dynamicImport(skikoJsModule)).default;
139+
this.mainInstantiate = (await dynamicImport(composeJsModule)).instantiate;
133140
}
134141

135142
async runIteration() {

0 commit comments

Comments
 (0)