mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 14:54:52 +02:00
8262994: Refactor String.split to help method inlining
Reviewed-by: plevart
This commit is contained in:
parent
2d7690b2e5
commit
622b6594d1
1 changed files with 42 additions and 33 deletions
|
@ -3129,43 +3129,52 @@ public final class String
|
|||
(ch < Character.MIN_HIGH_SURROGATE ||
|
||||
ch > Character.MAX_LOW_SURROGATE))
|
||||
{
|
||||
int off = 0;
|
||||
int next = 0;
|
||||
boolean limited = limit > 0;
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
while ((next = indexOf(ch, off)) != -1) {
|
||||
if (!limited || list.size() < limit - 1) {
|
||||
list.add(substring(off, next));
|
||||
off = next + 1;
|
||||
} else { // last one
|
||||
//assert (list.size() == limit - 1);
|
||||
int last = length();
|
||||
list.add(substring(off, last));
|
||||
off = last;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// If no match was found, return this
|
||||
if (off == 0)
|
||||
return new String[]{this};
|
||||
|
||||
// Add remaining segment
|
||||
if (!limited || list.size() < limit)
|
||||
list.add(substring(off, length()));
|
||||
|
||||
// Construct result
|
||||
int resultSize = list.size();
|
||||
if (limit == 0) {
|
||||
while (resultSize > 0 && list.get(resultSize - 1).isEmpty()) {
|
||||
resultSize--;
|
||||
}
|
||||
}
|
||||
String[] result = new String[resultSize];
|
||||
return list.subList(0, resultSize).toArray(result);
|
||||
// All the checks above can potentially be constant folded by
|
||||
// a JIT/AOT compiler when the regex is a constant string.
|
||||
// That requires method inlining of the checks, which is only
|
||||
// possible when the actual split logic is in a separate method
|
||||
// because the large split loop can usually not be inlined.
|
||||
return split(ch, limit);
|
||||
}
|
||||
return Pattern.compile(regex).split(this, limit);
|
||||
}
|
||||
|
||||
private String[] split(char ch, int limit) {
|
||||
int off = 0;
|
||||
int next = 0;
|
||||
boolean limited = limit > 0;
|
||||
ArrayList<String> list = new ArrayList<>();
|
||||
while ((next = indexOf(ch, off)) != -1) {
|
||||
if (!limited || list.size() < limit - 1) {
|
||||
list.add(substring(off, next));
|
||||
off = next + 1;
|
||||
} else { // last one
|
||||
//assert (list.size() == limit - 1);
|
||||
int last = length();
|
||||
list.add(substring(off, last));
|
||||
off = last;
|
||||
break;
|
||||
}
|
||||
}
|
||||
// If no match was found, return this
|
||||
if (off == 0)
|
||||
return new String[]{this};
|
||||
|
||||
// Add remaining segment
|
||||
if (!limited || list.size() < limit)
|
||||
list.add(substring(off, length()));
|
||||
|
||||
// Construct result
|
||||
int resultSize = list.size();
|
||||
if (limit == 0) {
|
||||
while (resultSize > 0 && list.get(resultSize - 1).isEmpty()) {
|
||||
resultSize--;
|
||||
}
|
||||
}
|
||||
String[] result = new String[resultSize];
|
||||
return list.subList(0, resultSize).toArray(result);
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits this string around matches of the given <a
|
||||
* href="../util/regex/Pattern.html#sum">regular expression</a>.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue