Skip to content

Commit

Permalink
Improve VariableMap.forBytes performance
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=190344384
  • Loading branch information
lukesandberg authored and Tyler Breisacher committed Mar 27, 2018
1 parent 01013ef commit eaa0edf
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions src/com/google/javascript/jscomp/VariableMap.java
Expand Up @@ -21,7 +21,6 @@


import com.google.common.annotations.GwtIncompatible; import com.google.common.annotations.GwtIncompatible;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableBiMap; import com.google.common.collect.ImmutableBiMap;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap; import com.google.common.collect.ImmutableSortedMap;
Expand Down Expand Up @@ -122,23 +121,30 @@ public byte[] toBytes() {
return baos.toByteArray(); return baos.toByteArray();
} }


@GwtIncompatible("com.google.common.base.Splitter.onPattern()")
private static final Splitter LINE_SPLITTER
= Splitter.onPattern("\\r?\\n").omitEmptyStrings();

/** /**
* Deserializes the variable map from a byte array returned by * Deserializes the variable map from a byte array returned by
* {@link #toBytes()}. * {@link #toBytes()}.
*/ */
@GwtIncompatible("com.google.common.base.Splitter.onPattern()") @GwtIncompatible("com.google.common.base.Splitter.onPattern()")
public static VariableMap fromBytes(byte[] bytes) throws ParseException { public static VariableMap fromBytes(byte[] bytes) throws ParseException {
Iterable<String> lines = LINE_SPLITTER.split( String string = new String(bytes, UTF_8);
new String(bytes, UTF_8));

ImmutableMap.Builder<String, String> map = ImmutableMap.builder(); ImmutableMap.Builder<String, String> map = ImmutableMap.builder();

int startOfLine = 0;
for (String line : lines) { while (startOfLine < string.length()) {
int pos = findIndexOfChar(line, SEPARATOR); int newLine = string.indexOf('\n', startOfLine);
if (newLine == -1) {
newLine = string.length();
}
int endOfLine = newLine;
if (string.charAt(newLine - 1) == '\r') {
newLine--;
}
String line = string.substring(startOfLine, newLine);
startOfLine = endOfLine + 1; // update index for next iteration
if (line.isEmpty()) {
continue;
}
int pos = findIndexOfUnescapedChar(line, SEPARATOR);
if (pos <= 0) { if (pos <= 0) {
throw new ParseException("Bad line: " + line, 0); throw new ParseException("Bad line: " + line, 0);
} }
Expand All @@ -155,23 +161,31 @@ private static String escape(String value) {
.replace("\n", "\\n"); .replace("\n", "\\n");
} }


private static int findIndexOfChar(String value, char stopChar) { private static int findIndexOfUnescapedChar(String value, char stopChar) {
int len = value.length(); int len = value.length();
for (int i = 0; i < len; i++) { for (int i = 0; i < len; ) {
char c = value.charAt(i); int stopCharIndex = value.indexOf(stopChar, i);
if (c == '\\' && ++i < len) { if (stopCharIndex == -1) {
c = value.charAt(i); return -1;
} else if (c == stopChar){ }
return i; if (value.charAt(stopCharIndex - 1) != '\\') {
// it isn't escaped, return
return stopCharIndex;
} }
i = stopCharIndex + 1;
} }
return -1; return -1;
} }


private static String unescape(CharSequence value) { private static String unescape(String value) {
StringBuilder sb = new StringBuilder(); int slashIndex = value.indexOf('\\');
if (slashIndex == -1) {
return value;
}
StringBuilder sb = new StringBuilder(value.length() - 1);
sb.append(value, 0, slashIndex);
int len = value.length(); int len = value.length();
for (int i = 0; i < len; i++) { for (int i = slashIndex; i < len; i++) {
char c = value.charAt(i); char c = value.charAt(i);
if (c == '\\' && ++i < len) { if (c == '\\' && ++i < len) {
c = value.charAt(i); c = value.charAt(i);
Expand Down

0 comments on commit eaa0edf

Please sign in to comment.