Remove redundant calls of toString()

Andrew Haley aph at redhat.com
Tue Apr 29 10:03:40 UTC 2014


On 04/29/2014 10:58 AM, David Holmes wrote:
> On 29/04/2014 7:40 PM, Andrew Haley wrote:
>> On 04/28/2014 07:57 AM, David Holmes wrote:
>>> On 28/04/2014 1:05 PM, Otávio Gonçalves de Santana wrote:
>>>> In my opinion not, because Objects.requireNonNull is more readable than
>>>> just string.toString. This way is more understandable which field is
>>>> required and doesn't impact on performance.
>>>
>>> An invocation of requireNonNull is potentially more expensive than the
>>> implicit null check that happens with foo.toString().
>>
>> It's also potentially cheaper: a cbnz versus a fetch from memory with a
>> few cycles pipeline delay.
> 
> Can you elaborate please? AFAIK implicit null checks have no overhead 
> when the object reference is not null.

I'm sorry, that remark was too simplistic.

If you have to read the data at that time, I agree.  And it might do
some useful prefetching.  But if you're just checking the object is
non-null it may be cheaper to do (o != null) .  I have noticed that
some Java code does (o.getClass()) or something similar to get an
implicit check rather than if (o != null)...

> Interpreted it is slower and the bytecode footprint is larger.
> 
> IIRC the guidelines for use of requireNonNull in the JDK code was for 
> replacing explicit null checks in precondition checking, not implicit ones.
> 
> Anyway I don't find these changes compelling.

I agree.

Andrew.





More information about the core-libs-dev mailing list