Skip to content

Commit

Permalink
Use std::shared_ptr for RegexCacheEntry; fixup constructors and destr…
Browse files Browse the repository at this point in the history
…uctors

shared_ptr may be overkill, but unique_ptr does not play nicely with LRU cache.
  • Loading branch information
dcamper committed Jun 19, 2024
1 parent 261c5f3 commit fae681d
Showing 1 changed file with 11 additions and 36 deletions.
47 changes: 11 additions & 36 deletions rtl/eclrtl/eclregex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,7 @@ class RegexCacheEntry
: savedOptions(_options), savedPattern(_pattern, _patternSize), compiledRegex16(_compiledRegex16)
{}

RegexCacheEntry(const RegexCacheEntry & other) = default;

virtual ~RegexCacheEntry() = default;
RegexCacheEntry(const RegexCacheEntry & other) = delete;

static hash64_t hashValue(size32_t patternSize, const char * pattern, uint32_t options)
{
Expand All @@ -188,7 +186,7 @@ class RegexCacheEntry
//---------------------------------------------------------------------------

#define DEFAULT_CACHE_MAX_SIZE 500
static CLRUCache<hash64_t, RegexCacheEntry*> compiledStrRegExprCache(DEFAULT_CACHE_MAX_SIZE);
static CLRUCache<hash64_t, std::shared_ptr<RegexCacheEntry>> compiledStrRegExprCache(DEFAULT_CACHE_MAX_SIZE);
static CriticalSection compiledStrRegExprLock;
static bool compiledCacheEnabled = true;

Expand Down Expand Up @@ -372,10 +370,6 @@ class CCompiledStrRegExpr : implements ICompiledStrRegExpr
: compiledRegex(cacheEntry.getCompiledRegex8()), isUTF8Enabled(_enableUTF8)
{}

CCompiledStrRegExpr(const CCompiledStrRegExpr & other) = default;

virtual ~CCompiledStrRegExpr() = default;

std::shared_ptr<pcre2_code_8> getCompiledRegex() const { return compiledRegex; }

//ICompiledStrRegExpr
Expand Down Expand Up @@ -527,16 +521,12 @@ CCompiledStrRegExpr* fetchOrCreateCompiledStrRegExpr(int _regexLength, const cha
{
CCompiledStrRegExpr * compiledObjPtr = nullptr;
uint32_t options = (_isCaseSensitive ? 0 : PCRE2_CASELESS);
hash64_t regexHash = HASH64_INIT;

// Create a hash of the regex pattern plus the options
regexHash = rtlHash64Data(_regexLength, _regex, regexHash);
regexHash = rtlHash64Data(sizeof(options), &options, regexHash);
hash64_t regexHash = RegexCacheEntry::hashValue(_regexLength, _regex, options);

// Check the cache
{
CriticalBlock lock(compiledStrRegExprLock);
RegexCacheEntry * cacheEntry = compiledStrRegExprCache.get(regexHash);
RegexCacheEntry * cacheEntry = compiledStrRegExprCache.get(regexHash).get();

if (cacheEntry && cacheEntry->hasSamePattern(_regexLength, _regex, options))
{
Expand All @@ -547,8 +537,7 @@ CCompiledStrRegExpr* fetchOrCreateCompiledStrRegExpr(int _regexLength, const cha
// Create a new compiled pattern object
compiledObjPtr = new CCompiledStrRegExpr(_regexLength, _regex, _isCaseSensitive, false);
// Create a cache entry for the new object
cacheEntry = new RegexCacheEntry(_regexLength, _regex, options, compiledObjPtr->getCompiledRegex());
compiledStrRegExprCache.set(regexHash, cacheEntry);
compiledStrRegExprCache.set(regexHash, std::make_shared<RegexCacheEntry>(_regexLength, _regex, options, compiledObjPtr->getCompiledRegex()));
}

return compiledObjPtr;
Expand Down Expand Up @@ -607,16 +596,12 @@ CCompiledStrRegExpr* fetchOrCreateCompiledU8StrRegExpr(int _regexLength, const c
CCompiledStrRegExpr * compiledObjPtr = nullptr;
unsigned int regexSize = rtlUtf8Size(_regexLength, _regex);
uint32_t options = PCRE2_UTF | (_isCaseSensitive ? 0 : PCRE2_CASELESS);
hash64_t regexHash = HASH64_INIT;

// Create a hash of the regex pattern plus the options
regexHash = rtlHash64Data(regexSize, _regex, regexHash);
regexHash = rtlHash64Data(sizeof(options), &options, regexHash);
hash64_t regexHash = RegexCacheEntry::hashValue(regexSize, _regex, options);

// Check the cache
{
CriticalBlock lock(compiledStrRegExprLock);
RegexCacheEntry * cacheEntry = compiledStrRegExprCache.get(regexHash);
RegexCacheEntry * cacheEntry = compiledStrRegExprCache.get(regexHash).get();

if (cacheEntry && cacheEntry->hasSamePattern(regexSize, _regex, options))
{
Expand All @@ -627,8 +612,7 @@ CCompiledStrRegExpr* fetchOrCreateCompiledU8StrRegExpr(int _regexLength, const c
// Create a new compiled pattern object
compiledObjPtr = new CCompiledStrRegExpr(_regexLength, _regex, _isCaseSensitive, true);
// Create a cache entry for the new object
cacheEntry = new RegexCacheEntry(regexSize, _regex, options, compiledObjPtr->getCompiledRegex());
compiledStrRegExprCache.set(regexHash, cacheEntry);
compiledStrRegExprCache.set(regexHash, std::make_shared<RegexCacheEntry>(regexSize, _regex, options, compiledObjPtr->getCompiledRegex()));
}

return compiledObjPtr;
Expand Down Expand Up @@ -767,14 +751,10 @@ class CCompiledUStrRegExpr : implements ICompiledUStrRegExpr
compiledRegex = std::shared_ptr<pcre2_code_16>(newCompiledRegex, pcre2_code_free_16);
}

CCompiledUStrRegExpr(const CCompiledUStrRegExpr& other) = default;

CCompiledUStrRegExpr(const RegexCacheEntry& cacheEntry)
: compiledRegex(cacheEntry.getCompiledRegex16())
{}

virtual ~CCompiledUStrRegExpr() = default;

std::shared_ptr<pcre2_code_16> getCompiledRegex() const { return compiledRegex; }

void replace(size32_t & outlen, UChar * & out, size32_t slen, const UChar * str, size32_t rlen, UChar const * replace) const
Expand Down Expand Up @@ -917,16 +897,12 @@ CCompiledUStrRegExpr* fetchOrCreateCompiledUStrRegExpr(int _regexLength, const U
CCompiledUStrRegExpr * compiledObjPtr = nullptr;
unsigned int regexSize = _regexLength * sizeof(UChar);
uint32_t options = PCRE2_UCP | (_isCaseSensitive ? 0 : PCRE2_CASELESS);
hash64_t regexHash = HASH64_INIT;

// Create a hash of the regex pattern plus the options
regexHash = rtlHash64Data(regexSize, _regex, regexHash);
regexHash = rtlHash64Data(sizeof(options), &options, regexHash);
hash64_t regexHash = RegexCacheEntry::hashValue(regexSize, reinterpret_cast<const char *>(_regex), options);

// Check the cache
{
CriticalBlock lock(compiledStrRegExprLock);
RegexCacheEntry * cacheEntry = compiledStrRegExprCache.get(regexHash);
RegexCacheEntry * cacheEntry = compiledStrRegExprCache.get(regexHash).get();

if (cacheEntry && cacheEntry->hasSamePattern(regexSize, reinterpret_cast<const char *>(_regex), options))
{
Expand All @@ -937,8 +913,7 @@ CCompiledUStrRegExpr* fetchOrCreateCompiledUStrRegExpr(int _regexLength, const U
// Create a new compiled pattern object
compiledObjPtr = new CCompiledUStrRegExpr(_regexLength, _regex, _isCaseSensitive);
// Create a cache entry for the new object
cacheEntry = new RegexCacheEntry(regexSize, reinterpret_cast<const char *>(_regex), options, compiledObjPtr->getCompiledRegex());
compiledStrRegExprCache.set(regexHash, cacheEntry);
compiledStrRegExprCache.set(regexHash, std::make_shared<RegexCacheEntry>(regexSize, reinterpret_cast<const char *>(_regex), options, compiledObjPtr->getCompiledRegex()));
}

return compiledObjPtr;
Expand Down

0 comments on commit fae681d

Please sign in to comment.