7

I need to determine if an InetSocketAddress is IPv6 or IPv4 efficiently. The only two ways I can see of doing this are either using the instanceof operator, or checking the length of getAddress() (which should return a byte[]). Both of these are less than ideal (instanceof is slow, and getAddress would have to make a copy of the array).

Is there a better alternative?

1
  • 3
    According this, instanceof may not be as slow as you think. Personally, unless you've profiled your app and it's reporting instanceof as taking > 5% of your run time, then I would put off optimizing away instanceof for later. Commented Aug 15, 2011 at 3:10

4 Answers 4

6

I don't think that you will find anything faster than instanceof. In this particular case, I'd expect the JIT compiler to optimize it to loading and comparing a pair of pointers; i.e. roughly 5 machine instructions.

But even if instanceof is significantly slower than I understand it to be, it is highly unlikely that it will have a significant impact on your application's overall performance.

I suggest that you just use instanceof and only bother to optimize it if you have hard evidence of a bottleneck at this point; e.g. evidence from profiling your application on a realistic workload1.


1 - Once you have done that, it should be a simple matter to run / rerun your benchmark with two or more versions of the IPv4 vs IPv6 test and measure which is faster.

Sign up to request clarification or add additional context in comments.

1 Comment

i.e. (inetSocketAddress.getAddress() instanceof Inet6Address)
6

A faster solution is to compare types. The types Inet4Address and Inet6Address are final, therefore they cannot be subclassed.

Inet4Address.class == IPtoCheck.getClass(); Inet6Address.class == IPtoCheck.getClass(); 

Testing shows that this is more than twice as fast as instanceof and isn't affected by the type being compared, whereas the performance of instanceof on Inet4 and Inet6 addresses is different.

1 Comment

But according to this testing instanceof is fastest in Java 8. I am inclined to believe a benchmark whose source code can be reviewed than one that is not cited. The performance difference is too small to be significant, so my guess is that the Java 8 JIT compiler optimizes the approaches using getClass and instanceof to equivalent native code. Note that the JIT compiler can also determine that a class is a leaf class even if it is not final. And it can reoptimize if a loading another class changes that determination.
1

If you don't like instanceof, you could try:

Inet4Address.class.isAssignableFrom(IPtoCheck); Inet6Address.class.isAssignableFrom(IPtoCheck); 

I don't think there is a faster solution than instanceof or the above.

1 Comment

That will be slower that instanceof. The JIT compiler isn't able to optimize reflective type checking.
1

Compared to the overhead of constructing the InetAddress itself with its implicit DNS overheads the overheads if any of instanceof are trivial.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.