Skip to content

Commit df197ef

Browse files
authored
Merge pull request #93 from danleh/cleanup-loadInternal
Cleanup of non-browser prefetch code
2 parents e30a452 + 78070d5 commit df197ef

File tree

1 file changed

+39
-84
lines changed

1 file changed

+39
-84
lines changed

JetStreamDriver.js

Lines changed: 39 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -181,47 +181,27 @@ function uiFriendlyDuration(time) {
181181
return `${time.toFixed(3)} ms`;
182182
}
183183

184+
// TODO: Cleanup / remove / merge. This is only used for caching loads in the
185+
// non-browser setting. In the browser we use exclusively `loadCache`,
186+
// `loadBlob`, `doLoadBlob`, `prefetchResourcesForBrowser` etc., see below.
184187
const fileLoader = (function() {
185188
class Loader {
186189
constructor() {
187190
this.requests = new Map;
188191
}
189192

190-
async _loadInternal(url) {
191-
if (!isInBrowser)
192-
return Promise.resolve(readFile(url));
193+
// Cache / memoize previously read files, because some workloads
194+
// share common code.
195+
load(url) {
196+
assert(!isInBrowser);
193197

194-
let response;
195-
const tries = 3;
196-
while (tries--) {
197-
let hasError = false;
198-
try {
199-
response = await fetch(url);
200-
} catch (e) {
201-
hasError = true;
202-
}
203-
if (!hasError && response.ok)
204-
break;
205-
if (tries)
206-
continue;
207-
globalThis.allIsGood = false;
208-
throw new Error("Fetch failed");
209-
}
210-
if (url.indexOf(".js") !== -1)
211-
return response.text();
212-
else if (url.indexOf(".wasm") !== -1)
213-
return response.arrayBuffer();
214-
215-
throw new Error("should not be reached!");
216-
}
217-
218-
async load(url) {
219-
if (this.requests.has(url))
198+
if (this.requests.has(url)) {
220199
return this.requests.get(url);
200+
}
221201

222-
const promise = this._loadInternal(url);
223-
this.requests.set(url, promise);
224-
return promise;
202+
const contents = readFile(url);
203+
this.requests.set(url, contents);
204+
return contents;
225205
}
226206
}
227207
return new Loader;
@@ -233,6 +213,8 @@ class Driver {
233213
this.isDone = false;
234214
this.errors = [];
235215
this.benchmarks = [];
216+
// TODO: Cleanup / remove / merge `blobDataCache` and `loadCache` vs.
217+
// the global `fileLoader` cache.
236218
this.blobDataCache = { };
237219
this.loadCache = { };
238220
this.counter = { };
@@ -241,9 +223,10 @@ class Driver {
241223
this.counter.failedPreloadResources = 0;
242224
}
243225

226+
// TODO: Remove, make `this.benchmarks` immutable and set it once in the
227+
// ctor instead of this and the global `addBenchmarksBy*` functions.
244228
addBenchmark(benchmark) {
245229
this.benchmarks.push(benchmark);
246-
benchmark.fetchResources();
247230
}
248231

249232
async start() {
@@ -336,8 +319,7 @@ class Driver {
336319
}
337320
}
338321

339-
runCode(string)
340-
{
322+
runCode(string) {
341323
if (!isInBrowser) {
342324
const scripts = string;
343325
let globalObject;
@@ -387,8 +369,7 @@ class Driver {
387369
return magicFrame;
388370
}
389371

390-
prepareToRun()
391-
{
372+
prepareToRun() {
392373
this.benchmarks.sort((a, b) => a.plan.name.toLowerCase() < b.plan.name.toLowerCase() ? 1 : -1);
393374

394375
let text = "";
@@ -433,8 +414,7 @@ class Driver {
433414
});
434415
}
435416

436-
reportError(benchmark, error)
437-
{
417+
reportError(benchmark, error) {
438418
this.pushError(benchmark.name, error);
439419

440420
if (!isInBrowser)
@@ -459,8 +439,7 @@ class Driver {
459439
async initialize() {
460440
if (isInBrowser)
461441
window.addEventListener("error", (e) => this.pushError("driver startup", e.error));
462-
await this.prefetchResourcesForBrowser();
463-
await this.fetchResources();
442+
await this.prefetchResources();
464443
this.prepareToRun();
465444
this.isReady = true;
466445
if (isInBrowser) {
@@ -471,14 +450,18 @@ class Driver {
471450
}
472451
}
473452

474-
async prefetchResourcesForBrowser() {
475-
if (!isInBrowser)
453+
async prefetchResources() {
454+
if (!isInBrowser) {
455+
for (const benchmark of this.benchmarks)
456+
benchmark.prefetchResourcesForShell();
476457
return;
458+
}
477459

460+
// TODO: Cleanup the browser path of the preloading below and in
461+
// `prefetchResourcesForBrowser` / `retryPrefetchResourcesForBrowser`.
478462
const promises = [];
479463
for (const benchmark of this.benchmarks)
480464
promises.push(benchmark.prefetchResourcesForBrowser());
481-
482465
await Promise.all(promises);
483466

484467
const counter = JetStream.counter;
@@ -498,16 +481,6 @@ class Driver {
498481
}
499482

500483
JetStream.loadCache = { }; // Done preloading all the files.
501-
}
502-
503-
async fetchResources() {
504-
const promises = [];
505-
for (const benchmark of this.benchmarks)
506-
promises.push(benchmark.fetchResources());
507-
await Promise.all(promises);
508-
509-
if (!isInBrowser)
510-
return;
511484

512485
const statusElement = document.getElementById("status");
513486
statusElement.classList.remove('loading');
@@ -623,7 +596,7 @@ class Benchmark {
623596
this.isAsync = !!plan.isAsync;
624597
this.disabledByDefault = !!plan.disabledByDefault;
625598
this.scripts = null;
626-
this._resourcesPromise = null;
599+
this.preloads = null;
627600
this._state = BenchmarkState.READY;
628601
}
629602

@@ -880,8 +853,8 @@ class Benchmark {
880853
}
881854

882855
prefetchResourcesForBrowser() {
883-
if (!isInBrowser)
884-
return;
856+
assert(isInBrowser);
857+
885858
const promises = this.plan.files.map((file) => this.loadBlob("file", null, file).then((blobData) => {
886859
if (!globalThis.allIsGood)
887860
return;
@@ -913,6 +886,8 @@ class Benchmark {
913886
}
914887

915888
async retryPrefetchResource(type, prop, file) {
889+
assert(isInBrowser);
890+
916891
const counter = JetStream.counter;
917892
const blobData = JetStream.blobDataCache[file];
918893
if (blobData.blob) {
@@ -947,8 +922,7 @@ class Benchmark {
947922
}
948923

949924
async retryPrefetchResourcesForBrowser() {
950-
if (!isInBrowser)
951-
return;
925+
assert(isInBrowser);
952926

953927
const counter = JetStream.counter;
954928
for (const resource of this.plan.files) {
@@ -968,33 +942,14 @@ class Benchmark {
968942
return !counter.failedPreloadResources && counter.loadedResources == counter.totalResources;
969943
}
970944

971-
fetchResources() {
972-
if (this._resourcesPromise)
973-
return this._resourcesPromise;
945+
prefetchResourcesForShell() {
946+
assert(!isInBrowser);
974947

975-
this.preloads = [];
976-
977-
if (isInBrowser) {
978-
this._resourcesPromise = Promise.resolve();
979-
return this._resourcesPromise;
980-
}
981-
982-
const filePromises = this.plan.files.map((file) => fileLoader.load(file));
983-
this._resourcesPromise = Promise.all(filePromises).then((texts) => {
984-
if (isInBrowser)
985-
return;
986-
this.scripts = [];
987-
assert(texts.length === this.plan.files.length);
988-
for (const text of texts)
989-
this.scripts.push(text);
990-
});
991-
992-
if (this.plan.preload) {
993-
for (const prop of Object.getOwnPropertyNames(this.plan.preload))
994-
this.preloads.push([ prop, this.plan.preload[prop] ]);
995-
}
948+
assert(this.scripts === null, "This initialization should be called only once.");
949+
this.scripts = this.plan.files.map(file => fileLoader.load(file));
996950

997-
return this._resourcesPromise;
951+
assert(this.preloads === null, "This initialization should be called only once.");
952+
this.preloads = Object.entries(this.plan.preload ?? {});
998953
}
999954

1000955
scoreIdentifiers() { throw new Error("Must be implemented by subclasses"); }

0 commit comments

Comments
 (0)