> 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 three additional commits since the last revision: - Encapsulate string internals, drop ByteVector.fromString and compatibleWith - Refresh javadoc - Remove unnecessary @throws NullPointerException ------------- Changes: - all: https://git.openjdk.org/jdk/pull/32373/files - new: https://git.openjdk.org/jdk/pull/32373/files/1c6ff6ab..c2662626 Webrevs: - full: https://webrevs.openjdk.org/?repo=jdk&pr=32373&range=04 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=32373&range=03-04 Stats: 999 lines in 23 files changed: 334 ins; 563 del; 102 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
