From d71e19fd753b58981c7b28af887f0fe1166d2973 Mon Sep 17 00:00:00 2001
From: Lioncash <mathew1800@gmail.com>
Date: Tue, 24 Jul 2018 11:19:51 -0400
Subject: [PATCH 1/2] video_core/memory_manager: Avoid repeated unnecessary
 page slot lookups

We don't need to keep calling the same function over and over again in a
loop, especially when the behavior is slightly non-trivial. We can just
keep a reference to the looked up location and do all the checking and
assignments based off it instead.
---
 src/video_core/memory_manager.cpp | 32 ++++++++++++++++++++-----------
 1 file changed, 21 insertions(+), 11 deletions(-)

diff --git a/src/video_core/memory_manager.cpp b/src/video_core/memory_manager.cpp
index 2f814a1842..98a6ca0401 100644
--- a/src/video_core/memory_manager.cpp
+++ b/src/video_core/memory_manager.cpp
@@ -13,8 +13,10 @@ GPUVAddr MemoryManager::AllocateSpace(u64 size, u64 align) {
     ASSERT(gpu_addr);
 
     for (u64 offset = 0; offset < size; offset += PAGE_SIZE) {
-        ASSERT(PageSlot(*gpu_addr + offset) == static_cast<u64>(PageStatus::Unmapped));
-        PageSlot(*gpu_addr + offset) = static_cast<u64>(PageStatus::Allocated);
+        VAddr& slot = PageSlot(*gpu_addr + offset);
+
+        ASSERT(slot == static_cast<u64>(PageStatus::Unmapped));
+        slot = static_cast<u64>(PageStatus::Allocated);
     }
 
     return *gpu_addr;
@@ -22,8 +24,10 @@ GPUVAddr MemoryManager::AllocateSpace(u64 size, u64 align) {
 
 GPUVAddr MemoryManager::AllocateSpace(GPUVAddr gpu_addr, u64 size, u64 align) {
     for (u64 offset = 0; offset < size; offset += PAGE_SIZE) {
-        ASSERT(PageSlot(gpu_addr + offset) == static_cast<u64>(PageStatus::Unmapped));
-        PageSlot(gpu_addr + offset) = static_cast<u64>(PageStatus::Allocated);
+        VAddr& slot = PageSlot(gpu_addr + offset);
+
+        ASSERT(slot == static_cast<u64>(PageStatus::Unmapped));
+        slot = static_cast<u64>(PageStatus::Allocated);
     }
 
     return gpu_addr;
@@ -34,8 +38,10 @@ GPUVAddr MemoryManager::MapBufferEx(VAddr cpu_addr, u64 size) {
     ASSERT(gpu_addr);
 
     for (u64 offset = 0; offset < size; offset += PAGE_SIZE) {
-        ASSERT(PageSlot(*gpu_addr + offset) == static_cast<u64>(PageStatus::Unmapped));
-        PageSlot(*gpu_addr + offset) = cpu_addr + offset;
+        VAddr& slot = PageSlot(*gpu_addr + offset);
+
+        ASSERT(slot == static_cast<u64>(PageStatus::Unmapped));
+        slot = cpu_addr + offset;
     }
 
     MappedRegion region{cpu_addr, *gpu_addr, size};
@@ -48,8 +54,10 @@ GPUVAddr MemoryManager::MapBufferEx(VAddr cpu_addr, GPUVAddr gpu_addr, u64 size)
     ASSERT((gpu_addr & PAGE_MASK) == 0);
 
     for (u64 offset = 0; offset < size; offset += PAGE_SIZE) {
-        ASSERT(PageSlot(gpu_addr + offset) == static_cast<u64>(PageStatus::Allocated));
-        PageSlot(gpu_addr + offset) = cpu_addr + offset;
+        VAddr& slot = PageSlot(gpu_addr + offset);
+
+        ASSERT(slot == static_cast<u64>(PageStatus::Allocated));
+        slot = cpu_addr + offset;
     }
 
     MappedRegion region{cpu_addr, gpu_addr, size};
@@ -62,9 +70,11 @@ GPUVAddr MemoryManager::UnmapBuffer(GPUVAddr gpu_addr, u64 size) {
     ASSERT((gpu_addr & PAGE_MASK) == 0);
 
     for (u64 offset = 0; offset < size; offset += PAGE_SIZE) {
-        ASSERT(PageSlot(gpu_addr + offset) != static_cast<u64>(PageStatus::Allocated) &&
-               PageSlot(gpu_addr + offset) != static_cast<u64>(PageStatus::Unmapped));
-        PageSlot(gpu_addr + offset) = static_cast<u64>(PageStatus::Unmapped);
+        VAddr& slot = PageSlot(gpu_addr + offset);
+
+        ASSERT(slot != static_cast<u64>(PageStatus::Allocated) &&
+               slot != static_cast<u64>(PageStatus::Unmapped));
+        slot = static_cast<u64>(PageStatus::Unmapped);
     }
 
     // Delete the region mappings that are contained within the unmapped region

From bf608f125eeb80a79fae20d98413cca1dbfdd486 Mon Sep 17 00:00:00 2001
From: Lioncash <mathew1800@gmail.com>
Date: Tue, 24 Jul 2018 11:51:10 -0400
Subject: [PATCH 2/2] video_core/memory_manager: Replace a loop with
 std::array's fill() function in PageSlot()

We already have a function that does what this code was doing, so let's
use that instead.
---
 src/video_core/memory_manager.cpp | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/src/video_core/memory_manager.cpp b/src/video_core/memory_manager.cpp
index 98a6ca0401..ca923d17db 100644
--- a/src/video_core/memory_manager.cpp
+++ b/src/video_core/memory_manager.cpp
@@ -138,9 +138,7 @@ VAddr& MemoryManager::PageSlot(GPUVAddr gpu_addr) {
     auto& block = page_table[(gpu_addr >> (PAGE_BITS + PAGE_TABLE_BITS)) & PAGE_TABLE_MASK];
     if (!block) {
         block = std::make_unique<PageBlock>();
-        for (unsigned index = 0; index < PAGE_BLOCK_SIZE; index++) {
-            (*block)[index] = static_cast<u64>(PageStatus::Unmapped);
-        }
+        block->fill(static_cast<VAddr>(PageStatus::Unmapped));
     }
     return (*block)[(gpu_addr >> PAGE_BITS) & PAGE_BLOCK_MASK];
 }