posted 3 years ago

Hi

I am reading this link http://java-performance.info/string-intern-in-java-6-7-8/

Section - JVM string pool implementation in Java 6, 7 and 8

It is written there that

I want to know how prime number can give us better performance.

Thanks

Abhay Agarwal

I am reading this link http://java-performance.info/string-intern-in-java-6-7-8/

Section - JVM string pool implementation in Java 6, 7 and 8

It is written there that

The default pool size is 1009 (it is present in the source code of the above mentioned bug report, increased in Java7u40). It was a constant in the early versions of Java 6 and became configurable between Java6u30 and Java6u41. It is configurable in Java 7 from the beginning (at least it is configurable in Java7u02). You need to specify -XX:StringTableSize=N, where N is the string pool map size. Ensure it is a prime number for the better performance.

I want to know how prime number can give us better performance.

Thanks

Abhay Agarwal

Campbell Ritchie

Marshal

Posts: 56598

172

posted 3 years ago

The article you linked to talks about a fixed-size hash table with N entries which contain linked lists of values. The throwaway reference to "performance" probably means that it's preferable to have the hash function distribute the values uniformly over the elements of the hash table, i.e. over the numbers from 0 to N-1. So now there's the unsupported assertion which appears to mean that prime values of N make this uniform distribution more likely than for non-prime values of N.

Without knowing the hash function, it's impossible to say whether that's the case or not. One might guess that the hash function is the String's hashCode modulo N, but if hashCode is uniformly distributed over the int range then its values modulo N are also uniformly distributed over [0..N-1] whether N is prime or not. And if it isn't uniformly distributed, than reducing it modulo some random prime number isn't guaranteed to yield anything more uniform.

So colour me unconvinced. It sounds like a programmer's urban legend to me. (Notice that none of the many performance tests in the article question that assumption.) Until I hear otherwise, that is.

(And your instinct to question unsupported claims of performance benefits is absolutely right. Good for you.)

Without knowing the hash function, it's impossible to say whether that's the case or not. One might guess that the hash function is the String's hashCode modulo N, but if hashCode is uniformly distributed over the int range then its values modulo N are also uniformly distributed over [0..N-1] whether N is prime or not. And if it isn't uniformly distributed, than reducing it modulo some random prime number isn't guaranteed to yield anything more uniform.

So colour me unconvinced. It sounds like a programmer's urban legend to me. (Notice that none of the many performance tests in the article question that assumption.) Until I hear otherwise, that is.

(And your instinct to question unsupported claims of performance benefits is absolutely right. Good for you.)

Did you see how Paul cut 87% off of his electric heat bill with 82 watts of micro heaters? |