partforge 0.165.1 → 0.166.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -107,7 +107,7 @@ relying on consumption should gate it with a `verify` volume assertion.
107
107
  OCCT/replicad backend is the reference.
108
108
 
109
109
  **Optional ops.** `KERNEL_OPTIONAL_OPS` (`beginSubPart`/`endSubPart`/`sweepCache`/`dropCache`/
110
- `cacheStats`/`resetCacheStats`/`cleanup`) and `SOLID_OPTIONAL_OPS` (`genus`/`isEmpty`) may
110
+ `setCacheBudget`/`trimCache`/`cacheBytes`/`cacheStats`/`resetCacheStats`/`cleanup`) and `SOLID_OPTIONAL_OPS` (`genus`/`isEmpty`) may
111
111
  be omitted entirely; callers in the framework guard with `?.`/`typeof`. A host that omits
112
112
  them loses sub-part caching and mesh-topology gates (`holes`, emptiness), nothing else.
113
113
  `sweepCache()` is the cache's rebind hygiene hook: called once when a worker is rebound to
@@ -117,6 +117,13 @@ once, under the same never-mid-bracket rule: the worker calls it on each booted
117
117
  before an export job, because a display build's retained solids and the export's own
118
118
  build share one WASM heap, and on a very large part the cache alone can leave too little
119
119
  heap for the export. The next display build after an export is cold.
120
+ `setCacheBudget(bytes)` bounds the bytes the cache's retained solids may hold (the
121
+ Manifold backend estimates each from its triangle and vertex counts); `trimCache()`
122
+ then drops whole partitions, least recently built first, until they fit. A trim frees
123
+ solids, so it runs only where no caller holds one — never inside a bracket and never
124
+ right after one whose solids are still in use; jobs.js trims after each display
125
+ sub-part is meshed and when a job returns. The budget arrives on `generate` messages
126
+ as `cacheBudget` (bytes, `null` = unbounded), from mount's `cacheBudgetMB` option.
120
127
 
121
128
  `beginSubPart`/`endSubPart` brackets MAY nest: only the outermost pair opens and
