Skip to content

Commit

Permalink
let RegexMatchCache store only unique hashes of regexes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
yfeldblum authored and facebook-github-bot committed Oct 8, 2024
1 parent ede5900 commit 591b7e0
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 12 deletions.
2 changes: 1 addition & 1 deletion fb303/CallbackValuesMap-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ void CallbackValuesMap<T>::getKeys(std::vector<std::string>* keys) const {
template <typename T>
void CallbackValuesMap<T>::getRegexKeys(
std::vector<std::string>& 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);
}
Expand Down
5 changes: 3 additions & 2 deletions fb303/CallbackValuesMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,13 @@ class CallbackValuesMap {
/* Returns the keys in the map that matches regex pattern */
void getRegexKeys(std::vector<std::string>& 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<std::string>& 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 */
Expand Down
7 changes: 4 additions & 3 deletions fb303/ServiceData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -486,11 +486,12 @@ void ServiceData::getRegexCounters(
void ServiceData::getRegexCountersOptimized(
map<string, int64_t>& output,
const string& regex) const {
const auto key = folly::RegexMatchCache::regex_key_and_view(regex);
const auto now = folly::RegexMatchCache::clock::now();
std::vector<std::string> 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);
}

Expand Down
2 changes: 1 addition & 1 deletion fb303/detail/QuantileStatMap-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ void BasicQuantileStatMap<ClockT>::getKeys(
template <typename ClockT>
void BasicQuantileStatMap<ClockT>::getRegexKeys(
std::vector<std::string>& 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);
}
Expand Down
5 changes: 3 additions & 2 deletions fb303/detail/QuantileStatMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,13 @@ class BasicQuantileStatMap {
/* Returns the keys in the map that matches regex pattern */
void getRegexKeys(std::vector<std::string>& 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<std::string>& keys,
const std::string& regex,
const folly::RegexMatchCache::regex_key_and_view& regex,
const folly::RegexMatchCache::time_point now) const;

size_t getNumKeys() const;
Expand Down
2 changes: 1 addition & 1 deletion fb303/detail/RegexUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace facebook::fb303::detail {
void cachedFindMatchesCopyUnderSharedLock(
std::vector<std::string>& 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());
Expand Down
4 changes: 2 additions & 2 deletions fb303/detail/RegexUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ void cachedClearStrings(Map& map) {
void cachedFindMatchesCopyUnderSharedLock(
std::vector<std::string>& out,
folly::RegexMatchCache const& cache,
std::string_view regex,
folly::RegexMatchCacheKeyAndView const& regex,
folly::RegexMatchCache::time_point now);

template <typename SyncMap>
void cachedFindMatches(
std::vector<std::string>& 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)) {
Expand Down

0 comments on commit 591b7e0

Please sign in to comment.