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() Thanks, I hadn't realized the spliterator implementation leaked that. I think there is a significant difference between (1) an implementation class name leaking compact string internals, and (2) an API like `compatibleWith` that directly answers the question (even if it's specified that the answer could change), and (3) a public API that provides the coder and promises the result will be stable. For (1), if there was ever a reason to rename `StringUTF16$CharsSpliterator` I hope that would not be blocked by compatibility concerns, and anyone who had code that was broken by that change would get to keep the pieces. For (2) it seems a little bit more subtle, even if `compatibleWith` specifies that the result could change it could encourage writing code that relies on it, and it might be less obviously a bad idea than writing `spliterator().getClass().toString()`. The current version avoids this by just having `ShortVector.fromString`, if there's a path to allowing vectorized loops over `byte` if the internal storage supports it I'm still interested in that, but I also think it might make sense to leave for future work since `ShortVector.fromString` provides some of the value with a smaller API and no risk of leaking internals. ------------- PR Comment: https://git.openjdk.org/jdk/pull/32373#issuecomment-7086693006
