[PR #3798] buffer_cache: Begin memory tracker rework #3723

Open
opened 2026-02-27 22:04:45 +03:00 by kerem · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/shadps4-emu/shadPS4/pull/3798
Author: @raphaelthegreat
Created: 11/14/2025
Status: 🔄 Open

Base: mainHead: new-tracker


📝 Commits (1)

  • 94e4543 buffer_cache: Begin memory tracker rework

📊 Changes

10 files changed (+428 additions, -877 deletions)

View changed files

📝 CMakeLists.txt (+0 -2)
src/common/bit_array.h (+0 -406)
src/common/range_lock.h (+0 -101)
📝 src/video_core/buffer_cache/buffer_cache.cpp (+2 -8)
📝 src/video_core/buffer_cache/buffer_cache.h (+0 -3)
📝 src/video_core/buffer_cache/memory_tracker.h (+66 -107)
📝 src/video_core/buffer_cache/region_definitions.h (+57 -11)
📝 src/video_core/buffer_cache/region_manager.h (+187 -133)
📝 src/video_core/page_manager.cpp (+111 -97)
📝 src/video_core/page_manager.h (+5 -9)

📄 Description

This is split from buffer-cleanup branch to test it separately

Current memory tracker was a rewrite from https://github.com/shadps4-emu/shadPS4/pull/3070. The main advantage of that rework was batching memory protection actions for the whole region instead of just the modified areas. However the introduction of the bitset also restricted locking flexibility; a single lock for the entire region was required.

This rework aims to solve the second issue, that of lock granularity, while retaining the benefits. It is a mix of older and newer code, by taking the faster and more readable approaches from either case. Locking is now per-page; each word also has a lock bitmask that is set/cleared to acquire/release the locks. For uncontested case, acquiring a lock is a bitwise or and a cmpxchg instruction. Locks for up to 64 pages can also be acquired immediately for said case, so iterating larger memory regions remains fast. When the lock is contested, waitable atomics are used which (at least in libstdc++) will perform a spinloop and then sleep, behaving in similar fashion to an adaptive mutex.

Readbacks are still unimplemented because the new locking makes the old invalidate method annoying to reimplement. WIll be added again before this is done


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/shadps4-emu/shadPS4/pull/3798 **Author:** [@raphaelthegreat](https://github.com/raphaelthegreat) **Created:** 11/14/2025 **Status:** 🔄 Open **Base:** `main` ← **Head:** `new-tracker` --- ### 📝 Commits (1) - [`94e4543`](https://github.com/shadps4-emu/shadPS4/commit/94e45435c6d1d57af25319f5c5d0d481ebd75b2b) buffer_cache: Begin memory tracker rework ### 📊 Changes **10 files changed** (+428 additions, -877 deletions) <details> <summary>View changed files</summary> 📝 `CMakeLists.txt` (+0 -2) ➖ `src/common/bit_array.h` (+0 -406) ➖ `src/common/range_lock.h` (+0 -101) 📝 `src/video_core/buffer_cache/buffer_cache.cpp` (+2 -8) 📝 `src/video_core/buffer_cache/buffer_cache.h` (+0 -3) 📝 `src/video_core/buffer_cache/memory_tracker.h` (+66 -107) 📝 `src/video_core/buffer_cache/region_definitions.h` (+57 -11) 📝 `src/video_core/buffer_cache/region_manager.h` (+187 -133) 📝 `src/video_core/page_manager.cpp` (+111 -97) 📝 `src/video_core/page_manager.h` (+5 -9) </details> ### 📄 Description This is split from buffer-cleanup branch to test it separately Current memory tracker was a rewrite from https://github.com/shadps4-emu/shadPS4/pull/3070. The main advantage of that rework was batching memory protection actions for the whole region instead of just the modified areas. However the introduction of the bitset also restricted locking flexibility; a single lock for the entire region was required. This rework aims to solve the second issue, that of lock granularity, while retaining the benefits. It is a mix of older and newer code, by taking the faster and more readable approaches from either case. Locking is now per-page; each word also has a lock bitmask that is set/cleared to acquire/release the locks. For uncontested case, acquiring a lock is a bitwise or and a cmpxchg instruction. Locks for up to 64 pages can also be acquired immediately for said case, so iterating larger memory regions remains fast. When the lock is contested, waitable atomics are used which (at least in libstdc++) will perform a spinloop and then sleep, behaving in similar fashion to an adaptive mutex. Readbacks are still unimplemented because the new locking makes the old invalidate method annoying to reimplement. WIll be added again before this is done --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/shadPS4#3723
No description provided.