Add a WebAssembly module - #632
Merged
Merged
Conversation
New 'PACKINGSOLVER_BUILD_WASM' CMake option, to build the libraries with
Emscripten ('emcmake cmake -DPACKINGSOLVER_BUILD_WASM=ON'), with the
'wasm/' module 'packingsolver.js' / 'packingsolver.wasm'. Its JavaScript
API, 'solve(problemType, instanceJson, parametersJson)', reads an instance
in the JSON format, solves it and returns the output and the solution
certificate as a JSON string, or an error.
- Every optimization mode is available, with 'anytime' by default as in
the library. The module is built with thread support: the threads run
in a pool of Web Workers started when the module is loaded. 'solve'
blocks the calling thread, so in a browser it must be called from a Web
Worker.
- It uses mimalloc: with the default allocator, which has a single lock,
the multi-threaded modes were up to 7 times slower than natively.
- It uses HiGHS (CLP isn't available) and enables C++ exceptions. Only the
bindings are compiled as C++17 (required by embind).
- For now, only the 'rectangle' problem type is available.
Tests: 'node --test wasm/tests/'.
This was referenced Oct 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
First step towards a website where users can solve their own instances. This PR builds PackingSolver to WebAssembly and adds a minimal JavaScript API. The web worker, the visualization, the input forms and the deployment will come in follow-up PRs.
Build
emcmake cmake -S . -B build_wasm -DCMAKE_BUILD_TYPE=Release -DPACKINGSOLVER_BUILD_WASM=ON cmake --build build_wasm --target PackingSolver_wasm node --test wasm/tests/PACKINGSOLVER_BUILD_WASMrequires the Emscripten toolchain. It disables CLP, whose prebuilt archives can't be linked, and builds neither the CLI nor the C++ tests. Every dependency, HiGHS included, compiles with Emscripten unchanged. Only the bindings are compiled as C++17, which embind requires; the libraries stay C++14.API
InstanceBuilder::read(std::istream&)(Read JSON instances from a stream #631).{"error": ...}. Through embind, C++ exceptions would only reach JavaScript as opaque pointers.rectangleproblem type is available.Threads
Every optimization mode works, with
anytimeby default as in the library:solveblocks the calling thread, which then can't start new workers. If the algorithms needed more threads than the pool has, the thread creation would fail andsolvewould return an error rather than hang. This path isn't tested: on the instances below, the rectangle solver uses at most 8 threads natively.solvemust be called from a Web Worker, since the main thread can't block, and the page needs the two headers that enableSharedArrayBuffer.Performance
Node 18, the same JSON instances as the native CLI. The 107-item instance has random items.
Every mode found the same solutions as natively. The module is 4.8 MB of WebAssembly, 1.6 MB gzipped, plus a 97 KB JavaScript loader. It loads in about 0.2 s, including the worker pool.
Testing
node --test wasm/tests/: 7 tests, all passing.CI doesn't build the module yet; that will come with the deployment workflow.