[compiler-rt] 8cd51a6 - [NFC][sanitizer] Use power of two in TwoLevelMap
Vitaly Buka via llvm-commits
llvm-commits at lists.llvm.org
Sun Oct 24 18:37:39 PDT 2021
Author: Vitaly Buka
Date: 2021-10-24T18:37:23-07:00
New Revision: 8cd51a69e5b4cf9513eb4f416f113058ebd8f257
URL: https://github.com/llvm/llvm-project/commit/8cd51a69e5b4cf9513eb4f416f113058ebd8f257
DIFF: https://github.com/llvm/llvm-project/commit/8cd51a69e5b4cf9513eb4f416f113058ebd8f257.diff
LOG: [NFC][sanitizer] Use power of two in TwoLevelMap
Using divisions by non power of two makes
a difference on x86_64 and aarch64 benchmarks.
Added:
Modified:
compiler-rt/lib/sanitizer_common/sanitizer_flat_map.h
Removed:
################################################################################
diff --git a/compiler-rt/lib/sanitizer_common/sanitizer_flat_map.h b/compiler-rt/lib/sanitizer_common/sanitizer_flat_map.h
index 038ee24e6bd8..0f7467c47e73 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_flat_map.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_flat_map.h
@@ -65,6 +65,10 @@ template <typename T, u64 kSize1, u64 kSize2,
typename AddressSpaceViewTy = LocalAddressSpaceView,
class MapUnmapCallback = NoOpMapUnmapCallback>
class TwoLevelMap {
+ static_assert(IsPowerOfTwo(kSize2), "");
+ static constexpr uptr kSize2Shift = Log2(kSize2);
+ static constexpr uptr kSize2Mask = kSize2 - 1;
+
public:
using AddressSpaceView = AddressSpaceViewTy;
void Init() {
@@ -100,19 +104,19 @@ class TwoLevelMap {
bool contains(uptr idx) const {
CHECK_LT(idx, kSize1 * kSize2);
- return Get(idx / kSize2);
+ return Get(idx >> kSize2Shift);
}
const T &operator[](uptr idx) const {
DCHECK_LT(idx, kSize1 * kSize2);
- T *map2 = GetOrCreate(idx / kSize2);
- return *AddressSpaceView::Load(&map2[idx % kSize2]);
+ T *map2 = GetOrCreate(idx >> kSize2Shift);
+ return *AddressSpaceView::Load(&map2[idx & kSize2Mask]);
}
T &operator[](uptr idx) {
DCHECK_LT(idx, kSize1 * kSize2);
- T *map2 = GetOrCreate(idx / kSize2);
- return *AddressSpaceView::LoadWritable(&map2[idx % kSize2]);
+ T *map2 = GetOrCreate(idx >> kSize2Shift);
+ return *AddressSpaceView::LoadWritable(&map2[idx & kSize2Mask]);
}
private:
More information about the llvm-commits
mailing list