News for hackers. Stuff that matters.
Re:Java is better than C++ for high speed trading systems by knuthsat on Thursday November 26, 2020 @05:30PM (#25224013)
Let's say you write an Iterable class so that you can use foreach on your data structure. It's quite possible iteration won't be completely inlined and the overhead of calling hasNext & next can become non-significant.

An even worse case that made massive speedups is just inlining everything. I believe modern IDEs can automatically inline all invocations of a function. You'd be surprised how much performance you can get with that.

As for writing efficient Java, it comes down to using primitive and value types.

Although, my experience is mostly with Java 8. I have no idea how streams or other new syntax works. After optimizing a really massive Java code base I've never used it since.

Re:Java is better than C++ for high speed trading systems by iamcreasy on Thursday November 26, 2020 @07:32PM (#25224572)
But you still have to make the next and hasNext calls in a for loop. What guarantees that they would be inline in a for loop?
Re:Java is better than C++ for high speed trading systems by knuthsat on Friday November 27, 2020 @05:44AM (#25227465)
I inline them manually.
Re:Java is better than C++ for high speed trading systems by iamcreasy on Saturday November 28, 2020 @03:07PM (#25240100)
How much was the performance improvement?
Re:Java is better than C++ for high speed trading systems by knuthsat on Sunday November 29, 2020 @06:00AM (#25244386)
The biggest improvement I measured was about 15x. I'd say you can get 3x most of the time (go down to 33% of previous runtime).
Re:Java is better than C++ for high speed trading systems by iamcreasy on Sunday November 29, 2020 @04:18PM (#25248173)
That is insane speedup! Thanks for sharing. How do I find next and hasNext is the bottleneck? Should I run profiler to see if they are being called a large number of times?
Re:Java is better than C++ for high speed trading systems by knuthsat on Sunday November 29, 2020 @06:59PM (#25249308)
Unfortunately, any profiler I tried does not show these issues. Because the code you are profiling will be optimized completely differently. Although it makes sense to do the optimizations/inlining inside the top-level functions that profiler reports to take a lot of time (even if the iteration happens in deeper levels).

You just have to try it and see. I remember it was quite easy in IntelliJ, I believe there's an option to inline all invocations of a function. I just went crazy with that option but did it systematically to really find where the bottleneck is and make a minimal change.