NumberUtils.createNumber() performance: Short-circuit check based on pre-derived candidate Double#1628
Conversation
Numeric identity pre-check for the heap-allocating f.toString().equals(d.toString())
|
Hello @ax1nch Where is the JMH test you quote? If you don't included it here, I can't run it to replicate your experiment... TY! |
|
Hi Gary Sorry, missed including that last time. So below is that benchmark code I had run between the Default check and the new short-circuit check: Best, Anish |
|
This test doesn't compare anything useful. You need to compare the old code vs. the new code, so: !f.isInfinite() && !(f.floatValue() == 0.0F && !isZero(mant, dec)) && f.toString().equals(d.toString())versus !f.isInfinite() && !(f.floatValue() == 0.0F && !isZero(mant, dec))
&& ((double) d.floatValue() == d.doubleValue() || f.toString().equals(d.toString()))or the whole of the old method vs. the whole of the new method. |
|
Sure.. Earlier I had tried to measure in isolation for when the short-circuiting code applies. I guess below is what you are looking at. The result summary and the corresponding code comparing old code vs new code: Pls note that I've copied over couple of private methods from |
|
@ax1nch |
pre-derived candidate Double #1628
This is a minor performance improvement. A codeql query I ran over some library code had picked up on the empty catch blocks in this file. Those are already handled well here of course, but I happen to notice the allocating toStrings. Since the toString based checks were only used for this condition, I thought it might be nice to find a fully a numeric-only check here too, before following through the history of this, tickets like LANG-1018
So I then tweaked the check to work with a single toString. The performance improvement was consistent, but not significant enough to justify a change. However I feel the current simple shortcircuit check might be worth it - I mean it's almost a free check (like when false), but much faster as a replacement(when true) at ~100x (relevant jmh out below) . It also feels like an apt positive-test, given the fact that the candidate Double is already available at that point
Also to note: the probability of a Float return may not be high in a random distribution of the param string but this is library code and so will be used in many cases where the distribution is not random, and there it counts