Very slow compilation for sha1 implementation in -O2
Compiling this sha1 implementation: http://svn.peeklinux.com/trunk/qemu/tests/sha1.c
takes more than 20 minutes on my machine with python emcc -O2 sha1.c. With -s RELOOP=1 it takes a little more than 6 minutes.
(Normally I can compile code that are several orders of magnitude larger in a few minutes.)
The generated code also has very bad performance characteristics, it is more than 100 times slower than when running a native binary.
I know that hand-written sha1 implementations in JavaScript also are slow (about 50 times slower than native code when I last measured). I was under the impression that bitwise operations mapped pretty well to JavaScript..
One more data point: When I compile 7zip's lzma decoding with Emscripten the generated code runs more than 15 times slower than the native version.
There is a very large function here. That causes problems for both our compiler, which slows down, and JS engines when running the generated code. Likely if you split up that function into parts, this will run much faster.
Investigating this further, this is a case where full llvm optimizations hurt us. The unrolled loops lead llvm to load all the data as a single big i512 value and operate on that. This causes lots of temporary variables/register flux which is why it runs slowly. It gets very very slow in the variable eliminator in emscripten, which calculates all the variable dependencies, which is why it compiles slowly.
But if I compile this with -O2 --llvm-opts 0, it runs 13.6X slower than native code, and it compiles in just a few seconds. So at least that is much faster than the 50X slower you mentioned for handwritten code here.
This looks very promising, but unfortunately I can not reproduce your results. With --llvm-opts 0 I do get the speedup in compilation time, but not in execution time.
I modified main() in sha1.c like this:
int
main(int argc, char **argv)
{
int timeTicks0 = emscripten_run_script_int("new Date().getTime()");
<sha1 computation code removed>
int timeTicks1 = emscripten_run_script_int("new Date().getTime()");
printf("SHA1 computation time in milliseconds: %d", timeTicks1 - timeTicks0);
return 0;
}
And then when I compile with python emcc -O2 --llvm-opts 0 --closure 0 sha1.c and run the result I get:
[Work] [none] 17:22 22220M y:\emscripten>node a.out.js
SHA1=15dd99a1991e0b3826fede3deffc1feba42278e6
SHA1 computation time in milliseconds: 2255
If I compile with python emcc -O2 --closure 0 sha1.c and run the result I get this:
[Work] [none] 17:49 22227M y:\emscripten>node a.out.js
SHA1=15dd99a1991e0b3826fede3deffc1feba42278e6
SHA1 computation time in milliseconds: 2283
Running this sha1 computation natively takes 18 milliseconds, which is 125X faster.
Did I do something wrong?
The generated JavaScript files are here: http://pastebin.com/CdK7ee7A http://pastebin.com/cP57tFb6
What JS engine and what version are you using, to test the speed of execution?
[Work] [none] 13:49 22133M y:\emscripten>node --version
v0.6.13
So according to https://raw.github.com/joyent/node/master/ChangeLog this is with V8 version 3.6.6.24.
The latest stable release of Node.js is 0.6.15 which uses the same version of V8.
I also tested with --llvm-opts 0 in Chrome 18.0.1017.2 dev-m which uses V8 version 3.8.7.1, and there the test took 2541 milliseconds (so 141X slower than native).
In Chrome 20.0.1105.2 dev-m which uses V8 version 3.10.2.1 the test took 2516 milliseconds.
Aha! In Firefox version 11.0 this ran a whole lot faster, there the test took 278 milliseconds (so 15X slower than native).
Btw, for me this test uses 237 milliseconds in Firefox 11.0 without specifying --llvm-opts 0 (and this was also without specifying --closure 0).
I filed this V8 issue about the slow execution speed: http://code.google.com/p/v8/issues/detail?id=2097
Would it be hard to optimize the variable eliminator in Emscripten?
Yeah, Chrome/V8 has some cases where they run computation intensive code like this very slowly. We've filed several bugs, thanks for filing another. I'll link to it on https://github.com/kripken/emscripten/wiki/External-Issues
It would be easy to optimize the variable eliminator I think. I believe the issue is the transitive closure - it has a matrix for which variable depends on which other variable, and it repeatedly calculates dependencies on it. But it recalculates the whole matrix each time, even when it doesn't need to. Should not be hard to make that orders of magnitude faster.
That would be fantastic :)
Ok, I modified the eliminator on incoming to do the transitive calculation in a more efficient way. It only looks at variables just modified in the previous iteration. This should help with cases like these with functions with huge numbers of variables with lots of dependencies. Measuring just the eliminator phase, it is 7.5 times faster over here. Still waiting on the test suite though to make sure I didn't do something wrong in the change.
Also I added sha1.cpp to the test suite, actually it failed at first because of an endianness issue, so I fixed endian.h to report the right endianness.
The compilation time for my project went from 17 minutes and 43 seconds to 5 minutes and 34 seconds. Excellent, thank you!
(The sha1 code is still expensive to compile, without it the project compiles in 3 minutes and 44 seconds.)
Sadly the test suite found a bug in my patch (just in o2.test_zlib, good thing we have a big test suite..). Still working on this.
I pushed a simpler optimization. This is about 2x faster than the original slow speed.
I optimized a little more, but can't figure out a way to really speed this up. It can't be fast without a major rewrite I am afraid. Worst case, we will get that later this year with the C++ backend that is planned.
I did a major rewrite of the eliminator. sha1 elimination went from 318 seconds to 0.5 seconds ;)
@kripken / @SiggyBar,
I have a piece of code that needs to leverage sha1 with on the order of 100k+ calls. What are the best optimization settings for compiling sha1.c?
Crude examination shows I am getting about 4x slower than native using a straight -O2, which is great, but would love to reduce that if possible.
Thanks
-O3 should be optimal. You can also try --llvm-lto 1 but it is risky.
Which browser is the 4x number on? How do others compare?
Both seem to have helped a small amount. However, both improvements might be within statistic fluctuations.
I had been testing with node for ease, but switching to Firefox made it only about 2x slower. I can work with that.
Thanks, Michael
On Thu, Jul 16, 2015 at 12:21 PM, Alon Zakai [email protected] wrote:
-O3 should be optimal. You can also try --llvm-lto 1 but it is risky.
Which browser is the 4x number on? How do others compare?
— Reply to this email directly or view it on GitHub https://github.com/kripken/emscripten/issues/406#issuecomment-122009250.