RFR: 8327156: Avoid copying in StringTable::intern(oop, TRAPS)
Johan Sjölen
jsjolen at openjdk.org
Tue Oct 15 13:10:13 UTC 2024
On Tue, 15 Oct 2024 07:37:04 GMT, David Holmes <dholmes at openjdk.org> wrote:
>> Hi everyone,
>>
>> String interning can be done on 4 different types of strings:
>> - oop-strings (unicode)
>> - oop-strings (latin1)
>> - Symbols (non-null-terminated utf8)
>> - null-terminated utf8 char arrays
>>
>> Currently, when doing interning, all 4 types are first converted to unicode and copied to a jchar array. This array is used when looking in the CDS- and interning tables. If an existing string does not exist, this array is converted to a new string object, which is then inserted into the interning table.
>>
>> This is less efficient than it has to be. As strings are likely to exist in the table(s), it would be beneficial to avoid the initial jchar array allocation. When inserting into the interning table, there is also a possibility to reuse the original string object, avoiding another allocation.
>>
>> This change makes it possible to search in the tables using the different string types, avoiding that initial allocation. This is done by wrapping the string and tagging it with a type, with helper functions directing to the correct hashing/lookup/equal functions. When inserting into the table, we can now reuse the original object or go directly from the input type to an object. To do this, functionality had to be added to hash utf8-strings and to compare oop-strings with utf8. These convert utf8 into unicode character by character and operates on those, thus avoiding needing extra allocations.
>>
>> Some quick rudimentary JMH benchmarks show a ~20% increase in throughput when interning the same string repeatedly, and a ~5% increase in throughput interning only unique strings. (Only tested on my local mac aarch debug build)
>>
>> 2 new tests have also been added. The first test tests that hash codes and string equality remain consistent when converting between different string types. The second test tests that string interning works as expected when equal strings are interned from different string types.
>> Also tested and passes tiers 1-3.
>
> src/hotspot/share/classfile/javaClasses.cpp line 740:
>
>> 738: }
>> 739:
>> 740: bool java_lang_String::equals(oop java_string, const jchar* chars, int num_unicode_points) {
>
> Please undo these changes - these are not "unicode points".
I don't think that you're correct here, David. As far as I understand, the length `len` does not represent the length of the `chars` array but the number of unicode code points in the array. The same is true for the other overloads, such as the UTF8 string. I'd appreciate it if you could explain why what I'm saying is incorrect here.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/21325#discussion_r1801134097
More information about the hotspot-dev
mailing list