-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: Replace Guava Splitter with faster custom String splitting method
- Loading branch information
Showing
8 changed files
with
125 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,9 +13,11 @@ | |
* Contributors: | ||
* - Microsoft Corporation: Initial code, written in TypeScript, licensed under MIT license | ||
* - Angelo Zerr <[email protected]> - translation and adaptation to Java | ||
* - Sebastian Thomschke - add splitToArray/List methods | ||
*/ | ||
package org.eclipse.tm4e.core.internal.utils; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Consumer; | ||
import java.util.regex.Pattern; | ||
|
@@ -62,6 +64,59 @@ public static boolean isValidHexColor(final CharSequence hex) { | |
return false; | ||
} | ||
|
||
/** | ||
* Very fast String splitting. | ||
* | ||
* 7.5 times faster than {@link String#split(String)} and 2.5 times faster than {@link com.google.common.base.Splitter}. | ||
*/ | ||
public static String[] splitToArray(final String line, final char separator) { | ||
var tmp = new String[8]; | ||
int count = 0; | ||
int start = 0; | ||
int end = line.indexOf(separator, 0); | ||
while (end >= 0) { | ||
if (count == tmp.length) { // check if array needs resize | ||
final var tmp2 = new String[tmp.length + tmp.length >> 1]; | ||
System.arraycopy(tmp, 0, tmp2, 0, count); | ||
tmp = tmp2; | ||
} | ||
tmp[count] = line.substring(start, end); | ||
count++; | ||
start = end + 1; | ||
end = line.indexOf(separator, start); | ||
} | ||
if (count == tmp.length) { // check if array needs resize | ||
final var tmp2 = new String[tmp.length + 1]; | ||
System.arraycopy(tmp, 0, tmp2, 0, count); | ||
tmp = tmp2; | ||
} | ||
tmp[count] = line.substring(start); | ||
count++; | ||
|
||
if (count == tmp.length) { | ||
return tmp; | ||
} | ||
final var result = new String[count]; | ||
System.arraycopy(tmp, 0, result, 0, count); | ||
return result; | ||
} | ||
|
||
/** | ||
* Very fast String splitting. | ||
*/ | ||
public static List<String> splitToList(final String line, final char separator) { | ||
final var result = new ArrayList<String>(8); | ||
int start = 0; | ||
int end = line.indexOf(separator, 0); | ||
while (end >= 0) { | ||
result.add(line.substring(start, end)); | ||
start = end + 1; | ||
end = line.indexOf(separator, start); | ||
} | ||
result.add(line.substring(start)); | ||
return result; | ||
} | ||
|
||
public static int strcmp(final String a, final String b) { | ||
final int result = a.compareTo(b); | ||
if (result < 0) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
...eclipse.tm4e.core/src/test/java/org/eclipse/tm4e/core/internal/utils/StringUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Copyright (c) 2023 Vegard IT GmbH and others. | ||
* | ||
* This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 | ||
* which is available at https://www.eclipse.org/legal/epl-2.0/ | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* | ||
* Contributors: | ||
* Sebastian Thomschke - initial implementation | ||
*/ | ||
package org.eclipse.tm4e.core.internal.utils; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import java.util.List; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
class StringUtilsTest { | ||
|
||
@Test | ||
void testSplitToArray() { | ||
assertArrayEquals(new String[] { "" }, StringUtils.splitToArray("", '.')); | ||
assertArrayEquals(new String[] { "abc" }, StringUtils.splitToArray("abc", '.')); | ||
assertArrayEquals(new String[] { "abc", "" }, StringUtils.splitToArray("abc.", '.')); | ||
assertArrayEquals(new String[] { "", "abc", "" }, StringUtils.splitToArray(".abc.", '.')); | ||
assertArrayEquals(new String[] { "", "" }, StringUtils.splitToArray(".", '.')); | ||
assertArrayEquals(new String[] { "", "", "", "" }, StringUtils.splitToArray("...", '.')); | ||
assertArrayEquals(new String[] { "1", "2", "3", "4", "5", "6", "7", "8" }, | ||
StringUtils.splitToArray("1.2.3.4.5.6.7.8", '.')); | ||
|
||
// test internal array resize | ||
assertArrayEquals(new String[] { "1", "2", "3", "4", "5", "6", "7", "8", "9" }, | ||
StringUtils.splitToArray("1.2.3.4.5.6.7.8.9", '.')); | ||
} | ||
|
||
@Test | ||
void testSplitToList() { | ||
assertEquals(List.of(""), StringUtils.splitToList("", '.')); | ||
assertEquals(List.of("abc"), StringUtils.splitToList("abc", '.')); | ||
assertEquals(List.of("abc", ""), StringUtils.splitToList("abc.", '.')); | ||
assertEquals(List.of("", "abc", ""), StringUtils.splitToList(".abc.", '.')); | ||
assertEquals(List.of("", ""), StringUtils.splitToList(".", '.')); | ||
assertEquals(List.of("", "", "", ""), StringUtils.splitToList("...", '.')); | ||
assertEquals(List.of("1", "2", "3", "4", "5", "6", "7", "8"), | ||
StringUtils.splitToList("1.2.3.4.5.6.7.8", '.')); | ||
|
||
// test internal array resize | ||
assertEquals(List.of("1", "2", "3", "4", "5", "6", "7", "8", "9"), | ||
StringUtils.splitToList("1.2.3.4.5.6.7.8.9", '.')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters