-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
perf: reduce the memory usage of LC Trie construction #4117
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1465472
perf: reduce the memory usage of LC Trie construction
171bdbf
Kick CI
9675509
Kick CI
5fe7395
Small additional optimization, plus benchmarks for more scenarios
d801512
Make the test setup more symmetrical and reenable the reclaiming of s…
9dfc9fd
Switch from C-style cast to C++ `static_cast`
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 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 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 |
---|---|---|
|
@@ -7,14 +7,55 @@ namespace { | |
|
||
std::vector<Envoy::Network::Address::InstanceConstSharedPtr> addresses; | ||
|
||
std::vector<std::pair<std::string, std::vector<Envoy::Network::Address::CidrRange>>> tag_data; | ||
|
||
std::vector<std::pair<std::string, std::vector<Envoy::Network::Address::CidrRange>>> | ||
tag_data_nested_prefixes; | ||
|
||
std::vector<std::pair<std::string, std::vector<Envoy::Network::Address::CidrRange>>> | ||
tag_data_minimal; | ||
|
||
std::unique_ptr<Envoy::Network::LcTrie::LcTrie<std::string>> lc_trie; | ||
|
||
std::unique_ptr<Envoy::Network::LcTrie::LcTrie<std::string>> lc_trie_nested_prefixes; | ||
|
||
std::unique_ptr<Envoy::Network::LcTrie::LcTrie<std::string>> lc_trie_minimal; | ||
|
||
} // namespace | ||
|
||
namespace Envoy { | ||
|
||
static void BM_LcTrieConstruct(benchmark::State& state) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add benchmarks for construct with:
|
||
std::unique_ptr<Envoy::Network::LcTrie::LcTrie<std::string>> trie; | ||
for (auto _ : state) { | ||
trie = std::make_unique<Envoy::Network::LcTrie::LcTrie<std::string>>(tag_data); | ||
} | ||
benchmark::DoNotOptimize(trie); | ||
} | ||
|
||
BENCHMARK(BM_LcTrieConstruct); | ||
|
||
static void BM_LcTrieConstructNested(benchmark::State& state) { | ||
std::unique_ptr<Envoy::Network::LcTrie::LcTrie<std::string>> trie; | ||
for (auto _ : state) { | ||
trie = std::make_unique<Envoy::Network::LcTrie::LcTrie<std::string>>(tag_data_nested_prefixes); | ||
} | ||
benchmark::DoNotOptimize(trie); | ||
} | ||
|
||
BENCHMARK(BM_LcTrieConstructNested); | ||
|
||
static void BM_LcTrieConstructMinimal(benchmark::State& state) { | ||
|
||
std::unique_ptr<Envoy::Network::LcTrie::LcTrie<std::string>> trie; | ||
for (auto _ : state) { | ||
trie = std::make_unique<Envoy::Network::LcTrie::LcTrie<std::string>>(tag_data_minimal); | ||
} | ||
benchmark::DoNotOptimize(trie); | ||
} | ||
|
||
BENCHMARK(BM_LcTrieConstructMinimal); | ||
|
||
static void BM_LcTrieLookup(benchmark::State& state) { | ||
static size_t i = 0; | ||
size_t output_tags = 0; | ||
|
@@ -41,6 +82,19 @@ static void BM_LcTrieLookupWithNestedPrefixes(benchmark::State& state) { | |
|
||
BENCHMARK(BM_LcTrieLookupWithNestedPrefixes); | ||
|
||
static void BM_LcTrieLookupMinimal(benchmark::State& state) { | ||
static size_t i = 0; | ||
size_t output_tags = 0; | ||
for (auto _ : state) { | ||
i++; | ||
i %= addresses.size(); | ||
output_tags += lc_trie_minimal->getData(addresses[i]).size(); | ||
} | ||
benchmark::DoNotOptimize(output_tags); | ||
} | ||
|
||
BENCHMARK(BM_LcTrieLookupMinimal); | ||
|
||
} // namespace Envoy | ||
|
||
// Boilerplate main(), which discovers benchmarks in the same file and runs them. | ||
|
@@ -54,19 +108,29 @@ int main(int argc, char** argv) { | |
addresses.push_back(Envoy::Network::Utility::parseInternetAddress(address)); | ||
} | ||
|
||
std::vector<std::pair<std::string, std::vector<Envoy::Network::Address::CidrRange>>> tag_data; | ||
// Construct three sets of prefixes: one consisting of 1,024 addresses in an | ||
// RFC 5737 netblock, another consisting of those same addresses plus | ||
// 0.0.0.0/0 (to exercise the LC Trie's support for nested prefixes), | ||
// and finally a set containing only 0.0.0.0/0. | ||
for (int i = 0; i < 32; i++) { | ||
for (int j = 0; j < 32; j++) { | ||
tag_data.emplace_back(std::pair<std::string, std::vector<Envoy::Network::Address::CidrRange>>( | ||
{"tag_1", | ||
{Envoy::Network::Address::CidrRange::create(fmt::format("192.0.{}.{}/32", i, j))}})); | ||
} | ||
} | ||
tag_data_nested_prefixes = tag_data; | ||
tag_data_nested_prefixes.emplace_back( | ||
std::pair<std::string, std::vector<Envoy::Network::Address::CidrRange>>( | ||
{"tag_0", {Envoy::Network::Address::CidrRange::create("0.0.0.0/0")}})); | ||
tag_data_minimal.emplace_back( | ||
std::pair<std::string, std::vector<Envoy::Network::Address::CidrRange>>( | ||
{"tag_1", {Envoy::Network::Address::CidrRange::create("0.0.0.0/0")}})); | ||
|
||
lc_trie = std::make_unique<Envoy::Network::LcTrie::LcTrie<std::string>>(tag_data); | ||
tag_data.emplace_back(std::pair<std::string, std::vector<Envoy::Network::Address::CidrRange>>( | ||
{"tag_0", {Envoy::Network::Address::CidrRange::create("0.0.0.0/0")}})); | ||
lc_trie_nested_prefixes = std::make_unique<Envoy::Network::LcTrie::LcTrie<std::string>>(tag_data); | ||
lc_trie_nested_prefixes = | ||
std::make_unique<Envoy::Network::LcTrie::LcTrie<std::string>>(tag_data_nested_prefixes); | ||
lc_trie_minimal = std::make_unique<Envoy::Network::LcTrie::LcTrie<std::string>>(tag_data_minimal); | ||
|
||
benchmark::Initialize(&argc, argv); | ||
if (benchmark::ReportUnrecognizedArguments(argc, argv)) { | ||
|
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.
reserve()
gives additional 5% performance boost.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.
Thanks! I'll switch to
reserve
.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.
But you didn't...
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.
Because I realized it would cause a buffer write overflow...
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.
How come? If
trie_.reserve(position + 1)
doesn't increase capacity, then how cometrie_.reserve(size_t(ip_prefixes_.size() / fill_factor_))
does?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.
They both have the potential to increase capacity. But what's needed in order for
trie_[position]
to be valid isn't an increase intrie_
's capacity, but rather an increase in its size. Thereserve
method doesn't increase the size, whereasresize
does.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.
Right, it's
>= size()
and not>= capacity()
check. Thanks!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.
If you are really trying to go for micro/max perf you could probably switch to a C style array here, but probably not worth it right now.