• Post Reply Bookmark Topic Watch Topic
  • New Topic

Prime number for Performance  RSS feed

 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't know. Start by asking the author of that article to explain it.
 
Paul Clapham
Sheriff
Posts: 22841
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Abhay Agarwal
Ranch Hand
Posts: 1376
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!