[foreign-memaccess+abi] RFR: 8268230: Foreign Linker API & Windows user32/kernel32: String conversion seems broken
Jorn Vernee
jvernee at openjdk.java.net
Thu Jun 10 13:16:32 UTC 2021
On Thu, 10 Jun 2021 12:33:23 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
>> The problem is that we only add a single 0 byte as a null terminator, regardless of the charset used. For wider char sets, more 0 bytes need to be added. For instance, for UTF_16LE two 0 bytes need to be added.
>>
>> This patch fixes the issue by adding the null terminator to the Java string, and only then encoding it as a `byte[]`.
>
> src/jdk.incubator.foreign/share/classes/jdk/incubator/foreign/CLinker.java line 302:
>
>> 300: }
>> 301:
>> 302: private static String addNullTerminator(String str) {
>
> This is gonna allocate another string in an already allocation-heavy code. Wouldn't it be better to just add the correct termination sequence in the segment? I suggest running StrLen benchmark before/after to make sure string conversion performance isn't negatively impacted.
I'll check the benchmark.
I looked at the CharSet API, and AFAICS the only way to get the number of bytes for the null terminator would be to create a String with the value `"\0"` then encode that and check the resulting `byte[]`. I thought going the string concat route would have better chances of being optimized.
-------------
PR: https://git.openjdk.java.net/panama-foreign/pull/554
More information about the panama-dev
mailing list