Skip to content

WebAssembly module: all problem types, sessions and a Web Worker - #633

Merged
fontanf merged 1 commit into
masterfrom
wasm-all-types-worker
Oct 3, 2026
Merged

fontanf merged 1 commit into
masterfrom
wasm-all-types-worker

Conversation

@fontanf

@fontanf fontanf commented Oct 3, 2026

Copy link
Copy Markdown
Owner

Stacked on #632: this PR's diff only shows the second step. Once #632 is merged, GitHub retargets it to master.

Changes

All problem types: solve and Session accept rectangleguillotine, rectangle, box, boxstacks, onedimensional and irregular, through one generic template over the six namespaces.

Session: solving without blocking.

const session = new module.Session();
session.start("rectangle", instanceJson, JSON.stringify({optimization_mode: "anytime"}));
// Then, periodically:
const {solutions, done, result} = JSON.parse(session.poll());
// ...
session.stop();    // The optimization ends with its best solution.
session.delete();
  • start runs the optimization in a thread of its own and returns immediately.
  • poll returns the updates since the previous call. There's an update each time the best solution or a bound improves, so an update may have no new solution, e.g. the bound found before any solution. At the end, poll also returns the result.
  • stop stops the optimization. With timer.add_end_boolean, as in the Python bindings.
  • Updates are produced on the algorithms' threads, which can't call JavaScript functions owned by the thread holding the session. So they're queued under a mutex and returned by poll. The calling thread never blocks.

Web Worker: wasm/js/packingsolver_worker.js runs a session and posts the updates to the page.

Direction Messages
to the worker {type: "solve", problemType, instance, parameters}, {type: "stop"}
from the worker {type: "ready"}, {type: "solution", output, certificate}, {type: "done", output, certificate}, {type: "error", error}

It's a classic worker in the browser. It also runs in Node with worker_threads, which is how it's tested.

Size: with the six types, the module is 7.4 MB, 2.4 MB gzipped.

Testing

node --test wasm/tests/: 31 tests, all passing in three consecutive runs. None uses a time limit.

  • solve, each problem type in not-anytime and not-anytime-sequential modes. Also the rectangle tests of Add a WebAssembly module #632: anytime mode on an instance proven optimal, the README example in the three non-anytime modes, and the error cases.
  • Session:
    • anytime mode on an instance proven optimal;
    • each problem type in anytime mode, stopped at the first solution;
    • stopping the README example, whose objective has no bound, so it only ends when stopped;
    • errors, and starting a session twice.
  • Web Worker: updates, stop and result; errors.

Not tested in a browser yet: that needs a page served with the headers enabling SharedArrayBuffer, the next step.

Base automatically changed from wasm-prototype to master October 3, 2026 19:16
- The module solves every problem type: rectangleguillotine, rectangle,
  box, boxstacks, onedimensional and irregular.
- 'Session' solves an instance without blocking: 'start' runs the
  optimization in a thread of its own, 'poll' returns the updates (each
  time the best solution or a bound improves) since the previous call and,
  at the end, the result, and 'stop' stops the optimization. The updates
  are queued by the threads of the algorithms, which can't call the
  JavaScript functions of the thread owning the session.
- 'wasm/js/packingsolver_worker.js' runs sessions in a Web Worker and
  posts the updates and the result to the page; it also runs in Node with
  'worker_threads'.
@fontanf
fontanf force-pushed the wasm-all-types-worker branch from 31eed86 to 81983ec Compare October 3, 2026 19:17
@fontanf
fontanf merged commit 555cb45 into master Oct 3, 2026
0 of 3 checks passed
@fontanf
fontanf deleted the wasm-all-types-worker branch October 3, 2026 19:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant