On Wed, 14 Oct 2026 12:08:12 GMT, Maurizio Cimadamore <[email protected]> wrote:
> The proposed approach seems sensible, except that it still amounts at > exposing some of the guts out. I understand what problem we're trying to > solve -- e.g. mapping string to bytes just to load into a vector is more > expensive than the vector op itself. But these `compatible` methods are > really asking how the JDK is encoding the string. For instance, if somebody > wanted to encode a string in some other 1-byte-per-char coder that is not > 8859 (and there are some e.g. in Japanese), this method will say no. And not > because the string cannot be encoded as one byte per char (it can!) but > simply because the JDK happened to use a particular implementation which is > biased towards 8859. > > Which makes it a bit odd to expose as a general API (although, in an > incubating package, maybe it's ok) -- in the sense that it's much less > general than it claims to be. The "right" API is to just give the string and > the charset to the factory, so that the factory, internally, can decide how > best to do things. That's what FFM does. But here the problem is that we > don't want to fallback to "slow code" that encodes the string into a byte[] > -- we prefer to just have it fail, and then use some other approach (e.g. > non-simd). Thanks, I have been continuing to think about this, and have a revised proposal that I think addresses some of the problems here while offering most of the performance. The proposal is to drop the `ByteVector` accessor and also `compatibleWith`, and just have `ShortVector.fromString`. I had [considered this earlier](https://mail.openjdk.org/archives/list/[email protected]/message/BK5PV2ZHGZ2OCHE33D5EHSGLWU52FFO6/), but wasn't thinking about dropping `ByteVector` _and_ `compatibleWith`. `compatibleWith` was necessary to provide a way to know if the `ByteVector` fast path was available, and to know when to use scalar fallback. By just having a `ShortVector` path there's no choice, it can just always provide UTF-16 code units. And even if the underlying String storage changed, the API is already committed to having fast support for UTF-16 code units (for `charAt` etc.). If the internal storage changed `ShortVector.fromString` could do a vectorized conversion back to UTF-16, which would probably still be faster than a scalar loop. And the alternative with `compatibleWith` is that if the internal storage changed `compatibleWith` would return false and the client would fall back to a scalar loop anyways, so having `ShortVector.fromString` degrade to closer to scalar performance is no worse. I do think that it would be interesting to find a way to provide `ByteVector.fromString` to get the last bit of performance here. And if the underlying string storage did change there would be additional motivation for that. But for now, I think `ShortVector.fromString` feels like the better tradeoff of providing vector performance with a smaller API and without leaking Compact String implementation details. What do you think? ------------- PR Comment: https://git.openjdk.org/jdk/pull/32373#issuecomment-7062614481
