-
Notifications
You must be signed in to change notification settings - Fork 5.5k
stats: cache the results of the stats-matcher class. #6207
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
Merged
jmarantz
merged 19 commits into
envoyproxy:master
from
jmarantz:cache-stats-matcher-results
Mar 19, 2019
Merged
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
fd51c80
cache the results of the stats-matcher class.
jmarantz c0b0747
Merge branch 'master' into cache-stats-matcher-results
jmarantz 9c3b3ef
Refactor the tests a bit, and move the main rejection-cache into the …
jmarantz a1bbd88
Use set of explicitly managed char* rather than set of std::string so…
jmarantz 43b10da
Test that accepts-all and rejects-all scenarios don't call rejects().
jmarantz b3f8d58
Merge branch 'master' into cache-stats-matcher-results
jmarantz 9fd2905
Add more comments around the lifetime & function of the ThreadLocalSt…
jmarantz 6185123
Add more comments, and disallow copy constructors for StringSet.
jmarantz 75f99b6
Merge branch 'master' into cache-stats-matcher-results
jmarantz 2bace0f
Remove extraneous duplicate test method that was not used.
jmarantz 03fa724
Merge branch 'master' into cache-stats-matcher-results
jmarantz 106ee9f
Merge branch 'master' into cache-stats-matcher-results
jmarantz 7865fdb
Address review comments.
jmarantz b6f4e28
Merge branch 'master' into cache-stats-matcher-results
jmarantz 49067d9
Add TODO to clean up the leak, and use unique_ptr to simplify memory …
jmarantz 7af5cf2
remove superfluous reserve().
jmarantz 0fffd53
Remove leak, using shared_ptr<std::string> to mirror the leak-avoidan…
jmarantz cfea59d
Add comments about why the set is implemented using a map.
jmarantz 95b2752
Use heterogenous flat_hash_set rather than a flat_hash_map<const char…
jmarantz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| #include "common/stats/char_star_set.h" | ||
|
|
||
| #include "common/common/utility.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Stats { | ||
|
|
||
| CharStarSet::~CharStarSet() { | ||
| // It's generally safer to clear associative containers before mutating | ||
| // elements of them (e.g. by deleting them), unless you know the container | ||
| // internals well, 2-phase deletes like are more obviously correct. | ||
| std::vector<char*> keys; | ||
| keys.reserve(hash_set_.size()); | ||
| for (char* p : hash_set_) { | ||
| keys.push_back(p); | ||
| } | ||
|
|
||
| hash_set_.clear(); | ||
| for (char* p : keys) { | ||
| delete[] p; | ||
| } | ||
| } | ||
|
|
||
| const char* CharStarSet::insert(absl::string_view str) { | ||
| size_t bytes = str.size() + 1; | ||
| char* ptr = new char[bytes]; | ||
| StringUtil::strlcpy(ptr, str.data(), bytes); | ||
| auto insertion = hash_set_.insert(ptr); | ||
| if (!insertion.second) { | ||
| delete[] ptr; | ||
| return *insertion.first; | ||
| } | ||
| return ptr; | ||
| } | ||
|
|
||
| const char* CharStarSet::find(const char* str) const { | ||
| // The const_cast is necessary because hash_set_ is declared as a | ||
| // flat_hash_set<char*>, and the find() method does not add a 'const' | ||
| // qualifier to its key template type. As long as we don't modify the returned | ||
| // iterator it will not actually mutate the key, and the const_cast is safe. | ||
| auto iter = hash_set_.find(const_cast<char*>(str)); | ||
| if (iter == hash_set_.end()) { | ||
| return nullptr; | ||
| } | ||
| return *iter; | ||
| } | ||
|
|
||
| } // namespace Stats | ||
| } // namespace Envoy | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| #pragma once | ||
|
|
||
| #include "common/common/hash.h" | ||
|
|
||
| #include "absl/container/flat_hash_set.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Stats { | ||
|
|
||
| // Implements a set of NUL-terminated char*, with the property that once | ||
| // inserted, the char* pointers remain stable for the life of the set. Note | ||
| // there is currently no 'erase' method. | ||
| // | ||
| // Also note that this class may not have long-term value; it may be removed | ||
| // once SymbolTables are integrated (#6161). With that, the current sole | ||
| // use of this class, as a cache for disallowed stats from StatsMatcher. | ||
| // could be replaced by a StatNameHashSet or similar. | ||
| class CharStarSet { | ||
| public: | ||
| CharStarSet() = default; | ||
| ~CharStarSet(); | ||
| CharStarSet(const CharStarSet&) = delete; | ||
| CharStarSet& operator=(const CharStarSet&) = delete; | ||
|
|
||
| // Inserts a nul-terminated string, returning a stable char* reference to it. | ||
| const char* insert(absl::string_view str); | ||
|
|
||
| // Finds a stable reference for the specified string, returning nullptr if | ||
| // it's not in the set yet. | ||
| const char* find(const std::string& str) const { return find(str.c_str()); } | ||
| const char* find(const char* str) const; | ||
|
|
||
| // Returns the number of retained strings. | ||
| size_t size() { return hash_set_.size(); } | ||
|
|
||
| private: | ||
| absl::flat_hash_set<char*, ConstCharStarHash, ConstCharStarEqual> hash_set_; | ||
| }; | ||
|
|
||
| } // namespace Stats | ||
| } // namespace Envoy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't we have the hash key be a
unique_ptr<char[]>which would greatly simplify a bunch of the memory management in this class?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that making the set key be a std::unique_ptr<char[]> would simplify some things but would complicate the implementation of find(), which would need to construct a unique_ptr object from a const char* it doesn't own in order to do the set lookup. That seems worse than the current state.
However I did change the destructor and insert() to use a std::unique_ptr as a temp, and I think that does help a little -- reduces lines of code anyway. PTAL. I could go either way. Or if you have a more specific suggestion about how to implement this interface more cleanly I'm fine with that too.