From 94070473307b4ded676b97b1a673b496c074cda0 Mon Sep 17 00:00:00 2001 From: JosJuice Date: Sun, 9 Aug 2026 17:37:15 +0200 Subject: 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. --- Source/Core/Common/LazyMemoryRegionBitSet.cpp | 96 +++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 Source/Core/Common/LazyMemoryRegionBitSet.cpp (limited to 'Source/Core/Common/LazyMemoryRegionBitSet.cpp') 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 + +#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(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 -- cgit v1.2.3