Objects.requireNonNull() shows zero overhead here.

I guess the main question is whether Objects.requireNonNull(this) behavior in the former test is a result of chance and current Hotspot behavior or is it somehow guaranteed by the spec.

I haven't looked into what actually happens in JIT-compilers on your benchmark, but I'm surprised it works at all.

Explicit null check on the receiver is an easy target for elimination and should be effectively a no-op in generated code. (And that's what you observe with the benchmark!) Once the check is gone, nothing keeps receiver alive anymore (past the last usage).

So, I'd say such behavior it's a matter of chance in your case and can't be relied on in general. And definitely not something guaranteed by JVMS.

Best regards,
Vladimir Ivanov

Reply via email to