> 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 offset)
> static ShortVector fromString(VectorSpecies<Short> species, String s...

Liam Miller-Cushon has updated the pull request incrementally with one 
additional commit since the last revision:

  Inline isLatin1() and coder()

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

Changes:
  - all: https://git.openjdk.org/jdk/pull/32373/files
  - new: https://git.openjdk.org/jdk/pull/32373/files/c2662626..0716135e

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=32373&range=05
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=32373&range=04-05

  Stats: 3 lines in 1 file changed: 1 ins; 0 del; 2 mod
  Patch: https://git.openjdk.org/jdk/pull/32373.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/32373/head:pull/32373

PR: https://git.openjdk.org/jdk/pull/32373

Reply via email to