From 5e76eff26d1f937080e6313b615e8fbdcaaae9ec Mon Sep 17 00:00:00 2001 From: qazal <77887910+Qazalin@users.noreply.github.com> Date: Tue, 9 Sep 2025 15:56:39 +0300 Subject: [PATCH] viz: pre fetch workers (#12083) * viz: pre fetch workers * move check --- tinygrad/viz/js/index.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tinygrad/viz/js/index.js b/tinygrad/viz/js/index.js index 5ec0b573e8..9a13ba377b 100644 --- a/tinygrad/viz/js/index.js +++ b/tinygrad/viz/js/index.js @@ -48,17 +48,16 @@ function addTags(root) { } let [workerUrl, worker] = [null, null]; +async function initWorker() { + const resp = await Promise.all(["/assets/dagrejs.github.io/project/dagre/latest/dagre.min.js","/js/worker.js"].map(u => fetch(u))); + workerUrl = URL.createObjectURL(new Blob([(await Promise.all(resp.map((r) => r.text()))).join("\n")], { type: "application/javascript" })); +} + async function renderDag(graph, additions, recenter=false) { // start calculating the new layout (non-blocking) updateProgress({ start:true }); - if (worker == null) { - const resp = await Promise.all(["/assets/dagrejs.github.io/project/dagre/latest/dagre.min.js","/js/worker.js"].map(u => fetch(u))); - workerUrl = URL.createObjectURL(new Blob([(await Promise.all(resp.map((r) => r.text()))).join("\n")], { type: "application/javascript" })); - worker = new Worker(workerUrl); - } else { - worker.terminate(); - worker = new Worker(workerUrl); - } + if (worker != null) worker.terminate(); + worker = new Worker(workerUrl); worker.postMessage({graph, additions}); worker.onmessage = (e) => { displayGraph("graph"); @@ -585,6 +584,7 @@ async function main() { else if (e.readyState === EventSource.OPEN) activeSrc = e; } if (ctx.name === "Profiler") return renderProfiler(); + if (workerUrl == null) await initWorker(); if (ckey in cache) { ret = cache[ckey]; }