diff --git a/test/cpp/microbenchmarks/bm_huffman_decode.cc b/test/cpp/microbenchmarks/bm_huffman_decode.cc index ca14a987879..e9a03f153ae 100644 --- a/test/cpp/microbenchmarks/bm_huffman_decode.cc +++ b/test/cpp/microbenchmarks/bm_huffman_decode.cc @@ -19,34 +19,38 @@ #include "src/core/ext/transport/chttp2/transport/bin_encoder.h" #include "src/core/ext/transport/chttp2/transport/decode_huff.h" +#include "src/core/lib/gprpp/no_destruct.h" #include "src/core/lib/slice/slice.h" #include "test/core/util/test_config.h" -static const std::vector* kInput = [] { - std::vector v; - std::mt19937 rd(0); - std::uniform_int_distribution<> dist_ty(0, 100); - std::uniform_int_distribution<> dist_byte(0, 255); - std::uniform_int_distribution<> dist_normal(32, 126); - for (int i = 0; i < 1024 * 1024; i++) { - if (dist_ty(rd) == 1) { - v.push_back(dist_byte(rd)); - } else { - v.push_back(dist_normal(rd)); +const std::vector* Input() { + static const grpc_core::NoDestruct> v([]() { + std::vector v; + std::mt19937 rd(0); + std::uniform_int_distribution<> dist_ty(0, 100); + std::uniform_int_distribution<> dist_byte(0, 255); + std::uniform_int_distribution<> dist_normal(32, 126); + for (int i = 0; i < 1024 * 1024; i++) { + if (dist_ty(rd) == 1) { + v.push_back(dist_byte(rd)); + } else { + v.push_back(dist_normal(rd)); + } } - } - grpc_core::Slice s = grpc_core::Slice::FromCopiedBuffer(v); - grpc_core::Slice c(grpc_chttp2_huffman_compress(s.c_slice())); - return new std::vector(c.begin(), c.end()); -}(); + grpc_core::Slice s = grpc_core::Slice::FromCopiedBuffer(v); + grpc_core::Slice c(grpc_chttp2_huffman_compress(s.c_slice())); + return std::vector(c.begin(), c.end()); + }()); + return v.get(); +} static void BM_Decode(benchmark::State& state) { std::vector output; auto add = [&output](uint8_t c) { output.push_back(c); }; for (auto _ : state) { output.clear(); - grpc_core::HuffDecoder(add, kInput->data(), - kInput->data() + kInput->size()) + grpc_core::HuffDecoder(add, Input()->data(), + Input()->data() + Input()->size()) .Run(); } } @@ -446,7 +450,7 @@ static void BM_LegacyDecode(benchmark::State& state) { for (auto _ : state) { output.clear(); decode_state = 0; - for (auto c : *kInput) { + for (auto c : *Input()) { nibble(c >> 4); nibble(c & 0xf); } @@ -463,6 +467,7 @@ void RunTheBenchmarksNamespaced() { RunSpecifiedBenchmarks(); } int main(int argc, char** argv) { grpc::testing::TestEnvironment env(&argc, argv); benchmark::Initialize(&argc, argv); + Input(); // Force initialization of input data. benchmark::RunTheBenchmarksNamespaced(); return 0; }