Skip to content

Commit 36d4d03

Browse files
committed
Resize the String#split result array size to the specified limit if the limit size is reasonable.
1 parent 635e686 commit 36d4d03

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/main/java/org/truffleruby/core/string/StringNodes.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3140,6 +3140,7 @@ public abstract static class StringAwkSplitPrimitiveNode extends PrimitiveArrayA
31403140
@Child GetByteCodeRangeNode codeRangeNode = GetByteCodeRangeNode.create();
31413141

31423142
private static final int SUBSTRING_CREATED = -1;
3143+
private static final int DEFAULT_SPLIT_VALUES_SIZE = 10;
31433144

31443145
@Specialization(guards = "is7Bit(tstring, encoding, codeRangeNode)")
31453146
protected Object stringAwkSplitAsciiOnly(Object string, int limit, Object block,
@@ -3154,7 +3155,8 @@ protected Object stringAwkSplitAsciiOnly(Object string, int limit, Object block,
31543155
@Cached LoopConditionProfile loopProfile,
31553156
@Bind("strings.getTString(string)") AbstractTruffleString tstring,
31563157
@Bind("strings.getEncoding(string)") RubyEncoding encoding) {
3157-
Object[] ret = new Object[10];
3158+
int retSize = limit > 0 && limit < DEFAULT_SPLIT_VALUES_SIZE ? limit : DEFAULT_SPLIT_VALUES_SIZE;
3159+
Object[] ret = new Object[retSize];
31583160
int storeIndex = 0;
31593161

31603162
int byteLength = tstring.byteLength(encoding.tencoding);
@@ -3230,7 +3232,8 @@ protected Object stringAwkSplit(Object string, int limit, Object block,
32303232
@Cached LoopConditionProfile loopProfile,
32313233
@Bind("strings.getTString(string)") AbstractTruffleString tstring,
32323234
@Bind("strings.getEncoding(string)") RubyEncoding encoding) {
3233-
Object[] ret = new Object[10];
3235+
int retSize = limit > 0 && limit < DEFAULT_SPLIT_VALUES_SIZE ? limit : DEFAULT_SPLIT_VALUES_SIZE;
3236+
Object[] ret = new Object[retSize];
32343237
int storeIndex = 0;
32353238

32363239
final boolean limitPositive = limit > 0;

0 commit comments

Comments
 (0)