On Sat, 21 Nov 2026 21:27:14 GMT, Liam Miller-Cushon <[email protected]> wrote:

>> This change adds Vector API methods that load the code units of a 
>> `java.lang.String` into a vector without copying, to allow processing a 
>> `String` with a vector loop instead of a scalar loop over `charAt`.
>> 
>> As discussed in the [panama-dev@ thread 'String processing and Vector 
>> APIs'](https://mail.openjdk.org/archives/list/[email protected]/thread/YSPX3TBXZTZEFZNXWDYOZCWLUQPC62P6/),
>>  one motivating use-case is a regex library that wants to accelerate scans 
>> for character classes. Implementing these methods outside the JDK using 
>> scalar `charAt` loops is slower than a vectorized implementation.
>> 
>> ### Goals
>> 
>> * Performance - to get the full performance benefit, these APIs should avoid 
>> copies or re-encoding, since a copy or re-encoding would be O(n) and reduce 
>> the performance benefit of a vector loop
>> * Encapsulation - encapsulation of `java.lang.String` internals must be 
>> preserved, including preventing modification of the String's backing 
>> storage, and also preserving encapsulation of String implementation details 
>> (like the exact encoding under Compact Strings)
>> 
>> ### Design
>> 
>> 
>> // ShortVector
>> static ShortVector fromString(VectorSpecies<Short> species, String string, 
>> int offset)
>> static ShortVector fromString(VectorSpecies<Short> species, String string, 
>> int offset, VectorMask<Short> m)
>> 
>> 
>> The methods load the `char`s of a `String` into the short vector, as UTF-16 
>> code units, with unpaired surrogates preserved, providing the same values as 
>> a scalar loop over `charAt`.
>> 
>> This is the same behaviour as a scalar loop over `charAt`, and guaranteeing 
>> a well-formed encoding would require scanning for unpaired surrogates, which 
>> would defeat the performance goal.
>> 
>> An internal `loadCodeUnits` API is added to `String`, using `VectorSupport` 
>> to allow the Vector implementation to load from the `String`'s `value`s 
>> array while keeping it encapsulated in `String`.
>> 
>> ### Alternative: `ByteVector.fromString`
>> 
>> An earlier version of the PR proposed
>> 
>> 
>> // ByteVector
>> static boolean compatibleWith(String string, Charset charset)
>> static ByteVector fromString(VectorSpecies<Byte> species, String string, 
>> Charset charset, int offset)
>> static ByteVector fromString(VectorSpecies<Byte> species, String string, 
>> Charset charset, int offset, VectorMask<Byte> m)
>> 
>> // ShortVector
>> static boolean compatibleWith(String string, Charset charset)
>> static ShortVector fromString(VectorSpecies<Short> species, String string, 
>> Charset charset, int offs...
>
> Liam Miller-Cushon has updated the pull request incrementally with one 
> additional commit since the last revision:
> 
>   Inline isLatin1() and coder()

I'm late in the game, but the current API already leaks the coder:

void main() {
  String s = "Hello, World!";
  IO.println(s.chars().spliterator());

  String s2 = "Hello! \uD83D\uDE42";
  IO.println(s2.chars().spliterator());
}

-------------

PR Comment: https://git.openjdk.org/jdk/pull/32373#issuecomment-7070180271

Reply via email to