RFR: 8310929: Optimization for Integer.toString [v13]

温绍锦 duke at openjdk.org
Fri Sep 1 11:30:41 UTC 2023


On Thu, 31 Aug 2023 19:53:17 GMT, Claes Redestad <redestad at openjdk.org> wrote:

>> 温绍锦 has updated the pull request incrementally with one additional commit since the last revision:
>> 
>>   assert bounds check
>
> src/java.base/share/classes/java/lang/StringUTF16.java line 1585:
> 
>> 1583:                     buf,
>> 1584:                     Unsafe.ARRAY_BYTE_BASE_OFFSET + (charPos << 1),
>> 1585:                     PACKED_DIGITS_UTF16[r]);
> 
> What performance would you get if you used the same lookup table as the other implementations, but inflate the value to UTF-16 on the fly?
> 
> 
> int packed = (int)Integer.PACKED_DIGITS[r];
> int inflated = ((packed & 0xFF00) << 8) | (packed & 0xFF));
> UNSAFE.putIntUnaligned(
>                     buf,
>                     Unsafe.ARRAY_BYTE_BASE_OFFSET + (charPos << 1),
>                     inflated);
> 
> 
> This would avoid juggling more lookup table data around than before, alleviating some of the concerns voiced in this PR comment thread.

Good suggestion, using the same lookup table, we can get similar performance.


  int packed = (int) Integer.PACKED_DIGITS[-i];
  int inflated = ((packed & 0xFF00) << 8) | (packed & 0xFF);

  charPos -= 2;
  assert charPos >= 0 && charPos < buf.length : "Trusted caller missed bounds check";
  UNSAFE.putIntUnaligned(
          buf,
          Unsafe.ARRAY_BYTE_BASE_OFFSET + (charPos << 1),
          inflated, 
          false);


The performance comparison data is as follows:


-Benchmark                                 Mode  Cnt   Score   Error  Units
-StringBuilders.toStringCharWithInt8UTF16  avgt   15  26.812 ± 0.095  ns/op

+Benchmark                                 Mode  Cnt   Score   Error  Units  (use same lookup table)
+StringBuilders.toStringCharWithInt8UTF16  avgt   15  27.807 ± 0.046  ns/op

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

PR Review Comment: https://git.openjdk.org/jdk/pull/14699#discussion_r1312910616


More information about the core-libs-dev mailing list