FYC: 7197183 : Provide CharSequence.subSequenceView which allows for sub-sequence views of character sequences. (original) (raw)

Mike Duigou mike.duigou at oracle.com
Thu Jul 17 00:09:29 UTC 2014


Hello all;

In Java 7u6 there was a significant change in the implementation of java.lang.String (JDK-6924259). This was done to reduce the size of String instances and it has been generally regarded as a positive change. As with almost any significant change to a class as core to Java as String there have also been applications negatively impacted. Most of the problems involve applications which make heavy use of String.substring() as sub-string instances now involve creation of their own copies of the backing characters.

There have been previous discussions of mitigations to the 6924259 change in String.substring() behaviour. These discussions haven't come to positive conclusions mostly because they generally require too many changes to the specification or behaviour of String. So here's another proposal (enclosed) that doesn't change the behaviour of any existing classes. It adds two new methods to CharSequence to create sub-sequence views of character sequences. The size of sub-sequence instances very closely matches the size of pre-6924259 String instances and indeed the implementation has the same pre-6924259 limitations, namely that the entire source CharSequence remains alive as long as the sub-sequence is referenced.

Unlike pre-6924259 the CharSubSequenceView can not be reliably compared via equals() to String instances and it is unsuitable for use as a hash map key.

With these benefits and caveats in mind, would you use this?

Mike

diff -r 66f582158e1c src/share/classes/java/lang/CharSequence.java --- a/src/share/classes/java/lang/CharSequence.java Wed Jul 16 20:43:53 2014 +0100 +++ b/src/share/classes/java/lang/CharSequence.java Wed Jul 16 16:58:52 2014 -0700 @@ -25,11 +25,14 @@

package java.lang;

+import java.io.Serializable; import java.util.NoSuchElementException; +import java.util.Objects; import java.util.PrimitiveIterator; import java.util.Spliterator; import java.util.Spliterators; import java.util.function.IntConsumer; +import java.util.function.IntSupplier; import java.util.stream.IntStream; import java.util.stream.StreamSupport;

@@ -231,4 +234,114 @@ Spliterator.ORDERED, false); } +

}



More information about the core-libs-dev mailing list