summaryrefslogtreecommitdiff
path: root/Source/Core/Common/LazyMemoryRegionBitSet.cpp
diff options
context:
space:
mode:
authorJosJuice <josjuice@gmail.com>2026-08-09 17:37:15 +0200
committerJosJuice <josjuice@gmail.com>2026-08-22 11:51:48 +0200
commit94070473307b4ded676b97b1a673b496c074cda0 (patch)
tree997267bea65edb9d6619667e05cf81c0886e6911 /Source/Core/Common/LazyMemoryRegionBitSet.cpp
parentd2f7ed3602be867051639dcee2c660a1879d0b97 (diff)
Jit: Use LazyMemoryRegion-backed bit sets
JitCache has been using a class called ValidBlockBitSet. This commit replaces it with a new class called LazyMemoryRegionBitSet, which works similarly but is backed by a LazyMemoryRegion, letting us skip allocating memory for parts of the bit set that correspond to emulated memory regions where the game isn't storing any code. I have also added optimized functions for setting or clearing long runs of bits. Additionally, this commit replaces JitBase's three std::unordered_sets that are keeping track of instructions with LazyMemoryRegionBitSets. Here the benefit is more speed than memory usage. This improves NBA Live 2005's menu performance by 1% or so in SuperSamus's testing.
Diffstat (limited to 'Source/Core/Common/LazyMemoryRegionBitSet.cpp')
-rw-r--r--Source/Core/Common/LazyMemoryRegionBitSet.cpp96
1 files changed, 96 insertions, 0 deletions
diff --git a/Source/Core/Common/LazyMemoryRegionBitSet.cpp b/Source/Core/Common/LazyMemoryRegionBitSet.cpp
new file mode 100644
index 0000000000..af9d79c8df
--- /dev/null
+++ b/Source/Core/Common/LazyMemoryRegionBitSet.cpp
@@ -0,0 +1,96 @@
+// Copyright 2026 Dolphin Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include "Common/LazyMemoryRegionBitSet.h"
+
+#include <cstring>
+
+#include "Common/Align.h"
+#include "Common/MsgHandler.h"
+
+namespace Common
+{
+LazyMemoryRegionBitSet::LazyMemoryRegionBitSet(size_t bits) : m_bits(bits), m_valid(Create(bits))
+{
+}
+
+bool LazyMemoryRegionBitSet::Create(size_t bits)
+{
+ m_pointer = reinterpret_cast<u32*>(m_region.Create(Common::AlignUp(bits, 32) / 8));
+ if (!m_pointer)
+ {
+ PanicAlertFmt("LazyMemoryRegionBitSet failed to create backing region");
+ return false;
+ }
+
+ return true;
+}
+
+void LazyMemoryRegionBitSet::Clear()
+{
+ m_region.Clear();
+}
+
+void LazyMemoryRegionBitSet::CheckBitIndicesWriteable(size_t bits_start, size_t bits_end)
+{
+ CheckBitIndexReadable(bits_end - 1);
+ const size_t start_byte = Common::AlignDown(bits_start, 32) / 8;
+ const size_t end_byte = Common::AlignUp(bits_end, 32) / 8;
+ m_region.EnsureMemoryPagesWritable(start_byte, end_byte - start_byte);
+}
+
+void LazyMemoryRegionBitSet::SetBits(size_t bits_start, size_t bits_end)
+{
+ if (bits_start >= bits_end)
+ return;
+
+ CheckBitIndicesWriteable(bits_start, bits_end);
+
+ const u32 extra_first_word = 0xFFFFFFFF << bits_start % 32;
+ const u32 extra_last_word = ~(0xFFFFFFFF << bits_end % 32);
+
+ if (bits_start / 32 == bits_end / 32)
+ {
+ m_pointer[bits_start / 32] |= extra_first_word & extra_last_word;
+ return;
+ }
+
+ if (bits_start % 32 != 0)
+ m_pointer[bits_start / 32] |= extra_first_word;
+
+ const size_t start_index = Common::AlignUp(bits_start, 32) / 32;
+ const size_t end_index = bits_end / 32;
+ std::memset(m_pointer + start_index, 0xFF, (end_index - start_index) * 4);
+
+ if (bits_end % 32 != 0)
+ m_pointer[bits_end / 32] |= extra_last_word;
+}
+
+void LazyMemoryRegionBitSet::ClearBits(size_t bits_start, size_t bits_end)
+{
+ if (bits_start >= bits_end)
+ return;
+
+ CheckBitIndicesWriteable(bits_start, bits_end);
+
+ const u32 extra_first_word = ~(0xFFFFFFFF << bits_start % 32);
+ const u32 extra_last_word = 0xFFFFFFFF << bits_end % 32;
+
+ if (bits_start / 32 == bits_end / 32)
+ {
+ m_pointer[bits_start / 32] &= extra_first_word | extra_last_word;
+ return;
+ }
+
+ if (bits_start % 32 != 0)
+ m_pointer[bits_start / 32] &= extra_first_word;
+
+ const size_t start_index = Common::AlignUp(bits_start, 32) / 32;
+ const size_t end_index = bits_end / 32;
+ std::memset(m_pointer + start_index, 0x00, (end_index - start_index) * sizeof(u32));
+
+ if (bits_end % 32 != 0)
+ m_pointer[bits_end / 32] &= extra_last_word;
+}
+
+} // namespace Common