From 069a599eaddc42b980a87f023ead22c76d31d1bd Mon Sep 17 00:00:00 2001 From: Stewart Allen Date: Thu, 17 Jun 2021 23:31:05 -0400 Subject: [PATCH] memory optimization: defer wasm load, reduce point/poly default fields --- src/geo/point.js | 12 ++++++------ src/geo/polygon.js | 6 +++--- src/mode/cam/animate.js | 2 +- src/mode/sla/driver.js | 21 +++++++++++++++++++++ src/mode/sla/export.js | 23 +---------------------- 5 files changed, 32 insertions(+), 32 deletions(-) diff --git a/src/geo/point.js b/src/geo/point.js index 5da90311..4bd41b84 100644 --- a/src/geo/point.js +++ b/src/geo/point.js @@ -42,13 +42,13 @@ this.key = key || [x, y, z].toString(); } this.poly = null; // parent polygon - this.dist = 0.0; // for group intersection sorting and tests - this.p1 = null; // used in sliceIntersect(), connectLines() and intersect() - this.p2 = null; // used in sliceIntersect(), connectLines() and intersect() + // this.dist = 0.0; // for group intersection sorting and tests + // this.p1 = null; // used in sliceIntersect(), connectLines() and intersect() + // this.p2 = null; // used in sliceIntersect(), connectLines() and intersect() this.pos = 0; // position in group - this.mod = 0; // group length (for modulus of pos) - this.del = false; // for culling - this.group = null; // for grouping in slice intersect, offset lines in trace + // this.mod = 0; // group length (for modulus of pos) + // this.del = false; // for culling + // this.group = null; // for grouping in slice intersect, offset lines in trace } /** ****************************************************************** diff --git a/src/geo/polygon.js b/src/geo/polygon.js index 30581f60..15cfa007 100644 --- a/src/geo/polygon.js +++ b/src/geo/polygon.js @@ -43,12 +43,12 @@ this.area2 = 0.0; // computed as 2x area (sign = direction) this.perim = 0.0; // for caching the result this.bounds = new Bounds(); - this.inner = null; // array of enclosed polygons (if any) + // this.inner = null; // array of enclosed polygons (if any) this.parent = null; // enclosing parent polygon this.depth = 0; // depth nested from top parent (density for support fill) this.delete = false; // for culling during tracing - this.fillang = null; // hinted fill angle - this.fill = null; // fill lines (only used by supports currently) + // this.fillang = null; // hinted fill angle + // this.fill = null; // fill lines (only used by supports currently) if (points) this.addPoints(points); } diff --git a/src/mode/cam/animate.js b/src/mode/cam/animate.js index 5728aaf0..80b629c7 100644 --- a/src/mode/cam/animate.js +++ b/src/mode/cam/animate.js @@ -496,7 +496,7 @@ self.kiri.loader.push(function() { } // load renderer code in worker context only - if (KIRI.worker) + if (KIRI.worker && false) fetch('/wasm/kiri-ani.wasm') .then(response => response.arrayBuffer()) .then(bytes => WebAssembly.instantiate(bytes, { diff --git a/src/mode/sla/driver.js b/src/mode/sla/driver.js index f650307b..5e8e17a7 100644 --- a/src/mode/sla/driver.js +++ b/src/mode/sla/driver.js @@ -22,6 +22,27 @@ // runs in worker. would usually be in src/mode/sla/prepare.js function prepare(widgets, settings, update) { self.worker.print = KIRI.newPrint(settings, widgets); + if (!SLA.wasm) { + fetch('/wasm/kiri-sla.wasm') + .then(response => response.arrayBuffer()) + .then(bytes => WebAssembly.instantiate(bytes, { + env: { + reportf: (a,b) => { console.log('[f]',a,b) }, + reporti: (a,b) => { console.log('[i]',a,b) } + } + })) + .then(results => { + let {module, instance} = results; + let {exports} = instance; + let heap = new Uint8Array(exports.memory.buffer); + SLA.wasm = { + heap, + memory: exports.memory, + render: exports.render, + rle_encode: exports.rle_encode + }; + }); + } update(1); } diff --git a/src/mode/sla/export.js b/src/mode/sla/export.js index c8974928..2c31462d 100644 --- a/src/mode/sla/export.js +++ b/src/mode/sla/export.js @@ -475,28 +475,6 @@ // rle_encode: exports.rle_encode // }; // } else - // load renderer code in worker context only - let wasm; - - fetch('/wasm/kiri-sla.wasm') - .then(response => response.arrayBuffer()) - .then(bytes => WebAssembly.instantiate(bytes, { - env: { - reportf: (a,b) => { console.log('[f]',a,b) }, - reporti: (a,b) => { console.log('[i]',a,b) } - } - })) - .then(results => { - let {module, instance} = results; - let {exports} = instance; - let heap = new Uint8Array(exports.memory.buffer); - wasm = { - heap, - memory: exports.memory, - render: exports.render, - rle_encode: exports.rle_encode - }; - }); // new WebAssembly rasterizer function renderLayerWasm(params) { @@ -561,6 +539,7 @@ } }); + let wasm = SLA.wasm; let imagelen = width * height; let writer = new self.DataWriter(new DataView(wasm.memory.buffer), imagelen); writer.writeU16(width, true);