From 1018609ec461525130e57423f33447de399ccc21 Mon Sep 17 00:00:00 2001 From: Charles Oliver Nutter Date: Mon, 9 Oct 2023 16:39:21 -0500 Subject: [PATCH] Simplify and reduce alloc for space padding --- .../org/jruby/util/cli/OutputStrings.java | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/core/src/main/java/org/jruby/util/cli/OutputStrings.java b/core/src/main/java/org/jruby/util/cli/OutputStrings.java index 0abc6efbf37..5428e012eef 100644 --- a/core/src/main/java/org/jruby/util/cli/OutputStrings.java +++ b/core/src/main/java/org/jruby/util/cli/OutputStrings.java @@ -7,6 +7,7 @@ import org.jruby.util.SafePropertyAccessor; import java.time.LocalDate; +import java.util.Arrays; /** * Utility methods to generate the command-line output strings for help, @@ -167,13 +168,23 @@ private static String strBold(String str, boolean tty) { return "\033[1m" + str + "\033[0m"; } + private static final int SPACES_MAX = 256; + private static final char[] SPACES = new char[SPACES_MAX]; + static { + Arrays.fill(SPACES, ' '); + } + private static String generateSpaces(int total) { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < total; i++) { - sb.append(" "); + char[] spaces; + + if (total > SPACES_MAX) { + spaces = new char[total]; + Arrays.fill(spaces, ' '); + } else { + spaces = SPACES; } - return sb.toString(); + return new String(spaces, 0, total); } private static String breakLine(String str, int index, int spaces) {