From aa4b64f4c8d1456a70e2f66f305457d255b952d3 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Thu, 3 Nov 2022 11:44:15 +0000 Subject: [PATCH] tint/utils: Optimise HashmapBase::Scan() Avoid an integer modulo for each scan iteration. Change-Id: Ie80af4620b39769db622b4ab175b5639cf9ade8e Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/108260 Reviewed-by: Corentin Wallez Kokoro: Ben Clayton Commit-Queue: Ben Clayton Auto-Submit: Ben Clayton --- src/tint/utils/hashmap_base.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/tint/utils/hashmap_base.h b/src/tint/utils/hashmap_base.h index 0460373cff..ca0712ae72 100644 --- a/src/tint/utils/hashmap_base.h +++ b/src/tint/utils/hashmap_base.h @@ -453,12 +453,18 @@ class HashmapBase { /// * must return Action::kStop within one whole cycle of the slots. template void Scan(size_t start, F&& f) const { - size_t index = start; - for (size_t distance = 0; distance < slots_.Length(); distance++) { + size_t distance = 0; + for (size_t index = start; index < slots_.Length(); index++) { if (f(distance, index) == Action::kStop) { return; } - index = Wrap(index + 1); + distance++; + } + for (size_t index = 0; index < start; index++) { + if (f(distance, index) == Action::kStop) { + return; + } + distance++; } tint::diag::List diags; TINT_ICE(Utils, diags) << "HashmapBase::Scan() looped entire map without finding a slot";