Skip to content

Commit

Permalink
#415 - cache calculated matching results
Browse files Browse the repository at this point in the history
  • Loading branch information
hsz committed Aug 20, 2017
1 parent a5b7835 commit 8563e36
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/mobi/hsz/idea/gitignore/util/MatcherUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
package mobi.hsz.idea.gitignore.util;

import com.intellij.util.containers.ContainerUtil;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -39,6 +41,9 @@
* @since 1.3.1
*/
public class MatcherUtil {
/** Stores calculated matching results. */
private static final HashMap<Integer, Boolean> CACHE = ContainerUtil.newHashMap();

/** Private constructor to prevent creating {@link Icons} instance. */
private MatcherUtil() {
}
Expand All @@ -56,16 +61,23 @@ public static boolean match(@Nullable Matcher matcher, @Nullable String path) {
return false;
}

String[] parts = getParts(matcher);
if (parts.length > 0 && !matchAllParts(parts, path)) {
return false;
}
int hashCode = new HashCodeBuilder().append(matcher.pattern()).append(path).toHashCode();

try {
return matcher.reset(path).find();
} catch (StringIndexOutOfBoundsException e) {
return false;
if (!CACHE.containsKey(hashCode)) {
final String[] parts = getParts(matcher);
boolean result = false;

if (parts.length == 0 || matchAllParts(parts, path)) {
try {
result = matcher.reset(path).find();
} catch (StringIndexOutOfBoundsException ignored) {
}
}

CACHE.put(hashCode, result);
}

return CACHE.get(hashCode);
}

/**
Expand Down

0 comments on commit 8563e36

Please sign in to comment.