JDK-8191874 : (str) Need String alternative with shared storage
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • Submitted: 2017-11-25
  • Updated: 2022-12-25
Related Reports
Relates :  
Description
Prior to the fix for JDK-6924259 (in JDK 8 and JDK 7u6) the String was allowed to share its storage with its substrings.
That approach, in particular, could lead to memory leaks, and had some other disadvantages.

However, it also had its advantages, e.g. ability to search within a limited range of a String:  str.substring(n, m).indexOf(xxx).

We need to investigate, if it is worthwhile to introduce another CharSequence implementation, which would behave analogously to the old implementation of String, i.e. will have a link to the potentially shared buffer, plus offset within this buffer and length.
This new class can be named in such a way that it would remind about its shared buffer, so that the user will have to take responsibility to avoid memory leaks.  One possible name is SharedString.

The String class will have a methods to create SharedStrings referencing the entire string or it's substrings without expensive copying.

SharedString will have search/match methods, analogous to the ones of String.

Primary use case of the new class would be to create local temporary variables for cheap substrings for quick access to sub-ranges.
This may be useful for applications with extensive text processing, like parsers.

Need to create a draft implementation and see how using such an alternative to String affects performance of JDK tools: javac, javadoc, etc.