On Mon, 16 Oct 2023 15:00:42 GMT, Shaojin Wen <d...@openjdk.org> wrote:

>> I submitted PR #15555 before, and there were too many changes. I split it 
>> into multiple PRs with small changes. This one is one of them.
>> 
>> this PR removed the duplicate code for getChars in 
>> BigDecimal#StringBuilderHelper, i also make performance faster.
>> Please review and don't hesitate to critique my approach and patch.
>
> Shaojin Wen has updated the pull request incrementally with one additional 
> commit since the last revision:
> 
>   Use StringConcatFactory.makeConcatWithConstants

Good, narrows it down to what's going on in `prepend(long, byte[], String)`. 
Might boil down to `System.arraycopy`. This method might not be optimized for 
tiny arrays on all platforms. Specializing for single-char case:


diff --git a/src/java.base/share/classes/java/lang/String.java 
b/src/java.base/share/classes/java/lang/String.java
index 9b19d7e2ac1..6eb70925dab 100644
--- a/src/java.base/share/classes/java/lang/String.java
+++ b/src/java.base/share/classes/java/lang/String.java
@@ -4723,7 +4723,11 @@ static void repeatCopyRest(byte[] buffer, int offset, 
int limit, int copied) {
      */
     void getBytes(byte[] dst, int dstBegin, byte coder) {
         if (coder() == coder) {
-            System.arraycopy(value, 0, dst, dstBegin << coder, value.length);
+            if (value.length == 1) {
+                dst[(dstBegin << coder)] = value[0];
+            } else {
+                System.arraycopy(value, 0, dst, dstBegin << coder, 
value.length);
+            }
         } else {    // this.coder == LATIN && coder == UTF16
             StringLatin1.inflate(value, 0, dst, dstBegin, value.length);
         }

.. seem to help the JIT do the right thing consistently, too:

Benchmark                                 Mode  Cnt   Score   Error  Units
BigDecimals.testSmallToEngineeringString  avgt   50  11,757 ± 0,480  ns/op

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

PR Comment: https://git.openjdk.org/jdk/pull/16006#issuecomment-1764787645

Reply via email to