Daninet / hash-wasm

Lightning fast hash functions using hand-tuned WebAssembly binaries
https://npmjs.com/package/hash-wasm
Other
859 stars 49 forks source link

Performance issues #2

Closed zdm closed 4 years ago

zdm commented 4 years ago

My tests shows other values:

jssha-sha1      - 135972.15 iterations / second (100000 / 0.74 sec.)
wasm-sha1       - 38596.03 iterations / second (100000 / 2.59 sec.)

Test is simple hash calc. func, called in loop.

jssha

const hash = new jsSHA( "SHA-1", "BYTES" );
hash.update( str );
hash.getHash( "HEX" );

hash-wasm

await sha1( str );

What I am doing wrong?

Daninet commented 4 years ago

I ran some additional tests and I managed to reproduce your results. It seems that for small input buffers jsSHA is much faster in SHA-1.

Here are my measurements: 16 bytes => jssha is about 2.6x faster 256 bytes => jssha has similar performance compared to hash-wasm 1024 bytes => hash-wasm is about 2x faster 32 kilobytes => hash-wasm is about 7.7x faster

My benchmarks in the readme, were run on a 4MB input buffer. That's why we see a big difference there. Thank you for bringing up this issue. I will add some additional columns to the benchmark section to also compare the performance of hashing smaller chunks of data.

zdm commented 4 years ago

ok, thank you for clarifying,

Daninet commented 4 years ago

I found a way to improve the performance for smaller input. You can check it out in v3.0.1.

Also, I added a new column with the 32 byte benchmark results.

zdm commented 4 years ago

Yes, I see, Thank you. Interesting, why your code is slow on small data?

zdm commented 4 years ago

You forgot to remove console.log from code.

await sha1( "123" );

prints to console.

Daninet commented 4 years ago

I improved the performance further in version 3.1.1. Now it should be at least 2 times faster than jsSHA for short input strings.

I cannot reproduce the console.log issue. I don't have console.logs in the code.

Daninet commented 4 years ago

The bottleneck is the JavaScript -> WebAssembly boundary. Currently, the WASM function calls from JavaScript are very expensive. I managed to increase the performance by decreasing the number of WASM calls from 3 to 1 in case of small input data.

I think, that in the future these bottlenecks will disappear, while the JavaScript engine developers implement further optimizations for WASM.

zdm commented 4 years ago

Thank you, performance is amazing now.