122
129
  commits a round, and an inner pair is a balanced no-op. Nesting is real rather than
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "partforge",
3
- "version": "0.165.1",
3
+ "version": "0.166.0",
4
4
  "description": "Turn a declarative part definition into a parametric-CAD web app (three.js + Manifold/Replicad). Requires a Vite-based consumer.",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -36,7 +36,7 @@ export const KERNEL_OPS = [
36
36
  // OCCT's replicad shapes need no dispose bookkeeping). jobs.js calls all of these via
37
37
  // `?.`, so a third-party backend may simply omit them.
38
38
  export const KERNEL_OPTIONAL_OPS = [
39
- "beginSubPart", "endSubPart", "sweepCache", "dropCache", "cacheStats", "resetCacheStats", "cleanup",
39
+ "beginSubPart", "endSubPart", "sweepCache", "dropCache", "setCacheBudget", "trimCache", "cacheBytes", "cacheStats", "resetCacheStats", "cleanup",
40
40
  // Drains the feature-skip warnings recorded since the last drain — a fillet or
41
41
  // chamfer the geometry defeated and the backend skipped rather than failed the
42
42
  // build over. Both backends implement it; a host that never calls it sees the
@@ -171,6 +171,9 @@ export const ROUTED_CAD_OPS = ["shell"];
171
171
  * @property {() => void} [endSubPart] close the cache round (always pair with beginSubPart)
172
172
  * @property {() => void} [sweepCache] drop cache partitions idle for 3 rebinds; call once per setPart, never mid-bracket
173
173
  * @property {() => void} [dropCache] drop EVERY cache partition (the worker calls it before an export); never mid-bracket
174
+ * @property {(bytes:number) => void} [setCacheBudget] bound the solid cache's retained bytes (Infinity = unbounded)
175
+ * @property {() => number} [trimCache] drop least recently built partitions until the cache fits its budget; only where no cached solid is held
176
+ * @property {() => number} [cacheBytes] the cache's estimated retained bytes
174
177
  * @property {() => {hits:number,misses:number}} [cacheStats]
175
178
  * @property {() => void} [resetCacheStats]
176
179
  * @property {() => void} [cleanup] free per-job WASM objects (Manifold backend); call after each job
@@ -61,6 +61,8 @@ function axisAngleMat4(axis, deg) {
61
61
  return [R00, R10, R20, 0, R01, R11, R21, 0, R02, R12, R22, 0, 0, 0, 0, 1];
62
62
  }
63
63
 
64
+ const BYTES_PER_TRI = 160, BYTES_PER_VERT = 24;
65
+
64
66
  export function createManifoldKernel(wasm, { quality = "preview" } = {}) {
65
67
  const { Manifold, CrossSection } = wasm;
66
68
  const segs = SEGS[quality], tube = TUBE[quality];
@@ -128,7 +130,18 @@ export function createManifoldKernel(wasm, { quality = "preview" } = {}) {
128
130
  };
129
131
  const judgedUnion = (ms) => judged("union", ms, unionRaw(ms));
130
132
 
131
- const cache = createSolidCache();
133
+ // A retained solid's heap, estimated from its counts (solid-cache.js MEMORY BUDGET):
134
+ // Manifold keeps per triangle three half-edges, a face normal, a triangle reference and
135
+ // its share of the collider, and per vertex a position and a normal.
136
+ // CrossSections (memoized 2-D profiles) are small: a few dozen bytes per vertex.
137
+ // Measured against the heap a 105-plate forge's cache held (feedback #212).
138
+ const cache = createSolidCache({
139
+ sizeOf: (pin) => {
140
+ if (typeof pin?.numTri === "function") return pin.numTri() * BYTES_PER_TRI + pin.numVert() * BYTES_PER_VERT;
141
+ if (typeof pin?.numVert === "function") return pin.numVert() * 32;
142
+ return 0;
143
+ },
144
+ });
132
145
  // Feature-skip warnings (the OCCT backend's safeOp policy, adopted here): a
133
146
  // fillet/chamfer whose mesh machinery is defeated by the geometry returns its
134
147
  // INPUT solid unchanged and records one message here instead of failing the
@@ -1064,6 +1077,9 @@ export function createManifoldKernel(wasm, { quality = "preview" } = {}) {
1064
1077
  endSubPart: () => cache.end(),
1065
1078
  sweepCache: () => cache.sweep(),
1066
1079
  dropCache: () => cache.clear(),
1080
+ setCacheBudget: (bytes) => cache.setBudget(bytes),
1081
+ trimCache: () => cache.trim(),
1082
+ cacheBytes: () => cache.retainedBytes(),
1067
1083
  cacheStats: () => cache.stats(),
1068
1084
  resetCacheStats: () => cache.resetStats(),
1069
1085
  // Drain the feature-skip warnings recorded since the last drain (see
@@ -7,7 +7,21 @@
7
7
  // entry is retained by BOTH partitions, so disposal is refcounted — see release().
8
8
  // WASM-agnostic — it stores opaque {value, pin, dispose} triples supplied by the
9
9
  // caller (the Manifold backend), so it is unit-testable with plain objects.
10
- export function createSolidCache() {
10
+ //
11
+ // MEMORY BUDGET (feedback #212). Retaining a whole view's build graph is what makes an
12
+ // edit rebuild in seconds, and it is also most of the heap a big view holds: opening a
13
+ // 105-plate forge peaked at 3.3 GB with the cache and 0.95 GB without, which a phone's
14
+ // browser tab cannot hold. `setBudget(bytes)` bounds it; `trim()` then disposes whole
15
+ // partitions, least recently built first, until the retained solids fit. A sub-part
16
+ // trimmed away simply rebuilds cold next time. `sizeOf(pin)` is the caller's estimate
17
+ // of one retained object's bytes (the Manifold backend reads its triangle and vertex
18
+ // counts); it is asked once per entry, at the first trim() after the entry was made.
19
+ //
20
+ // trim() is NOT done inside end(): a caller may still be using a round's solids after
21
+ // closing it (buildView hands its built view to measure), and a trim there would free
22
+ // them under it. jobs.js trims where nothing is held — after each display sub-part has
23
+ // been meshed and cleaned up, and when a job ends.
24
+ export function createSolidCache({ sizeOf = () => 0 } = {}) {
11
25
  const caches = new Map(); // name -> Map(hash -> { value, pin, dispose, refs })
12
26
  const pinned = new Set(); // every live `pin` across all sub-parts
13
27
  const index = new Map(); // hash -> entry, ACROSS partitions: identical geometry built
@@ -17,6 +31,11 @@ export function createSolidCache() {
17
31
  let name = null, active = null, prev = null;
18
32
  let depth = 0; // bracket nesting; only the OUTERMOST one is real (see begin)
19
33
  let hits = 0, misses = 0;
34
+ let budget = Infinity; // bytes the retained solids may hold (setBudget)
35
+ let bytes = 0; // the retained solids' estimated size, each entry once
36
+ let tick = 0; // begin() order, for least-recently-built eviction
37
+ const lastUsed = new Map(); // name -> tick of the partition's last begin()
38
+ const unsized = new Set(); // entries made since the last trim(), size not yet read
20
39
 
21
40
  // One partition stops retaining `entry`. Disposal waits for the LAST holder:
22
41
  // a solid shared across sub-parts is one WASM object, so disposing it when the
@@ -27,8 +46,16 @@ export function createSolidCache() {
27
46
  if (--entry.refs > 0) return;
28
47
  if (index.get(hash) === entry) index.delete(hash);
29
48
  pinned.delete(entry.pin);
49
+ unsized.delete(entry);
50
+ bytes -= entry.size ?? 0;
30
51
  entry.dispose();
31
52
  };
53
+ const dropPartition = (n) => {
54
+ for (const [hash, entry] of caches.get(n) ?? []) release(hash, entry); // refcounted: a shared solid survives
55
+ caches.delete(n);
56
+ lastBuilt.delete(n);
57
+ lastUsed.delete(n);
58
+ };
32
59
 
33
60
  return {
34
61
  // Nested brackets collapse into the outermost one. There is a single open
@@ -39,7 +66,7 @@ export function createSolidCache() {
39
66
  // contain one, and the inner pair becomes a no-op.
40
67
  begin(n) {
41
68
  if (depth++ > 0) return;
42
- name = n; lastBuilt.set(n, generation); prev = caches.get(n) ?? new Map(); active = new Map();
69
+ name = n; lastBuilt.set(n, generation); lastUsed.set(n, ++tick); prev = caches.get(n) ?? new Map(); active = new Map();
43
70
  },
44
71
 
45
72
  end() {
@@ -58,11 +85,9 @@ export function createSolidCache() {
58
85
  // the eviction line, so recently-viewed views stay warm across edits.
59
86
  sweep() {
60
87
  generation++;
61
- for (const [n, entries] of caches) {
88
+ for (const n of [...caches.keys()]) {
62
89
  if (generation - (lastBuilt.get(n) ?? 0) < 3) continue;
63
- for (const [hash, entry] of entries) release(hash, entry); // refcounted: a shared solid survives
64
- caches.delete(n);
65
- lastBuilt.delete(n);
90
+ dropPartition(n);
66
91
  }
67
92
  },
68
93
 
@@ -78,6 +103,34 @@ export function createSolidCache() {
78
103
  }
79
104
  caches.clear();
80
105
  lastBuilt.clear();
106
+ lastUsed.clear();
107
+ },
108
+
109
+ // Bytes the retained solids may hold; Infinity (the default) never trims.
110
+ setBudget(b) {
111
+ budget = typeof b === "number" && b >= 0 ? b : Infinity;
112
+ },
113
+
114
+ // Dispose whole partitions, least recently built first, until the retained solids
115
+ // fit the budget. Never mid-bracket, and only where no caller holds a cached solid
116
+ // (see the header). Returns how many partitions it dropped.
117
+ trim() {
118
+ if (name != null) return 0;
119
+ for (const entry of unsized) {
120
+ let s = 0;
121
+ try { s = Math.max(0, Number(sizeOf(entry.pin)) || 0); } catch { s = 0; }
122
+ entry.size = s;
123
+ bytes += s;
124
+ }
125
+ unsized.clear();
126
+ let dropped = 0;
127
+ while (bytes > budget && lastUsed.size > 0) {
128
+ let oldest = null, at = Infinity;
129
+ for (const [n, t] of lastUsed) if (t < at) { at = t; oldest = n; }
130
+ dropPartition(oldest);
131
+ dropped++;
132
+ }
133
+ return dropped;
81
134
  },
82
135
 
83
136
  lookup(hash, make) {
@@ -90,6 +143,7 @@ export function createSolidCache() {
90
143
  misses++;
91
144
  const entry = make();
92
145
  entry.refs = 1;
146
+ unsized.add(entry);
93
147
  active.set(hash, entry);
94
148
  index.set(hash, entry);
95
149
  pinned.add(entry.pin);
@@ -98,6 +152,7 @@ export function createSolidCache() {
98
152
 
99
153
  isPinned: (pin) => pinned.has(pin),
100
154
  stats: () => ({ hits, misses }),
155
+ retainedBytes: () => bytes,
101
156
  resetStats: () => { hits = 0; misses = 0; },
102
157
  };
103
158
  }
@@ -65,7 +65,7 @@ const loadBedLayout = () => import("./export/bed-layout.js");
65
65
 
66
66
  // Handle one geometry job, posting results/progress via `post(msg, transfer?)`.
67
67
  // Backend-agnostic and part-agnostic: every part specific comes through `part`.
68
- // { type:"generate", subparts, view, params } → { type:"meshes", meshes, ms }
68
+ // { type:"generate", subparts, view, params, cacheBudget? } → { type:"meshes", meshes, ms }
69
69
  // { type:"export-stl", view, params } → { type:"download-parts", ext, mime, parts }
70
70
  // { type:"export-step", view, params } → { type:"download", data, filename, mime }
71
71
  // { type:"export-bundle", view, params, parts, options }
@@ -462,6 +462,9 @@ export async function handle(kernel, part, msg, post, opts = {}) {
462
462
  kernel.takeBuildWarnings?.();
463
463
  const warnings = [...jobWarnings]; // job-level notices ride along with the per-sub-part ones
464
464
  kernel.resetCacheStats?.(); // count hits/misses for just this job
465
+ // The host's bound on the solid cache (mount's cacheBudgetMB), in bytes; null
466
+ // means unbounded. Kept by the kernel, so the oracle's builds share it.
467
+ if (Object.hasOwn(msg, "cacheBudget")) kernel.setCacheBudget?.(msg.cacheBudget ?? Infinity);
465
468
  for (const [i, name] of msg.subparts.entries()) {
466
469
  if (useCache) kernel.beginSubPart?.(name); // open the per-sub-part cache round
467
470
  try {
@@ -472,6 +475,7 @@ export async function handle(kernel, part, msg, post, opts = {}) {
472
475
  for (const message of kernel.takeBuildWarnings?.() ?? []) warnings.push({ part: name, message });
473
476
  if (useCache) kernel.endSubPart?.(); // always close the bracket — a throw mid-build must not strand pinned solids
474
477
  kernel.cleanup?.(); // free this round's transients (cached/pinned solids survive)
478
+ kernel.trimCache?.(); // over the host's budget: drop the least recently built sub-parts' solids
475
479
  }
476
480
  // Cooperative cancel: yield one macrotask so queued messages (a newer
477
481
  // generate, a part rebind) can be seen, then stop at this boundary if
@@ -705,5 +709,6 @@ export async function handle(kernel, part, msg, post, opts = {}) {
705
709
  else post({ type: "error", message: String(err?.message || err), jobId: msg.jobId });
706
710
  } finally {
707
711
  kernel.cleanup?.();
712
+ kernel.trimCache?.(); // a job holds no cached solid once it has returned (solid-cache.js MEMORY BUDGET)
708
713
  }
709
714
  }
@@ -286,7 +286,8 @@ function createCleanupStack() {
286
286
  // mesh-validity cache, and the geometry workers. The app supplies `createWorker(name)`
287
287
  // so Vite can bundle the worker (see geometry-service.js).
288
288
  //
289
- // Embedding contract (0.158.0: a preview GLB may carry its view's autoplay
289
+ // Embedding contract (0.166.0: `cacheBudgetMB` bounds the worker's solid cache —
290
+ // pass a small one on phones; 0.158.0: a preview GLB may carry its view's autoplay
290
291
  // animation — mount({ preview }) draws its opening pose and does not play it,
291
292
  // mountPreview plays it unless `animate: false`, and exportPreview bakes it only
292
293
  // when every track is place-only; 0.147.0: a cached `preview` GLB is drawn before
@@ -515,6 +516,7 @@ export function mount(part, { createWorker, elements = {}, onBuild, onPick, pick
515
516
  files, panelState,
516
517
  annotateSend = "viewbar",
517
518
  selectionPopover,
519
+ cacheBudgetMB,
518
520
  container: legacyContainer, controls: legacyControls } = {}) {
519
521
  // --- element resolution (the only getElementById calls in the framework, save the ?pickserver client's optional #viewbar lookup) ----
520
522
  const byId = (id) => document.getElementById(id);
@@ -641,6 +643,10 @@ export function mount(part, { createWorker, elements = {}, onBuild, onPick, pick
641
643
  const qs = new URLSearchParams(location.search);
642
644
  const debug = qs.has("debug");
643
645
  let cachingOn = !(debug && qs.has("nocache"));
646
+ // The host's bound on the worker's solid cache, sent with every generate (null =
647
+ // unbounded). A phone host passes a small one: the cache of a very large view is
648
+ // most of its heap (feedback #212). Anything but a positive finite number is ignored.
649
+ const cacheBudgetBytes = Number.isFinite(cacheBudgetMB) && cacheBudgetMB > 0 ? Math.round(cacheBudgetMB * 2 ** 20) : null;
644
650
  let lastGen = { skipped: 0, rebuilt: 0, posed: 0 }; // Layer-0/1 counts for the most recent generate
645
651
  // Sub-parts the pose fast path has repaired since the last build was dispatched.
646
652
  // A SET of names, not a running total: a slider drag re-repairs the same
@@ -1069,7 +1075,7 @@ export function mount(part, { createWorker, elements = {}, onBuild, onPick, pick
1069
1075
  for (const backend of ["manifold", "occt"]) {
1070
1076
  const subparts = missing.filter((n) => (backends[n] ?? "manifold") === backend);
1071
1077
  if (subparts.length === 0) continue;
1072
- service.send({ type: "generate", subparts, view: view(), params, cache: cachingOn }, backend);
1078
+ service.send({ type: "generate", subparts, view: view(), params, cache: cachingOn, cacheBudget: cacheBudgetBytes }, backend);
1073
1079
  jobs++;
1074
1080
  }
1075
1081
  return jobs;
package/types/index.d.ts CHANGED
@@ -218,6 +218,14 @@ export interface MountOptions {
218
218
  onPick?: (event: PickEvent) => void;
219
219
  /** Opt in to the selection popover (requires `onPick`): a pick opens a popover at the spot with the sub-part's appearance controls, and with `slot: true` an empty region the host floats its own UI over (`runtime.selection`). */
220
220
  selectionPopover?: { slot: boolean };
221
+ /**
222
+ * Bound, in MB, on the geometry worker's solid cache — the built solids it keeps so
223
+ * an edit rebuilds only what changed. Unbounded by default. Past the bound the least
224
+ * recently built sub-parts' solids are freed and those sub-parts rebuild cold on the
225
+ * next edit. Pass a small bound on phones: a very large view's cache can hold
226
+ * gigabytes, more than a mobile browser tab is allowed.
227
+ */
228
+ cacheBudgetMB?: number;
221
229
  /** Receive exported bytes instead of partforge's own DOM download. */
222
230
  onDownload?: (file: DownloadPayload) => void;
223
231
  /** The active view (tab) name — emitted once on mount, then on every change. */
package/types/kernel.d.ts CHANGED
@@ -644,6 +644,12 @@ export interface GeometryKernel {
644
644
  sweepCache?(): void;
645
645
  /** Drop every cache partition — the worker calls this before an export job. Never call mid-bracket. */
646
646
  dropCache?(): void;
647
+ /** Bound the solid cache's retained bytes (`Infinity` = unbounded, the default). */
648
+ setCacheBudget?(bytes: number): void;
649
+ /** Drop the least recently built partitions until the cache fits its budget. Only where no cached solid is held. */
650
+ trimCache?(): number;
651
+ /** The cache's estimated retained bytes. */
652
+ cacheBytes?(): number;
647
653
  cacheStats?(): { hits: number; misses: number };
648
654
  resetCacheStats?(): void;
649
655
  /** Free per-job WASM objects (Manifold backend); call after each job. */