Same story for doubleValue, floatValue, longValue, intValue, byteValue, shortValue, min, max, signum.
I have not benchmarked it, but I think this is a "bit" slower than just implementing the method.
Is there a reason why we don't just do that?
Thanks,
Simon
I haven't just benchmarked it but I recall that after warm-up it is just as fast. Not sure if it delays warm-up. Far worse is isNaN which doesn't even take the value class route.
I haven't just benchmarked it but I recall that after warm-up it is just as fast. Not sure if it delays warm-up. Far worse is isNaN which doesn't even take the value class route.
Then I'm wondering what's the difference in RichInt is...
--
(1) Chars are unsigned. abs should just return itself.
(2) For types that must be widened to Int (i.e. Byte, Char, Short), min and max are faster if you do the direct comparison: def min(that: Byte) = if (self < that) self else that so that you don't need to cast back to the smaller type.
You underestimate the effectiveness of the JIT compiler. I did test.Are you sure? I'm assuming here that the JVM's intrinsics are magnitudes faster than leveraging user-level code with branches.
What are these jvm intrinsics? java.lang.Math.max is in effect "if (x < y) y else x". I'm not sure how inlining that expression can be orders of magnitude slower.
What are these jvm intrinsics? java.lang.Math.max is in effect "if (x < y) y else x". I'm not sure how inlining that expression can be orders of magnitude slower.
What are these jvm intrinsics? java.lang.Math.max is in effect "if (x < y) y else x". I'm not sure how inlining that expression can be orders of magnitude slower.
If you refer to the Java implementation, that's correct. The difference is that most JVM's these days replace the implementation of java.lang.Math and parts of java.lang.Integer/java.lang.Long with branch-less processor-specific instructions, so the actual Java implementation is never run.
If you refer to the Java implementation, that's correct. The difference is that most JVM's these days replace the implementation of java.lang.Math and parts of java.lang.Integer/java.lang.Long with branch-less processor-specific instructions, so the actual Java implementation is never run.
What's the general strategy for taking this sort of thing into account? Do you guys sit around reading hotspot code?
Actually min and max are HotSpot intrinsics. See lines 644-645:
What's the general strategy for taking this sort of thing into account? Do you guys sit around reading hotspot code?
Code generators are encouraged to use platform-specific native libraries or microprocessor instructions, where available, to provide higher-performance implementations ofMathmethods.
The standard library is used by many JVMs, not just HotSpot. And a JNI-based method would be slower than the pure Java one. I agree with you that a better way to declare instrinsics would be welcome.
There is no reason why native methods in the JDK would have to pay the JNI tax
(and I'm pretty sure they don't).
Anyway, I agree with Paul, for the JDK, there is not a big distinction between native methods and non-native, intrinsic methods, although non-native methods have one nicety:
If you are running on a JVM which lacks better implementations or you run your code on a Pentium1, then the JDK comes with a default implementation of it, for free!
Well, isn't the sad story that the JVM does the JDK favors it won't do for other *DKs?
--
What are these jvm intrinsics? java.lang.Math.max is in effect "if (x < y) y else x". I'm not sure how inlining that expression can be orders of magnitude slower.
If you refer to the Java implementation, that's correct. The difference is that most JVM's these days replace the implementation of java.lang.Math and parts of java.lang.Integer/java.lang.Long with branch-less processor-specific instructions, so the actual Java implementation is never run.
--
Well, isn't the sad story that the JVM does the JDK favors it won't do for other *DKs?
Do you have any evidence for this? As far as I know, there is no other mechanism about from intrinsic methods to avoid the JNI tax.
I just asked someone who is supposed to know it, answer: Native methods inside the OpenJDK don't pay JNI overhead.
I just asked someone who is supposed to know it, answer: Native methods inside the OpenJDK don't pay JNI overhead.
Do you have any evidence for this? As far as I know, there is no other mechanism about from intrinsic methods to avoid the JNI tax.
I just asked someone who is supposed to know it, answer: Native methods inside the OpenJDK don't pay JNI overhead.
On Sun, Jun 23, 2013 at 1:41 PM, Simon Ochsenreither <simon.och...@gmail.com> wrote:Here is what appears some support for simon's information, at least in the specific case of sun.misc.Unsafe - though even if true, I don't know if it generalizes.
I just asked someone who is supposed to know it, answer: Native methods inside the OpenJDK don't pay JNI overhead.
You could put a breakpoint on them and observe how it isn't triggered? :-)
JVM debugging is not C debugging: the design rule (from Self onwards) is that if optimizations change the behavior visible under debugging, they are undone. But only where you can see them, so that the rest of the code runs at full speed. There's some mind-bogging technology working for you during JVM debugging.By your reasoning, you could observe that optimizers reorder instructions on the debugger, by seeing the line pointer jumping back and forth on source without any loops. When I programmed in C (circa 2004-2006) and debugged optimized code, that was the rule.
1911 // %%% This folding logic should (ideally) be in a different place.
1912 // Some should be inside IfNode, and there to be a more reliable
1913 // transformation of ?: style patterns into cmoves. We also want
1914 // more powerful optimizations around cmove and min/max.
--
You received this message because you are subscribed to the Google Groups "scala-internals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-interna...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
Here are the intrinsics implemented in hotspot:
This is not evidence. :) For what is worth, I know for a fact that methods inside OpenJDK do pay JNI overhead. There are countless issues about this in Sun's bug DB. Including things like gzip compression.
On Sun, Jun 23, 2013 at 11:25 PM, Ismael Juma <ism...@juma.me.uk> wrote:
This is not evidence. :) For what is worth, I know for a fact that methods inside OpenJDK do pay JNI overhead. There are countless issues about this in Sun's bug DB. Including things like gzip compression.That sounds like you could easily do better in general. You always need something like JNI if a native library that doesn't know about the VM should work on data structures (= memory locations) defined inside the VM. Unsafe's methods are obviously special because their implementations know everything about data structure in the VM but still can't be implemented in Java code so they have to be declared native in the classfiles but don't need the JNI overhead because they don't actually call out of the VM.