Skip to content

Commit

Permalink
Enable weakrefs in WASM builds
Browse files Browse the repository at this point in the history
This will allow users to not have to explicitly `free()` every single
object from WASM, greatly improving the user experience.

People running very allocation-heavy processe using CML can still use
`free()` to have more control over deallocation.

`cip25` was tested with this with a heavy allocation example from the
browser (running the example in `cip25/wasm/README.md`) many times.
Without it, it would run out of memory after 250,000 itertions
consistently. With it, and with timeouts to allow the GC to kick in it
was able to make it much higher but tended to eventually run out of
memory after 1-2 million. This could be something unavoidable in how the
GC works (fragmentation? or maybe 50k chunks we were using were too big
so it wasn't getting run until it was too late?).

Further testing/investigation might be warranted, but in general if you
are using CML for heavy workloads you might still want to do `free()` on
the objects used there.
  • Loading branch information
rooooooooob committed Feb 8, 2023
1 parent 9331979 commit ba4ce3e
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 5 deletions.
43 changes: 43 additions & 0 deletions chain/wasm/package.json
@@ -0,0 +1,43 @@
{
"name": "cml-chain",
"version": "0.1.0",
"description": "Multiplatform SDK for on-chain Cardano blochain types",
"scripts": {
"rust:build-nodejs": "rimraf ./pkg; cross-env WASM_BINDGEN_WEAKREF=1 wasm-pack build --target=nodejs; npm run js:ts-json-gen; wasm-pack pack",
"rust:build-browser": "rimraf ./pkg; cross-env WASM_BINDGEN_WEAKREF=1 wasm-pack build --target=browser; npm run js:ts-json-gen; wasm-pack pack",
"rust:build-web": "rimraf ./pkg; cross-env WASM_BINDGEN_WEAKREF=1 wasm-pack build --target=web; npm run js:ts-json-gen; wasm-pack pack",
"rust:build-asm": "(rimraf ./rust/pkg && cd rust; wasm-pack build --target=browser -- --features wasm; cd ..; npm run js:ts-json-gen; cd rust; wasm-pack pack) && npm run asm:build && npm run js:flowgen",
"asm:build": "../../binaryen/bin/wasm2js ./rust/pkg/cardano_multiplatform_lib_bg.wasm --output ./rust/pkg/cardano_multiplatform_lib.asm.js && node ./scripts/legacy/wasm-to-asm",
"rust:publish": "cargo publish",
"rust:check-warnings": "(RUSTFLAGS=\"-D warnings\" cargo +stable build)",
"rust:test": "cargo test",
"js:prepublish": "npm run rust:test && rimraf ./publish && cp -r ./pkg ./publish && cp README.md publish/ && cp ../../LICENSE publish/",
"js:test-publish": "npm run rust:build-nodejs && npm run js:prepublish && node ../../scripts/publish-helper chain -nodejs && cd publish",
"js:publish-nodejs:prod": "npm run rust:build-nodejs && npm run js:prepublish && node ../../scripts/publish-helper chain -nodejs && cd publish && npm publish --access public",
"js:publish-nodejs:beta": "npm run rust:build-nodejs && npm run js:prepublish && node ../../scripts/publish-helper chain -nodejs && cd publish && npm publish --tag beta --access public",
"js:publish-browser:prod": "npm run rust:build-browser && npm run js:prepublish && node ../../scripts/publish-helper chain -browser && cd publish && npm publish --access public",
"js:publish-browser:beta": "npm run rust:build-browser && npm run js:prepublish && node ../../scripts/publish-helper chain -browser && cd publish && npm publish --tag beta --access public",
"js:publish-asm:prod": "npm run rust:build-asm && npm run js:prepublish && node ../../scripts/publish-helper chain -asmjs && cd publish && npm publish --access public",
"js:publish-asm:beta": "npm run rust:build-asm && npm run js:prepublish && node ../../scripts/publish-helper chain -asmjs && cd publish && npm publish --tag beta --access public",
"js:ts-json-gen": "cd json-gen && cargo +stable run && cd .. && node ../../scripts/run-json2ts.js && node ../../scripts/json-ts-types.js chain",
"postinstall": "git submodule update --init --recursive && cd binaryen; cmake . && make"
},
"husky": {
"hooks": {
"pre-push": "npm run rust:test && npm run rust:build-nodejs"
}
},
"author": "dcSpark",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/dcSpark/cardano-multiplatform-lib.git"
},
"devDependencies": {
"flowgen": "1.11.0",
"husky": "4.2.5",
"json-schema-to-typescript": "^10.1.5",
"rimraf": "3.0.2",
"cross-env": "^7.0.3"
}
}
11 changes: 6 additions & 5 deletions cip25/wasm/package.json
@@ -1,14 +1,14 @@
{
"name": "cml-cip25",
"version": "3.1.3",
"version": "1.0.0",
"description": "Cardano Multiplatform SDK for CIP25 (Cardano NFT Metadata)",
"keywords": [
"cardano"
],
"scripts": {
"rust:build-nodejs": "rimraf ./pkg; wasm-pack build --target=nodejs; npm run js:ts-json-gen; wasm-pack pack",
"rust:build-browser": "rimraf ./pkg; wasm-pack build --target=browser; npm run js:ts-json-gen; wasm-pack pack",
"rust:build-web": "rimraf ./pkg; wasm-pack build --target=web; npm run js:ts-json-gen; wasm-pack pack",
"rust:build-nodejs": "rimraf ./pkg; cross-env WASM_BINDGEN_WEAKREF=1 wasm-pack build --target=nodejs; npm run js:ts-json-gen; wasm-pack pack",
"rust:build-browser": "rimraf ./pkg; cross-env WASM_BINDGEN_WEAKREF=1 wasm-pack build --target=browser; npm run js:ts-json-gen; wasm-pack pack",
"rust:build-web": "rimraf ./pkg; cross-env WASM_BINDGEN_WEAKREF=1 wasm-pack build --target=web; npm run js:ts-json-gen; wasm-pack pack",
"rust:build-asm": "(rimraf ./rust/pkg && cd rust; wasm-pack build --target=browser -- --features wasm; cd ..; npm run js:ts-json-gen; cd rust; wasm-pack pack) && npm run asm:build && npm run js:flowgen",
"asm:build": "../../binaryen/bin/wasm2js ./rust/pkg/cardano_multiplatform_lib_bg.wasm --output ./rust/pkg/cardano_multiplatform_lib.asm.js && node ./scripts/legacy/wasm-to-asm",
"rust:publish": "cargo publish",
Expand Down Expand Up @@ -39,6 +39,7 @@
"devDependencies": {
"husky": "4.2.5",
"json-schema-to-typescript": "^10.1.5",
"rimraf": "3.0.2"
"rimraf": "3.0.2",
"cross-env": "^7.0.3"
}
}
45 changes: 45 additions & 0 deletions cip36/wasm/package.json
@@ -0,0 +1,45 @@
{
"name": "cml-cip36",
"version": "0.1.0",
"description": "Cardano Multiplatform SDK for CIP36 (Catalyst delegation)",
"keywords": [
"cardano"
],
"scripts": {
"rust:build-nodejs": "rimraf ./pkg; cross-env WASM_BINDGEN_WEAKREF=1 wasm-pack build --target=nodejs; npm run js:ts-json-gen; wasm-pack pack",
"rust:build-browser": "rimraf ./pkg; cross-env WASM_BINDGEN_WEAKREF=1 wasm-pack build --target=browser; npm run js:ts-json-gen; wasm-pack pack",
"rust:build-web": "rimraf ./pkg; cross-env WASM_BINDGEN_WEAKREF=1 wasm-pack build --target=web; npm run js:ts-json-gen; wasm-pack pack",
"rust:build-asm": "(rimraf ./rust/pkg && cd rust; wasm-pack build --target=browser -- --features wasm; cd ..; npm run js:ts-json-gen; cd rust; wasm-pack pack) && npm run asm:build && npm run js:flowgen",
"asm:build": "../../binaryen/bin/wasm2js ./rust/pkg/cardano_multiplatform_lib_bg.wasm --output ./rust/pkg/cardano_multiplatform_lib.asm.js && node ./scripts/legacy/wasm-to-asm",
"rust:publish": "cargo publish",
"rust:check-warnings": "(RUSTFLAGS=\"-D warnings\" cargo +stable build)",
"rust:test": "cargo test",
"js:prepublish": "npm run rust:test && rimraf ./publish && cp -r ./pkg ./publish && cp README.md publish/ && cp ../../LICENSE publish/",
"js:test-publish": "npm run rust:build-nodejs && npm run js:prepublish && node ../../scripts/publish-helper cip36 -nodejs && cd publish",
"js:publish-nodejs:prod": "npm run rust:build-nodejs && npm run js:prepublish && node ../../scripts/publish-helper cip36 -nodejs && cd publish && npm publish --access public",
"js:publish-nodejs:beta": "npm run rust:build-nodejs && npm run js:prepublish && node ../../scripts/publish-helper cip36 -nodejs && cd publish && npm publish --tag beta --access public",
"js:publish-browser:prod": "npm run rust:build-browser && npm run js:prepublish && node ../../scripts/publish-helper cip36 -browser && cd publish && npm publish --access public",
"js:publish-browser:beta": "npm run rust:build-browser && npm run js:prepublish && node ../../scripts/publish-helper cip36 -browser && cd publish && npm publish --tag beta --access public",
"js:publish-asm:prod": "npm run rust:build-asm && npm run js:prepublish && node ../../scripts/publish-helper cip36 -asmjs && cd publish && npm publish --access public",
"js:publish-asm:beta": "npm run rust:build-asm && npm run js:prepublish && node ../../scripts/publish-helper cip36 -asmjs && cd publish && npm publish --tag beta --access public",
"js:ts-json-gen": "cd json-gen && cargo +stable run && cd .. && node ../../scripts/run-json2ts.js && node ../../scripts/json-ts-types.js cip36",
"postinstall": "git submodule update --init --recursive && cd binaryen; cmake . && make"
},
"husky": {
"hooks": {
"pre-push": "npm run rust:test && npm run rust:build-nodejs"
}
},
"author": "dcSpark",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/dcSpark/cardano-multiplatform-lib.git"
},
"devDependencies": {
"husky": "4.2.5",
"json-schema-to-typescript": "^10.1.5",
"rimraf": "3.0.2",
"cross-env": "^7.0.3"
}
}

0 comments on commit ba4ce3e

Please sign in to comment.