-
-
Notifications
You must be signed in to change notification settings - Fork 672
Complex computation slower than JavaScript #760
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Probably interop between wasm and js is a key. Without full source of code and benchmark hard to tell what's wrong. Are you use |
No, i don't use any flags, the code for passing typedarray to wasm please see this gist: https://gist.github.com/hjlld/db8f3856fb9b8724814c2f3ef722e1d7 and in AS i exported relative type id by:
|
And you measure |
the two geometries i mentioned before, each one have 28052 f32 numbers, which means totally 28052 * 2 numbers passed from JS to wasm make the computation. @MaxGraey the full computation code is updated in the same gist: https://gist.github.com/hjlld/db8f3856fb9b8724814c2f3ef722e1d7#file-caldistanceoftwomesharray-ts in which:
|
Several suggestions:
@inline
function Min(a: f32, b: f32, c: f32): f32 {
return Mathf.min(a, Mathf.min(b, c));
}
let ilen = meshes1.length;
let jlen = meshes2.length;
for (let i = 0; i < ilen; i ++ ) {
for ( let j = 0; j < jlen; j ++ ) {
...
}
}
let mesh = new Mesh( unchecked(positions2[ i ]), unchecked(indices2[ i ]), unchecked(matrices2[ i ] )); and for set index accessors: unchecked(vertex1Array[ i ] = 100.0f)
if( distance < distanceMin ) {
distanceMin = distance;
} use distanceMin = Mathf.min(distanceMin, distance); |
How you measure? |
I mean how you measured performance? Is it |
I use |
Got it. So you measure also a lot of array's creation and interops which not exists on JS side. You should optimize |
thanks a lot , i'll fix the code and try again. |
should every |
Yes, this skip bounds check. But until this PR which fix some missing unchecked methods for typed arrays is not landed in dist files this |
i'll close this, try to implement a new algorithm. Thanks @MaxGraey a lot! |
Hello, I'm working on solve a problem that compute the minimum distance between two geometries in 3D space. I choose a brutal force algorithm, it's a complexity
O(nm)
fornm
distances computed.It only relies on + - * / and Mathf.sqrt(), Mathf.abs() computation, all numbers are f32.
Any way I realized it by Javascript( compiled from Typescript ), then I use the same Typescript code to compile it to wasm by AssemblyScript's compiler.
The wasm way also works, but seems even slower than JS.
For example, same two geometries, in JS cost 83 seconds, but in wasm / web worker, need 445 seconds.
Any one could help me to provide any idea or possible reason?
BTW, I use wasm in web workers, but all typedarray are transferable with their buffer, like
Worker.postMessage( typedarray, [ typedarray.buffer ] )
, so i don't think the data transfer between main thread and worker thread is the key.The text was updated successfully, but these errors were encountered: