The hidden costs of using URL
by Markus Malkusch
Posted on Friday Oct 07, 2016 at 03:27PM in Technology
I'm a fan of using existing types and so I use from time to time the class
URL for URLs. In my Whois API I'm having a quiet huge number of such URL objects in a Spring managed Bean. I recently did a Spring upgrade and experienced a significant performance regression on starting the application. It turned out that Spring did a tiny change which would effectivly call
hashCode() on its beans. And exactly this is an unexpected expensive call in
URL.hashCode(). The API documentation could be more explicit on that:
The hash code is based upon all the URL components relevant for URL comparison. As such, this operation is a blocking operation.
"Blocking" might give a hint on the performance impact or just refer to the fact that this method is synchronized. Digging deeper into the hash code generation, there's this line of code:
InetAddress addr = getHostAddress(u); When generating the hash code for an URL object, the hostname of the URL will be resolved via DNS to its IP address. I.e.
URL.hashCode() effectivly waits on a Network operation.