JDK-8252827 : Caching Integer.toString just like Integer.valueOf
  • Type: Enhancement
  • Component: core-libs
  • Affected Version: 14
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: generic
  • CPU: generic
  • Submitted: 2020-09-04
  • Updated: 2021-04-30
  • Resolved: 2021-04-30
Related Reports
Relates :  
Description
A DESCRIPTION OF THE PROBLEM :
String and int are the most used types,and Integer.toString(int) are most used too.
Integer.toString may have a cache just like Integer.valueOf,it may save a lot of memory and gc time.



Comments
The Integer cache is mostly an anachronism from when autoboxing was added in Java SE 5.0, when it was felt that autoboxing would unnecessarily proliferate instances of wrapper classes. A cache can clearly save memory by deduplicating object instances. However, the cache lookup in valueOf adds enough complexity that it inhibits scalar replacement, which could avoid object allocation entirely. Thus the overall effects of the Integer cache are unclear. Caching string conversions is probably less useful, as they are probably less frequently used than Integer values themselves. The cost of checking the cache (preloading it, or lazily allocating instances) likely outweighs potential memory savings. The situation will undoubtedly change with Valhalla, which will convert Integer into a primitive class. This will mostly remove the need for the Integer cache. This will put a string cache into even more doubt. In the future, such caching may be revisited, but it probably won't be an obvious extension of the current Integer cache. See email discussion thread: http://mail.openjdk.java.net/pipermail/core-libs-dev/2021-April/076454.html Closing as Won't Fix.
30-04-2021

Moved to JDK to get more thoughts about this enhancement.
06-09-2020