mirror of https://github.com/grpc/grpc.git
[channel_args] Improve comparison performance (#30322)
* [channel_args] Improve comparison performance * fix * Automated change: Fix sanity tests * faster still * fix * fix * some no cost boosts if they get hit * fix * final tweak for pointers * check * fix * Automated change: Fix sanity tests * fix * ugh Co-authored-by: ctiller <ctiller@users.noreply.github.com>pull/30354/head
parent
6629f604f9
commit
f5bb60d8e8
12 changed files with 266 additions and 84 deletions
@ -0,0 +1,95 @@ |
|||||||
|
// Copyright 2017 gRPC authors.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
/* Benchmark ChannelArgs comparison performance between grpc_channel_args and
|
||||||
|
* grpc_core::ChannelArgs */ |
||||||
|
|
||||||
|
#include <random> |
||||||
|
|
||||||
|
#include <benchmark/benchmark.h> |
||||||
|
|
||||||
|
#include "absl/container/btree_map.h" |
||||||
|
|
||||||
|
#include <grpcpp/support/channel_arguments.h> |
||||||
|
|
||||||
|
#include "src/core/lib/channel/channel_args.h" |
||||||
|
|
||||||
|
const char kKey[] = "a very long key"; |
||||||
|
const char kValue[] = "a very long value"; |
||||||
|
|
||||||
|
void BM_ChannelArgs(benchmark::State& state) { |
||||||
|
grpc_core::ChannelArgs arg1, arg2; |
||||||
|
arg1 = arg1.Set(kKey, kValue); |
||||||
|
arg2 = arg2.Set(kKey, kValue); |
||||||
|
for (auto s : state) { |
||||||
|
benchmark::DoNotOptimize(arg1 < arg2); |
||||||
|
} |
||||||
|
} |
||||||
|
BENCHMARK(BM_ChannelArgs); |
||||||
|
|
||||||
|
void BM_grpc_channel_args(benchmark::State& state) { |
||||||
|
grpc_channel_args arg1, arg2; |
||||||
|
grpc::ChannelArguments xargs; |
||||||
|
xargs.SetString(kKey, kValue); |
||||||
|
xargs.SetChannelArgs(&arg1); |
||||||
|
xargs.SetChannelArgs(&arg2); |
||||||
|
for (auto s : state) { |
||||||
|
benchmark::DoNotOptimize(grpc_channel_args_compare(&arg1, &arg2)); |
||||||
|
} |
||||||
|
} |
||||||
|
BENCHMARK(BM_grpc_channel_args); |
||||||
|
|
||||||
|
void BM_ChannelArgsAsKeyIntoMap(benchmark::State& state) { |
||||||
|
std::map<grpc_core::ChannelArgs, int> m; |
||||||
|
std::vector<grpc_core::ChannelArgs> v; |
||||||
|
for (int i = 0; i < 10000; i++) { |
||||||
|
const auto& a = grpc_core::ChannelArgs().Set(kKey, i); |
||||||
|
m[a] = i; |
||||||
|
v.push_back(a); |
||||||
|
} |
||||||
|
std::shuffle(v.begin(), v.end(), std::mt19937(std::random_device()())); |
||||||
|
size_t n = 0; |
||||||
|
for (auto s : state) { |
||||||
|
benchmark::DoNotOptimize(m.find(v[n++ % v.size()])); |
||||||
|
} |
||||||
|
} |
||||||
|
BENCHMARK(BM_ChannelArgsAsKeyIntoMap); |
||||||
|
|
||||||
|
void BM_ChannelArgsAsKeyIntoBTree(benchmark::State& state) { |
||||||
|
absl::btree_map<grpc_core::ChannelArgs, int> m; |
||||||
|
std::vector<grpc_core::ChannelArgs> v; |
||||||
|
for (int i = 0; i < 10000; i++) { |
||||||
|
const auto& a = grpc_core::ChannelArgs().Set(kKey, i); |
||||||
|
m[a] = i; |
||||||
|
v.push_back(a); |
||||||
|
} |
||||||
|
std::shuffle(v.begin(), v.end(), std::mt19937(std::random_device()())); |
||||||
|
size_t n = 0; |
||||||
|
for (auto s : state) { |
||||||
|
benchmark::DoNotOptimize(m.find(v[n++ % v.size()])); |
||||||
|
} |
||||||
|
} |
||||||
|
BENCHMARK(BM_ChannelArgsAsKeyIntoBTree); |
||||||
|
|
||||||
|
// Some distros have RunSpecifiedBenchmarks under the benchmark namespace,
|
||||||
|
// and others do not. This allows us to support both modes.
|
||||||
|
namespace benchmark { |
||||||
|
void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); } |
||||||
|
} // namespace benchmark
|
||||||
|
|
||||||
|
int main(int argc, char** argv) { |
||||||
|
::benchmark::Initialize(&argc, argv); |
||||||
|
benchmark::RunTheBenchmarksNamespaced(); |
||||||
|
return 0; |
||||||
|
} |
Loading…
Reference in new issue