diff options
author | Taras Tsugrii <ttsugrii@fb.com> | 2018-08-02 04:31:12 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-08-02 04:32:39 -0700 |
commit | 13ebd67cf8ea756a0e82eaa03f0f4a9581ccf9be (patch) | |
tree | 2010da08cc29d7251093b99a1d2bef3553888951 /src/main/java/com/google/devtools/build/lib/syntax | |
parent | bbb32f3f3c487de5e133b74026df72150974d7a5 (diff) |
[Skylark] Speed up string.partition function.
According to JMH using `ImmutableList.of` or `Arrays.asList` is ~2X faster
than using existing approach of creating an empty `ArrayList` with expected
size and populating it using `add` method. This is most likely due to extra
range and capacity checks.
Returning `ImmutableList` instead of `ArrayList` avoids the need to copy it
again in order to create a `SkylarkTuple`.
These changes speed up the function by ~3X.
Closes #5736.
PiperOrigin-RevId: 207079605
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/syntax')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/syntax/StringModule.java | 29 |
1 files changed, 11 insertions, 18 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/syntax/StringModule.java b/src/main/java/com/google/devtools/build/lib/syntax/StringModule.java index 7d4cbb6bd9..482ab6dca1 100644 --- a/src/main/java/com/google/devtools/build/lib/syntax/StringModule.java +++ b/src/main/java/com/google/devtools/build/lib/syntax/StringModule.java @@ -454,36 +454,29 @@ public final class StringModule { * @return A three-tuple (List) of the form [part_before_separator, separator, * part_after_separator]. */ - private static List<String> stringPartition(String input, String separator, boolean forward) { + private static ImmutableList<String> stringPartition( + String input, String separator, boolean forward) { if (separator.isEmpty()) { throw new IllegalArgumentException("Empty separator"); } - int partitionSize = 3; - ArrayList<String> result = new ArrayList<>(partitionSize); int pos = forward ? input.indexOf(separator) : input.lastIndexOf(separator); if (pos < 0) { - for (int i = 0; i < partitionSize; ++i) { - result.add(""); - } - // Following Python's implementation of str.partition() and str.rpartition(), // the input string is copied to either the first or the last position in the // list, depending on the value of the forward flag. - result.set(forward ? 0 : partitionSize - 1, input); + return forward ? ImmutableList.of(input, "", "") : ImmutableList.of("", "", input); } else { - result.add(input.substring(0, pos)); - result.add(separator); - - // pos + sep.length() is at most equal to input.length(). This worst-case - // happens when the separator is at the end of the input string. However, - // substring() will return an empty string in this scenario, thus making - // any additional safety checks obsolete. - result.add(input.substring(pos + separator.length())); + return ImmutableList.of( + input.substring(0, pos), + separator, + // pos + sep.length() is at most equal to input.length(). This worst-case + // happens when the separator is at the end of the input string. However, + // substring() will return an empty string in this scenario, thus making + // any additional safety checks obsolete. + input.substring(pos + separator.length())); } - - return result; } @SkylarkCallable( |