From 591b7e0edd792794c9521e1d77c8e550e70bd200 Mon Sep 17 00:00:00 2001 From: Yedidya Feldblum Date: Tue, 8 Oct 2024 03:19:33 -0700 Subject: [PATCH] let RegexMatchCache store only unique hashes of regexes Summary: `RegexMatchCache` does not need to store full regex strings. It needs only some unique identifier of the regex, and cryptographic hashes are commonly used for this purpose. Reviewed By: mdas7 Differential Revision: D63338698 fbshipit-source-id: af9093b1db4bff2dc30607e7bb31b743ee7510d9 --- fb303/CallbackValuesMap-inl.h | 2 +- fb303/CallbackValuesMap.h | 5 +++-- fb303/ServiceData.cpp | 7 ++++--- fb303/detail/QuantileStatMap-inl.h | 2 +- fb303/detail/QuantileStatMap.h | 5 +++-- fb303/detail/RegexUtil.cpp | 2 +- fb303/detail/RegexUtil.h | 4 ++-- 7 files changed, 15 insertions(+), 12 deletions(-) diff --git a/fb303/CallbackValuesMap-inl.h b/fb303/CallbackValuesMap-inl.h index 34cebd018..9981a0783 100644 --- a/fb303/CallbackValuesMap-inl.h +++ b/fb303/CallbackValuesMap-inl.h @@ -77,7 +77,7 @@ void CallbackValuesMap::getKeys(std::vector* keys) const { template void CallbackValuesMap::getRegexKeys( std::vector& keys, - const std::string& regex, + const folly::RegexMatchCache::regex_key_and_view& regex, const folly::RegexMatchCache::time_point now) const { detail::cachedFindMatches(keys, callbackMap_, regex, now); } diff --git a/fb303/CallbackValuesMap.h b/fb303/CallbackValuesMap.h index cbf330f1d..6d3924c49 100644 --- a/fb303/CallbackValuesMap.h +++ b/fb303/CallbackValuesMap.h @@ -59,12 +59,13 @@ class CallbackValuesMap { /* Returns the keys in the map that matches regex pattern */ void getRegexKeys(std::vector& keys, const std::string& regex) const { + const auto key = folly::RegexMatchCache::regex_key_and_view(regex); const auto now = folly::RegexMatchCache::clock::now(); - getRegexKeys(keys, regex, now); + getRegexKeys(keys, key, now); } void getRegexKeys( std::vector& keys, - const std::string& regex, + const folly::RegexMatchCache::regex_key_and_view& regex, const folly::RegexMatchCache::time_point now) const; /** Returns the number of keys present in the map */ diff --git a/fb303/ServiceData.cpp b/fb303/ServiceData.cpp index f5420047e..dba4c25d6 100644 --- a/fb303/ServiceData.cpp +++ b/fb303/ServiceData.cpp @@ -486,11 +486,12 @@ void ServiceData::getRegexCounters( void ServiceData::getRegexCountersOptimized( map& output, const string& regex) const { + const auto key = folly::RegexMatchCache::regex_key_and_view(regex); const auto now = folly::RegexMatchCache::clock::now(); std::vector keys; - detail::cachedFindMatches(keys, counters_, regex, now); - quantileMap_.getRegexKeys(keys, regex, now); - dynamicCounters_.getRegexKeys(keys, regex, now); + detail::cachedFindMatches(keys, counters_, key, now); + quantileMap_.getRegexKeys(keys, key, now); + dynamicCounters_.getRegexKeys(keys, key, now); getSelectedCounters(output, keys); } diff --git a/fb303/detail/QuantileStatMap-inl.h b/fb303/detail/QuantileStatMap-inl.h index 4cc87a73b..b8177ba69 100644 --- a/fb303/detail/QuantileStatMap-inl.h +++ b/fb303/detail/QuantileStatMap-inl.h @@ -179,7 +179,7 @@ void BasicQuantileStatMap::getKeys( template void BasicQuantileStatMap::getRegexKeys( std::vector& keys, - const std::string& regex, + const folly::RegexMatchCache::regex_key_and_view& regex, const folly::RegexMatchCache::time_point now) const { detail::cachedFindMatches(keys, counters_, regex, now); } diff --git a/fb303/detail/QuantileStatMap.h b/fb303/detail/QuantileStatMap.h index af91fd154..84e6c325e 100644 --- a/fb303/detail/QuantileStatMap.h +++ b/fb303/detail/QuantileStatMap.h @@ -78,12 +78,13 @@ class BasicQuantileStatMap { /* Returns the keys in the map that matches regex pattern */ void getRegexKeys(std::vector& keys, const std::string& regex) const { + const auto key = folly::RegexMatchCache::regex_key_and_view(regex); const auto now = folly::RegexMatchCache::clock::now(); - getRegexKeys(keys, regex, now); + getRegexKeys(keys, key, now); } void getRegexKeys( std::vector& keys, - const std::string& regex, + const folly::RegexMatchCache::regex_key_and_view& regex, const folly::RegexMatchCache::time_point now) const; size_t getNumKeys() const; diff --git a/fb303/detail/RegexUtil.cpp b/fb303/detail/RegexUtil.cpp index 926e108a2..389c82e61 100644 --- a/fb303/detail/RegexUtil.cpp +++ b/fb303/detail/RegexUtil.cpp @@ -23,7 +23,7 @@ namespace facebook::fb303::detail { void cachedFindMatchesCopyUnderSharedLock( std::vector& out, folly::RegexMatchCache const& cache, - std::string_view const regex, + folly::RegexMatchCacheKeyAndView const& regex, folly::RegexMatchCache::time_point const now) { auto const matches = cache.findMatchesUnsafe(regex, now); folly::grow_capacity_by(out, matches.size()); diff --git a/fb303/detail/RegexUtil.h b/fb303/detail/RegexUtil.h index 5709c35cd..aa801de6b 100644 --- a/fb303/detail/RegexUtil.h +++ b/fb303/detail/RegexUtil.h @@ -65,14 +65,14 @@ void cachedClearStrings(Map& map) { void cachedFindMatchesCopyUnderSharedLock( std::vector& out, folly::RegexMatchCache const& cache, - std::string_view regex, + folly::RegexMatchCacheKeyAndView const& regex, folly::RegexMatchCache::time_point now); template void cachedFindMatches( std::vector& out, SyncMap& map, - std::string_view const regex, + folly::RegexMatchCacheKeyAndView const& regex, folly::RegexMatchCache::time_point const now) { auto r = map.rlock(); if (!r->matches.isReadyToFindMatches(regex)) {