From 44058634855931371f8b526fbe961dcda5c8d9d0 Mon Sep 17 00:00:00 2001 From: twanvl Date: Wed, 26 Dec 2007 17:43:17 +0000 Subject: [PATCH] Added caching to filter_text script function git-svn-id: svn://svn.code.sf.net/p/magicseteditor/code/trunk@786 0fc631ac-6414-0410-93d0-97cfa31319b6 --- src/script/functions/basic.cpp | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/script/functions/basic.cpp b/src/script/functions/basic.cpp index 09bccb68..f54f40ed 100644 --- a/src/script/functions/basic.cpp +++ b/src/script/functions/basic.cpp @@ -494,14 +494,37 @@ class ScriptFilterRule : public ScriptValue { // Create a regular expression rule for filtering strings ScriptValueP filter_rule(Context& ctx) { - intrusive_ptr ret(new ScriptFilterRule); - // match + // cached? SCRIPT_PARAM_C(String, match); + SCRIPT_PARAM_DEFAULT_N(String, _("in context"), in_context, String()); + + // cache + const int CACHE_SIZE = 6; + struct CacheItem{ + String match, in_context; + intrusive_ptr rule; + }; + static CacheItem cache[CACHE_SIZE]; + static int cache_pos = 0; + // find in cache? + for (int i = 0 ; i < CACHE_SIZE ; ++i) { + if (cache[i].rule && cache[i].match == match && cache[i].in_context == in_context) { + return cache[i].rule; + } + } + // add item + cache[cache_pos].match = match; + cache[cache_pos].in_context = in_context; + cache[cache_pos].rule = intrusive_ptr(new ScriptFilterRule); + intrusive_ptr& ret = cache[cache_pos].rule; + cache_pos = (cache_pos+1) % CACHE_SIZE; + + // match if (!ret->regex.Compile(match, wxRE_ADVANCED)) { throw ScriptError(_("Error while compiling regular expression: '")+match+_("'")); } // in_context - SCRIPT_OPTIONAL_PARAM_N(String, _("in context"), in_context) { + if (!in_context.empty()) { if (!ret->context.Compile(in_context, wxRE_ADVANCED)) { throw ScriptError(_("Error while compiling regular expression: '")+in_context+_("'")); }