Files
nix/src/libexpr-tests/regex-cache-bench.cc
Kamil Monicz 723c47550e libexpr-tests: add nix-expr-benchmarks
Provides focused microbenchmarks for expression evaluation hot paths (dynamic attrs, getDerivations attr scanning, and repeated builtins.match).
2025-12-19 02:10:05 +01:00

46 lines
1.2 KiB
C++

#include <benchmark/benchmark.h>
#include "nix/expr/eval.hh"
#include "nix/expr/eval-settings.hh"
#include "nix/fetchers/fetch-settings.hh"
#include "nix/store/store-open.hh"
using namespace nix;
static void BM_EvalManyBuiltinsMatchSameRegex(benchmark::State & state)
{
static constexpr int iterations = 5'000;
static constexpr std::string_view exprStr =
"builtins.foldl' "
"(acc: _: acc + builtins.length (builtins.match \"a\" \"a\")) "
"0 "
"(builtins.genList (x: x) "
"5000)";
for (auto _ : state) {
state.PauseTiming();
auto store = openStore("dummy://");
fetchers::Settings fetchSettings{};
bool readOnlyMode = true;
EvalSettings evalSettings{readOnlyMode};
evalSettings.nixPath = {};
EvalState st({}, store, fetchSettings, evalSettings, nullptr);
Expr * expr = st.parseExprFromString(std::string(exprStr), st.rootPath(CanonPath::root));
Value v;
state.ResumeTiming();
st.eval(expr, v);
st.forceValue(v, noPos);
benchmark::DoNotOptimize(v);
}
state.SetItemsProcessed(state.iterations() * iterations);
}
BENCHMARK(BM_EvalManyBuiltinsMatchSameRegex);