miniboxing / miniboxing-plugin

Miniboxing is a program transformation that improves the performance of Scala generics when used with primitive types. It can speed up generic collections by factors between 1.5x and 22x, while maintaining bytecode duplication to a minimum. You can easily add miniboxing to your sbt project:
http://scala-miniboxing.org
Other
121 stars 17 forks source link

Convert Float/Double to Long more accurately #220

Closed Blaisorblade closed 9 years ago

Blaisorblade commented 9 years ago

Warning: don't merge yet.

Double.doubleToLongBits and Float.floatToIntBits are documented to not be fully invertible, because they take extra effort to collapse different NaN values together. Hence, it appears miniboxing could conceivably interfere with (admittedly strange) code that distinguishes different NaNs (say, code also casting across Long and Double, like in some JavaScript engines). Using the Raw variants prevents the issue (and has no overhead at all).

This is untested, done from the browser, and I'm guessing that the goal of this code is to "box" primitives as Long to pass them to the specialized-for-all-primitives version of the interface.

There are more uses of these primitives to review and probably fix. https://github.com/miniboxing/miniboxing-plugin/search?q=doubleToLongBits&type=Code&utf8=%E2%9C%93

codecov-io commented 9 years ago

Current coverage is 90.19%

Merging #220 into wip will change coverage by 0% by dd91fbe

Powered by Codecov

VladUreche commented 9 years ago

Thanks @Blaisorblade! It makes a lot of sense to update the conversions, as we want the fastest version possible. I will update the remaining ones in a later commit.

Still, the default miniboxing transformation now uses three classes per type parameter:

This way we avoid jumping from integral to floating-point and back, which is costly (Rex benchmarked this and it's 4 times slower than a conversion from int to long or from float to double). Also, it seems to be handled differently by the JVM, causing some loop unswitching optimizations not to kick in.

Blaisorblade commented 9 years ago

Thanks @VladUreche! I was indeed looking at your miniboxing for guidance on casting on the JVM ;-), so good to know!

VladUreche commented 9 years ago

Well, you have to thank @ichoran for that benchmark!