Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HPCC-31921 Add caching of regex compiled search patterns #18748

Merged

Conversation

dcamper
Copy link
Contributor

@dcamper dcamper commented Jun 10, 2024

Type of change:

  • This change is a bug fix (non-breaking change which fixes an issue).
  • This change is a new feature (non-breaking change which adds functionality).
  • This change improves the code (refactor or other change that does not change the functionality)
  • This change fixes warnings (the fix does not alter the functionality or the generated code)
  • This change is a breaking change (fix or feature that will cause existing behavior to change).
  • This change alters the query API (existing queries will have to be recompiled)

Checklist:

  • My code follows the code style of this project.
    • My code does not create any new warnings from compiler, build system, or lint.
  • The commit message is properly formatted and free of typos.
    • The commit message title makes sense in a changelog, by itself.
    • The commit is signed.
  • My change requires a change to the documentation.
    • I have updated the documentation accordingly, or...
    • I have created a JIRA ticket to update the documentation.
    • Any new interfaces or exported functions are appropriately commented.
  • I have read the CONTRIBUTORS document.
  • The change has been fully tested:
    • I have added tests to cover my changes.
    • All new and existing tests passed.
    • I have checked that this change does not introduce memory leaks.
    • I have used Valgrind or similar tools to check for potential issues.
  • I have given due consideration to all of the following potential concerns:
    • Scalability
    • Performance
    • Security
    • Thread-safety
    • Cloud-compatibility
    • Premature optimization
    • Existing deployed queries will not be broken
    • This change fixes the problem, not just the symptom
    • The target branch of this pull request is appropriate for such a change.
  • There are no similar instances of the same problem that should be addressed
    • I have addressed them here
    • I have raised JIRA issues to address them separately
  • This is a user interface / front-end modification
    • I have tested my changes in multiple modern browsers
    • The component(s) render as expected

Smoketest:

  • Send notifications about my Pull Request position in Smoketest queue.
  • Test my draft Pull Request.

Testing:

Manual execution of regression tests. Performance testing with pathological code (wall clock timings).

@dcamper dcamper requested a review from ghalliday June 10, 2024 19:46
Copy link

Jira Issue: https://hpccsystems.atlassian.net//browse/HPCC-31921

Jirabot Action Result:
Workflow Transition: Merge Pending
Updated PR

@dcamper dcamper force-pushed the hpcc-31921-regex-caching-master branch from e6c944b to da6cb86 Compare June 11, 2024 12:24
Copy link
Member

@ghalliday ghalliday left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dcamper looks good. One functional question, and a couple of performance comments.


recentList.erase(foundIter->second.second);
recentList.push_front(key);
lookupMap[key] = {foundIter->second.first, recentList.begin()};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than replacing the entry you can just update the pointer in the LRU list.

foundIter->second.second = recentList.begin();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch; fixed.

{
recentList.erase(foundIter->second.second);
recentList.push_front(key);
lookupMap[key] = {value, recentList.begin()};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foundIter->second = ...

is probably more efficient, but it shouldn't be hit in the current usage

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Agreed that in the current regex scenario this block won't be hit. I really struggled with creating a general purpose feature at this level of code versus just what I needed, and reigning in my desire to add another version of this LRU cache that would be more efficient for non-pointer objects.

// Check the cache
{
CriticalBlock lock(compiledStrRegExprLock);
compiledObjPtr = dynamic_cast<CCompiledStrRegExpr*>(compiledStrRegExprCache.get(regexHash));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the danger/likelihood of false positive matches caused by clashing hash codes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do the math, but the likelihood is "very very low" considering cache size is only 500. It is never impossible to prevent collisions on a hash, but our key space is huge compared to the number of cached items.

Earlier versions of this code used separate caches for the three data types (string, UTF-8 and unicode). That would reduce the possibility of collision but not enough, IMO. Separate caches would also eliminate the need for the added parent class and the virtualization of destructors.

}

CCompiledStrRegExpr(CCompiledStrRegExpr & other) = default; // Note non-const argument
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the need for the non-const argument?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The std::shared_ptr in other apparently needs to be updated (the compiler complained about the original const version I had). If you know of a pattern where I can make other const, please tell me!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, never mind. I think I had a different underlying issue at the time. All copy constructors now accept const arguments.

@@ -2796,7 +2796,7 @@ IHqlExpression * foldConstantOperator(IHqlExpression * expr, unsigned foldOption
StringBuffer pattern, search;
v0->getUTF8Value(pattern);
v1->getUTF8Value(search);
ICompiledStrRegExpr * compiled = rtlCreateCompiledU8StrRegExpr(pattern, !expr->hasAttribute(noCaseAtom));
ICompiledStrRegExpr * compiled = rtlCreateCompiledU8StrRegExpr(pattern.length(), pattern, !expr->hasAttribute(noCaseAtom));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right. I think it is passing a size to a length parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent catch; fixed.

// Check the cache
{
CriticalBlock lock(compiledStrRegExprLock);
compiledObjPtr = dynamic_cast<CCompiledStrRegExpr*>(compiledStrRegExprCache.get(regexHash));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My recollection is that a dynamic cast is significantly less efficient than a static cast, or a virtual method call. It might be worth considering if it can be removed, but probably not worth it for this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is less efficient, but safer; I blame muscle memory for this one. I've swapped in static_cast because "I know what I'm doing" (hahaha). static_cast is resolved at compile time.

@dcamper dcamper requested a review from ghalliday June 13, 2024 12:34
Copy link
Member

@ghalliday ghalliday left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dcamper looks good. My only concerns are
i) Please can you include a regression test (that is ok to be released open source) and with a smaller number of iterations.
ii) There is no way to configure on/off. I think that's probably ok.
iii) How likely are there to be clashes in hash codes. How much less efficient is a hash table that stores strings? The problem is that mismatches would be silent, and could cause chaos.

system/jlib/jhash.hpp Show resolved Hide resolved
@dcamper
Copy link
Contributor Author

dcamper commented Jun 18, 2024

iii) How likely are there to be clashes in hash codes. How much less efficient is a hash table that stores strings? The problem is that mismatches would be silent, and could cause chaos.

That cached object now retains the original regex pattern and the options. If the object is retrieved from the cache via the hash key, then the saved pattern and options are compared to ensure that there is no collision.

@dcamper dcamper requested a review from ghalliday June 18, 2024 15:56
Copy link
Member

@ghalliday ghalliday left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it looks functionally correct. A couple of suggestions to discuss about the structure.

if (foundIter == lookupMap.end())
return nullptr;

recentList.erase(foundIter->second.second);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

recentList.splice(0, recentList, foundIter->second.second)

is probably more efficient - it should avoid cloning the list element. From the docs that form should be valid if source and target are the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Changed here and in the set() method as well.

rtl/eclrtl/eclregex.cpp Show resolved Hide resolved
* <Software/Globals> section for an optional "regex" subsection with a "cacheSize" attribute
* By default, the maximum cache size is set to 500 patterns. Override with 0 to disable caching.
*/
void initMaxCacheSize()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add static to avoid possible namespace pollution

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch; fixed.

@dcamper dcamper requested a review from ghalliday June 19, 2024 16:08
Copy link
Member

@ghalliday ghalliday left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dcamper looks very close. A few suggests to clean up the code, and I think this will currently leak RegexCacheEntry objects.

: savedOptions(_options), savedPattern(_pattern, _patternSize), compiledRegex16(_compiledRegex16)
{}

RegexCacheEntry(const RegexCacheEntry & other) = default;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can now be = delete


RegexCacheEntry(const RegexCacheEntry & other) = default;

virtual ~RegexCacheEntry() = default;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can now be removed.


compiledRegex = pcre2_compile_8((PCRE2_SPTR8)_regex, regexSize, options, &errNum, &errOffset, pcre2CompileContext8);
CCompiledStrRegExpr(const CCompiledStrRegExpr & other) = default;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can now be = delete? or remove and just use the default.

failWithPCRE2Error(errNum, "Error in regex pattern: ", _regex, errOffset);
}
}
virtual ~CCompiledStrRegExpr() = default;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can also probably now go away

{
CCompiledStrRegExpr * compiledObjPtr = nullptr;
uint32_t options = (_isCaseSensitive ? 0 : PCRE2_CASELESS);
hash64_t regexHash = HASH64_INIT;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleanup:
regexHash = RegexHashEntry::hashValue(_regexLength, _regex, options);
and elsewhere

while (lookupMap.size() > maxCacheSize)
{
lookupMap.erase(recentList.back());
recentList.pop_back();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When will the cache entry be deleted? Should it be a map to a std::unique_ptr?

@dcamper dcamper force-pushed the hpcc-31921-regex-caching-master branch from 1d747f5 to 261c5f3 Compare June 19, 2024 17:02
@dcamper dcamper requested a review from ghalliday June 19, 2024 18:33
Copy link
Member

@ghalliday ghalliday left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dcamper looks good and also a clean implementation. Thanks, please squash and I will merge.

@dcamper dcamper force-pushed the hpcc-31921-regex-caching-master branch from fae681d to 7637764 Compare June 20, 2024 11:54
@dcamper dcamper force-pushed the hpcc-31921-regex-caching-master branch from 7637764 to 7997a0a Compare June 20, 2024 11:55
@dcamper
Copy link
Contributor Author

dcamper commented Jun 20, 2024

Clean implementation with a great deal of help. Thank you!

Rebased to latest master branch and squashed. Please merge.

@ghalliday ghalliday merged commit 683bc67 into hpcc-systems:master Jun 20, 2024
50 of 52 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants