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

Performance improvements #3808

Merged
merged 21 commits into from
Nov 8, 2023
Merged
Changes from 1 commit
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add regex caching
JohnMcPMS committed Nov 3, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 1c1e50aabfc44b5ce848aa473038ca3a1f062aa0
58 changes: 57 additions & 1 deletion src/AppInstallerCommonCore/Regex.cpp
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@
#include "Public/winget/Regex.h"
#include "Public/AppInstallerErrors.h"
#include "Public/AppInstallerLogging.h"
#include "Public/AppInstallerLanguageUtilities.h"

#define WINGET_THROW_REGEX_ERROR_IF_FAILED(_err_,_func_) \
if (U_FAILURE(_err_)) \
@@ -20,6 +21,61 @@ namespace AppInstaller::Regex
using uregex_ptr = wil::unique_any<URegularExpression*, decltype(uregex_close), uregex_close>;
using utext_ptr = wil::unique_any<UText*, decltype(utext_close), utext_close>;

static std::unique_ptr<impl> Create(std::string_view pattern, Options options)
Copy link
Member

Choose a reason for hiding this comment

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

Nit: can you add a comment stating this does caching to avoid recompiling the regex, please?

{
struct key
{
std::string pattern;
Options options = Options::None;

bool operator<(const key& other) const
{
if (pattern < other.pattern)
{
return true;
}
else if (pattern == other.pattern)
{
return ToIntegral(options) < ToIntegral(other.options);
}
else
{
return false;
}
}
};

static std::map<key, impl> s_regex_cache;
static wil::srwlock s_regex_cache_lock;

key requested;
requested.pattern = pattern;
requested.options = options;

{
// Attempt to find in the cache
auto sharedLock = s_regex_cache_lock.lock_shared();

auto itr = s_regex_cache.find(requested);
if (itr != s_regex_cache.end())
{
return std::make_unique<impl>(itr->second);
}
}

auto exclusiveLock = s_regex_cache_lock.lock_exclusive();

auto itr = s_regex_cache.find(requested);
if (itr != s_regex_cache.end())
{
return std::make_unique<impl>(itr->second);
}
else
{
return std::make_unique<impl>(s_regex_cache.emplace(std::move(requested), impl{ pattern, options }).first->second);
}
}

impl(std::string_view pattern, Options options)
{
UErrorCode uec = U_ZERO_ERROR;
@@ -167,7 +223,7 @@ namespace AppInstaller::Regex

Expression::Expression() = default;

Expression::Expression(std::string_view pattern, Options options) : pImpl(std::make_unique<impl>(pattern, options)) {}
Expression::Expression(std::string_view pattern, Options options) : pImpl(impl::Create(pattern, options)) {}

Expression::Expression(const Expression& other)
{