3 ms·
> A special “I would never write Rust like that” variant that returns the value in a Box on the heap. This is because a friend of mine, after seeing my results,
by jeroenhd 4y ago
> A special “I would never write Rust like that” variant that returns the value in a Box on the heap. This is because a friend of mine, after seeing my results, told me I was cheating, because all Rust versions so far used registers/stack to return the value, and Java was at a disadvantage due to returning on the heap (by default). So here you are.
Why would Rust be cheating here? Java cannot make these types of optimizations yet (though they are likely coming with Project Valhalla) but that doesn't mean Rust should be similarly handicapped in benchmarks.
Java has many smart optimizations and advantages over Rust (being garbage collected for one, making it much easier to write code in, and runtime reflection, a blessing and a curse) and with tricks like rearranging objects to make more effective use of CPU caches you can end up writing Java that's very close in performance to native, precompiled code.
However, when it comes to raw performance, you shouldn't expect the standard JVM to come close to Rust. There is inherent overhead in the way the language and the runtime are designed. There is no "cheating" here, the algorithms are the same and some languages just produce more efficient code in these scenarios. You wouldn't slow down the JVM to make the benchmark fair for a Python implementation either!
A more interesting comparison may be compiling Java to native assembly (through Graal for example) so Java too can take advantage of not having to deal with reflection and using SIMD instructions.
Alternatively, a Java vs C# rundown would also be more interesting, as both languages serve similar purposes and solve similar problems. C#'s language-based approach to optional values has the potential to be a lot faster than Java's OOM-based approach but by how much remains to be seen.
Java vs Kotlin may also be interesting to benchmark to see if the Kotlin compiler can produce faster code than Java's Optional; both run inside the same JVM so the comparison may be even better.
- pjmlp 4y agoKotlin is only syntax sugar, so any bytecode pattern it happens to generate better than javac is also doable in Java. In fact it is mostly the opposite, all the Kotlin concepts that don't exist in Java (the language), need additional bytecodes to fake their semantics on top of JVM bytecodes optimized for Java semantics. Like functions, lazy initializations, delegation, or co-routines.
- tadfisher 4y agoDefine "doable". There are many, many bytecode constructs that are possible on the JVM, but are not generated by javac: https://stackoverflow.com/a/23218472 https://stackoverflow.com/a/23218472 Do you mean, "javac can also implement them if it is modified to do so"? Because you are also making the case that Kotlin is syntax sugar on top of Java, when it is actually a bytecode-generating compiler in its own right, so I'm not sure how to understand this comment.
- pjmlp 4y agoJava is syntax sugar for Java Virtual Machine, and the only language that actually matters for the design of Java Virtual Machine Bytecodes. Anyone else has to generate boilercode to pretend the semantics expected by those bytecodes, was easily shown via javap tooling on .class files.
- usrusr 4y agoBut it's syntax sugar designed to make you stop worrying and love the null. Would be quite interesting to see how "elvised" Long? would microbenchmark against Optional<Long> and OptionalLong!
- pjmlp 4y agoNot worth the trouble to be married to JetBrains tooling.
- TeeWEE 4y agoI dont think this is true for the nullability type... Which are in essence compile time optionals... Without the overhead.
- kaba0 4y agoI agree with you, though let’s add that different languages have different paradigms/idiomatic patterns and in case of Java Optional is not one, while in case of Rust, it is and was likely optimizes extensively. Of course the niches of the two languages are very different, the whole point of Rust is being a safe low-level language, which can express the wanted functionality more specifically (at the cost of much higher developer complexity). So this test is as “unfair” as benchmarking Rust’s allocation performance against Java, for example
- motoboi 4y agoGraal is tested and produces no different result than base java. That surprised me.