Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Chrome 4kB size limit #10

Closed
zemlyansky opened this issue Nov 25, 2021 · 1 comment
Closed

Chrome 4kB size limit #10

zemlyansky opened this issue Nov 25, 2021 · 1 comment

Comments

@zemlyansky
Copy link
Owner

zemlyansky commented Nov 25, 2021

When loading a bundled script with the arima module Chrome throws:

Uncaught RangeError: WebAssembly.Compile is disallowed on the main thread, if the buffer size is larger than 4KB. Use WebAssembly.compile, or compile on a worker thread.

Related: webpack/webpack#6475

I quite agree with the @jdalton reply there:

Imposing arbitrary limits on APIs is a bad practice. Super disappointed in Chrome's direction on this. It's fine to encourage async use but to offer a sync API and then make it broken is super wonky.

But we have what we have. So current workaround is to use WebWorkers as suggested previously (#8) or update the package to use async loading. What do you think?

@zemlyansky zemlyansky mentioned this issue Nov 25, 2021
zemlyansky added a commit that referenced this issue Nov 25, 2021
@zemlyansky
Copy link
Owner Author

zemlyansky commented Nov 25, 2021

tl;dr To load in Chrome upgrade arima to ^0.2.4 then:

const ARIMAPromise = require('arima/async')

ARIMAPromise.then(ARIMA => {
  const ts = Array(10).fill(0).map((_, i) => i + Math.random() / 5)
  const arima = new ARIMA({ p: 2, d: 1, q: 2, P: 0, D: 0, Q: 0, S: 0, verbose: false }).train(ts)
  const [pred, errors] = arima.predict(10)
})

Changes:
As a fast and dirty workaround I added two emcc calls during the building process. One with the -s BINARYEN_ASYNC_COMPILATION=0 flag, another with -s BINARYEN_ASYNC_COMPILATION=1. Those two generate one common wasm/native.wasm file and two Module files wasm/native-sync.js and wasm/native-async.js. The native.wasm file is then wrapped into its own requirable file called wrapper/native.bin.js (no changes here). Module from native-sync.js is used to compile the wasm synchronously in ./index.js(arima) as previously. Module from native-async.js on the other hand, compiles asynchronously and returns a Promise used in ./async.js(arima/async) that also returns a Promise. That last one represents the ARIMA class documented in README.

In such way, to make it work in Chrome, use arima/async. Wait for the promise to fullfill. Use ARIMA

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant