From fa15314651c299d9e737ff31e5ae6e13352e9276 Mon Sep 17 00:00:00 2001 From: Colin Ian King Date: Wed, 12 Jun 2024 15:51:49 +0100 Subject: [PATCH] db_bench: optimize Done, replace slow divisions with addition/subtractions There are two slow 64 bit division operations that are computationally quite expensive. Replace these with a modulo style counter that uses simpler and faster increment, comparision and subtraction operations. On a 60 second run, this optimization reduced the function CPU utilization from 21.8 seconds down to 2.3 seconds on an i7-6700 as measured using Intel vtune. Signed-off-by: Colin Ian King --- tools/db_bench_tool.cc | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tools/db_bench_tool.cc b/tools/db_bench_tool.cc index 0dc92def7..a3b8b06a9 100644 --- a/tools/db_bench_tool.cc +++ b/tools/db_bench_tool.cc @@ -2887,6 +2887,7 @@ class Duration { max_ops_ = max_ops; ops_per_stage_ = (ops_per_stage > 0) ? ops_per_stage : max_ops; ops_ = 0; + ops_modulo_ = 0; start_at_ = FLAGS_env->NowMicros(); } @@ -2895,12 +2896,14 @@ class Duration { bool Done(int64_t increment) { if (increment <= 0) increment = 1; // avoid Done(0) and infinite loops ops_ += increment; + ops_modulo_ += increment; if (max_seconds_) { // Recheck every appx 1000 ops (exact iff increment is factor of 1000) auto granularity = FLAGS_ops_between_duration_checks; - if ((ops_ / granularity) != ((ops_ - increment) / granularity)) { + if (ops_modulo_ >= granularity) { uint64_t now = FLAGS_env->NowMicros(); + ops_modulo_ -= granularity; return ((now - start_at_) / 1000000) >= max_seconds_; } else { return false; @@ -2915,6 +2918,7 @@ class Duration { int64_t max_ops_; int64_t ops_per_stage_; int64_t ops_; + int64_t ops_modulo_; uint64_t start_at_; };