From 2a4de4f4200cb5eace487e73b63628a5f0377d25 Mon Sep 17 00:00:00 2001 From: xwcai98 Date: Tue, 19 Aug 2025 14:44:02 +0800 Subject: [PATCH] Clear Regexp cache Signed-off-by: xwcai98 Change-Id: Idf4ab00852bfbc0af72b5547b855ee3ac22dc80e --- ecmascript/builtins/builtins_regexp.cpp | 28 +++++++++++++++++++++++++ ecmascript/builtins/builtins_regexp.h | 3 ++- ecmascript/global_env.cpp | 1 + 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/ecmascript/builtins/builtins_regexp.cpp b/ecmascript/builtins/builtins_regexp.cpp index a7e470d023..254e994195 100644 --- a/ecmascript/builtins/builtins_regexp.cpp +++ b/ecmascript/builtins/builtins_regexp.cpp @@ -2583,6 +2583,34 @@ JSTaggedValue RegExpExecResultCache::CreateCacheTable(JSThread *thread) return JSTaggedValue(table); } +void RegExpExecResultCache::ClearCache(JSThread* thread, JSHandle cache) +{ + if (cache->IsUndefined()) { + return; + } + JSHandle regexpCache(cache); + if (regexpCache->GetCacheCount() == 0) { + return; + } + if (regexpCache->GetNeedUpdateGlobal()) { + JSHandle env = thread->GetGlobalEnv(); + ASSERT(!env.GetTaggedValue().IsHole()); + const int lastMatchIndex = regexpCache->GetLastMatchGlobalTableIndex(); + ASSERT(lastMatchIndex != -1); + env->SetRegExpGlobalResult(thread, regexpCache->Get(thread, lastMatchIndex + CAPTURE_SIZE)); + } + regexpCache->SetLargeStrCount(thread, DEFAULT_LARGE_STRING_COUNT); + regexpCache->SetConflictCount(thread, DEFAULT_CONFLICT_COUNT); + regexpCache->SetStrLenThreshold(thread, 0); + regexpCache->SetHitCount(thread, 0); + regexpCache->SetCacheCount(thread, 0); + regexpCache->SetLastMatchGlobalTableIndex(thread, DEFAULT_LAST_MATCH_INDEX); + regexpCache->SetUseLastMatch(thread, false); + regexpCache->SetNeedUpdateGlobal(thread, false); + std::fill_n(regexpCache->GetData() + CACHE_TABLE_HEADER_SIZE, regexpCache->GetLength() - CACHE_TABLE_HEADER_SIZE, + JSTaggedValue::Undefined().GetRawData()); +} + void RegExpExecResultCache::AddResultInCache(JSThread *thread, JSHandle cache, const JSHandle regexp, const JSHandle input, diff --git a/ecmascript/builtins/builtins_regexp.h b/ecmascript/builtins/builtins_regexp.h index 285fde67fd..8040df21ca 100644 --- a/ecmascript/builtins/builtins_regexp.h +++ b/ecmascript/builtins/builtins_regexp.h @@ -225,6 +225,7 @@ public: return reinterpret_cast(object); } static JSTaggedValue CreateCacheTable(JSThread *thread); + static void ClearCache(JSThread* thread, JSHandle cache); // extend as an additional parameter to judge cached template JSTaggedValue FindCachedResult(JSThread *thread, const JSHandle input, @@ -350,7 +351,7 @@ private: static constexpr int DEFAULT_LARGE_STRING_COUNT = 10; static constexpr int DEFAULT_CONFLICT_COUNT = 100; static constexpr int INITIAL_CACHE_NUMBER = 0x10; - static constexpr int DEFAULT_CACHE_NUMBER = 0x1000; + static constexpr int DEFAULT_CACHE_NUMBER = 0x100; static constexpr int DEFAULT_LAST_MATCH_INDEX = -1; static constexpr int CACHE_COUNT_INDEX = 0; static constexpr int CACHE_HIT_COUNT_INDEX = 1; diff --git a/ecmascript/global_env.cpp b/ecmascript/global_env.cpp index 156b232c20..a3b5efa7f5 100644 --- a/ecmascript/global_env.cpp +++ b/ecmascript/global_env.cpp @@ -118,6 +118,7 @@ void GlobalEnv::NotifyArrayPrototypeChangedGuardians(JSThread *thread, JSHandle< void GlobalEnv::ClearCache(JSThread *thread) const { builtins::StringSplitResultCache::ClearCache(thread, GetStringSplitResultCache()); + builtins::RegExpExecResultCache::ClearCache(thread, GetRegExpCache()); } GlobalEnvField GetBuildinTypedArrayHClassOnHeapIndex(JSType jSType) -- Gitee