From c9367b24de11c982ff888909d808bcd93f015ef4 Mon Sep 17 00:00:00 2001 From: Jakub Valtar Date: Thu, 17 Sep 2015 15:58:26 -0400 Subject: [PATCH] Make the PApplet regex cache LRU --- core/src/processing/core/PApplet.java | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/core/src/processing/core/PApplet.java b/core/src/processing/core/PApplet.java index 75f816a0e8..dd97d04180 100644 --- a/core/src/processing/core/PApplet.java +++ b/core/src/processing/core/PApplet.java @@ -8564,26 +8564,22 @@ static public String[] split(String value, String delim) { } - static protected HashMap matchPatterns; + static protected LinkedHashMap matchPatterns; static Pattern matchPattern(String regexp) { Pattern p = null; if (matchPatterns == null) { - matchPatterns = new HashMap(); + matchPatterns = new LinkedHashMap(16, 0.75f, true) { + @Override + protected boolean removeEldestEntry(Map.Entry 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); }