News for hackers. Stuff that matters.
Re:Overhead of Returning Optional Values in Java and Rust by _old_dude_ on Saturday October 16, 2021 @01:05PM (#28889326)
I expect Java to be slower, Optional was introduced as an afterthought not as a native construct like in Rust.

Sadly, the two benchmarks are not equivalent, the Rust version loop over ints, the Java version loop over longs.

Looping over longs is notoriously slow in Java, because there is no vectorisation and the JIT insert insert a GC checks because looping over longs may take a long of time.

Also one of the codes uses Optional<Long> instead of OptionalLong, so the code does a double boxing long -> Long -> OptionalLong.

Re:Overhead of Returning Optional Values in Java and Rust by tialaramex on Saturday October 16, 2021 @02:31PM (#28890197)
Rust doesn't have types named 'int' and 'long' out of the box. It does have types named e.g. i32, i64, u32 and u64.

Because the function get_int() takes a u64, Rust will conclude that the variable i must be a u64 so as to fit in that parameter.

It seems to me that "Java is bad at 64-bit integers" is also useful information but not pertinent to this benchmark, unless you assume it's trying to compare Java to Rust, at which of course Java will be far slower - but I don't think that's the point of the benchmark at all.

Re:Overhead of Returning Optional Values in Java and Rust by comex on Saturday October 16, 2021 @01:58PM (#28889895)
The Rust version iterates over u64, which is the same size as Java long, though it’s unsigned whereas Java long is signed.
Re:Overhead of Returning Optional Values in Java and Rust by Gadiguibou on Saturday October 16, 2021 @01:59PM (#28889908)
> Sadly, the two benchmarks are not equivalent, the Rust version loop over ints, the Java version loop over longs.

I thought longs were 64 bit signed integers in Java, what's the difference with a u64 in Rust? Is it the signedness that affects performance?

> OptionalLong

Thanks for the info! I didn't know there were Optional primitive types in Java!

Re:Overhead of Returning Optional Values in Java and Rust by sushsjsuauahab on Saturday October 16, 2021 @02:32PM (#28890204)
Well, it's not exactly a primitive (long) and instead it is an Object (Long), but I understand what you mean :)
Re:Overhead of Returning Optional Values in Java and Rust by shawnz on Saturday October 16, 2021 @11:37PM (#28893733)
OptionalLong is an object which optionally contains a value type (lowercase L) long. Optional<Long> is an object Optional which optionally contains an object Long.
Re:Overhead of Returning Optional Values in Java and Rust by sushsjsuauahab on Sunday October 17, 2021 @03:35PM (#28899112)
Oh nice! I should have checked the docs or Intellij to see that it returns the primitive type ;)