> The current versions of FloatToDecimal and DoubleToDecimal allocate 
> additional objects. Reducing these allocations can improve the performance of 
> Float/Double.toString and AbstractStringBuilder's append(float/double).
> 
> This patch is just a code refactoring to reduce object allocation, but does 
> not change the Float/Double to decimal algorithm.
> 
> The following code comments the allocated objects to be removed.
> 
> 
> class FloatToDecimal {
>     public static String toString(float v) {
>         // allocate object FloatToDecimal
>         return new FloatToDecimal().toDecimalString(v);
>     }
> 
>     public static Appendable appendTo(float v, Appendable app)
>             throws IOException {
>         // allocate object FloatToDecimal
>         return new FloatToDecimal().appendDecimalTo(v, app);
>     }
> 
>     private Appendable appendDecimalTo(float v, Appendable app)
>             throws IOException {
>         switch (toDecimal(v)) {
>             case NON_SPECIAL:
>                 // allocate object char[]
>                 char[] chars = new char[index + 1];
>                 for (int i = 0; i < chars.length; ++i) {
>                     chars[i] = (char) bytes[i];
>                 }
>                 if (app instanceof StringBuilder builder) {
>                     return builder.append(chars);
>                 }
>                 if (app instanceof StringBuffer buffer) {
>                     return buffer.append(chars);
>                 }
>                 for (char c : chars) {
>                     app.append(c);
>                 }
>                 return app;
>             // ...
>         }
>     }
> }
> 
> class DoubleToDecimal {
>     public static String toString(double v) {
>         // allocate object DoubleToDecimal
>         return new DoubleToDecimal(false).toDecimalString(v);
>     }
> 
>     public static Appendable appendTo(double v, Appendable app)
>             throws IOException {
>         // allocate object DoubleToDecimal
>         return new DoubleToDecimal(false).appendDecimalTo(v, app);
>     }
> 
>     private Appendable appendDecimalTo(double v, Appendable app)
>             throws IOException {
>         switch (toDecimal(v, null)) {
>             case NON_SPECIAL:
>                 // allocate object char[]
>                 char[] chars = new char[index + 1];
>                 for (int i = 0; i < chars.length; ++i) {
>                     chars[i] = (char) bytes[i];
>                 }
>                 if (app instanceof StringBuilder builder) {
>                     return builder.append(chars);
>                 }
>               ...

Shaojin Wen has updated the pull request incrementally with four additional 
commits since the last revision:

 - Update src/java.base/share/classes/jdk/internal/math/DoubleToDecimal.java
   
   Co-authored-by: Raffaello Giulietti <raffaello.giulie...@oracle.com>
 - Update src/java.base/share/classes/jdk/internal/math/DoubleToDecimal.java
   
   Co-authored-by: Raffaello Giulietti <raffaello.giulie...@oracle.com>
 - Update src/java.base/share/classes/jdk/internal/math/DoubleToDecimal.java
   
   Co-authored-by: Raffaello Giulietti <raffaello.giulie...@oracle.com>
 - Update src/java.base/share/classes/jdk/internal/math/ToDecimal.java
   
   Co-authored-by: Raffaello Giulietti <raffaello.giulie...@oracle.com>

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

Changes:
  - all: https://git.openjdk.org/jdk/pull/19730/files
  - new: https://git.openjdk.org/jdk/pull/19730/files/b8e80428..e3830386

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=19730&range=13
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=19730&range=12-13

  Stats: 4 lines in 2 files changed: 0 ins; 0 del; 4 mod
  Patch: https://git.openjdk.org/jdk/pull/19730.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/19730/head:pull/19730

PR: https://git.openjdk.org/jdk/pull/19730

Reply via email to