Use single character replace variant in Resources.toPackageName(String)

Christoph Dreis christoph.dreis at freenet.de
Mon Jan 6 17:33:09 UTC 2020


Hi and a happy new year,

I recently noticed that jdk.internal.module.Resources.toPackageName(String) makes use of String.replace(CharSequence, CharSequence) while it could use the single char variant in my opinion:

diff --git a/src/java.base/share/classes/jdk/internal/module/Resources.java b/src/java.base/share/classes/jdk/internal/module/Resources.java
--- a/src/java.base/share/classes/jdk/internal/module/Resources.java
+++ b/src/java.base/share/classes/jdk/internal/module/Resources.java
@@ -64,7 +64,7 @@
         if (index == -1 || index == name.length()-1) {
             return "";
         } else {
-            return name.substring(0, index).replace("/", ".");
+            return name.substring(0, index).replace('/', '.');
         }
     }

I ran an isolated benchmark with some test data on it, which shows the following results

Benchmark                                                            (param)     Mode  Cnt     Score     Error   Units
MyBenchmark.testNew                                       java/lang  avgt      10    14,905 ±   0,130   ns/op
MyBenchmark.testNew:·gc.alloc.rate.norm    java/lang  avgt      10    48,000 ±   0,001    B/op
MyBenchmark.testNew                                      a/b/c/d/e  avgt      10    23,122 ±   0,389   ns/op
MyBenchmark.testNew:·gc.alloc.rate.norm   a/b/c/d/e  avgt      10    96,000 ±   0,001    B/op
MyBenchmark.testOld                                        java/lang  avgt       10    16,614 ±   0,420   ns/op
MyBenchmark.testOld:·gc.alloc.rate.norm     java/lang  avgt       10    48,000 ±   0,001    B/op
MyBenchmark.testOld                                       a/b/c/d/e  avgt       10    84,745 ±   1,329   ns/op
MyBenchmark.testOld:·gc.alloc.rate.norm    a/b/c/d/e  avgt       10   120,000 ±   0,001    B/op

As you can see the more replacing needs to be done the better the newer version seems to be perform.

In case this is considered worthwhile I would need someone to sponsor the patch. If not, I'm sorry for the noise.

Cheers,
Christoph Dreis





More information about the core-libs-dev mailing list