Skip to content

Commit

Permalink
Merge pull request #3815 from JakubValtar/regex-cache
Browse files Browse the repository at this point in the history
Make the PApplet regex cache LRU
  • Loading branch information
benfry committed Sep 17, 2015
2 parents 0ff60fd + c9367b2 commit 95e4d54
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions core/src/processing/core/PApplet.java
Expand Up @@ -8564,26 +8564,22 @@ static public String[] split(String value, String delim) {
}


static protected HashMap<String, Pattern> matchPatterns;
static protected LinkedHashMap<String, Pattern> matchPatterns;

static Pattern matchPattern(String regexp) {
Pattern p = null;
if (matchPatterns == null) {
matchPatterns = new HashMap<String, Pattern>();
matchPatterns = new LinkedHashMap<String, Pattern>(16, 0.75f, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<String, Pattern> eldest) {
// Limit the number of match patterns at 10 most recently used
return size() == 10;
}
};
} else {
p = matchPatterns.get(regexp);
}
if (p == null) {
if (matchPatterns.size() == 10) {
// Just clear out the match patterns here if more than 10 are being
// used. It's not terribly efficient, but changes that you have >10
// different match patterns are very slim, unless you're doing
// something really tricky (like custom match() methods), in which
// case match() won't be efficient anyway. (And you should just be
// using your own Java code.) The alternative is using a queue here,
// but that's a silly amount of work for negligible benefit.
matchPatterns.clear();
}
p = Pattern.compile(regexp, Pattern.MULTILINE | Pattern.DOTALL);
matchPatterns.put(regexp, p);
}
Expand Down

0 comments on commit 95e4d54

Please sign in to comment.