Parcourir la source

Kernel: Add a 1-deep cache to Process::region_from_range()

This simple cache gets hit over 70% of the time on "g++ Process.cpp"
and shaves ~3% off the runtime.
Andreas Kling il y a 5 ans
Parent
commit
8d9dd1b04b
2 fichiers modifiés avec 14 ajouts et 1 suppressions
  1. 9 1
      Kernel/Process.cpp
  2. 5 0
      Kernel/Process.h

+ 9 - 1
Kernel/Process.cpp

@@ -223,6 +223,8 @@ Region* Process::allocate_region_with_vmobject(VirtualAddress vaddr, size_t size
 bool Process::deallocate_region(Region& region)
 {
     InterruptDisabler disabler;
+    if (m_region_lookup_cache.region == &region)
+        m_region_lookup_cache.region = nullptr;
     for (int i = 0; i < m_regions.size(); ++i) {
         if (&m_regions[i] == &region) {
             m_regions.remove(i);
@@ -234,10 +236,16 @@ bool Process::deallocate_region(Region& region)
 
 Region* Process::region_from_range(const Range& range)
 {
+    if (m_region_lookup_cache.range == range && m_region_lookup_cache.region)
+        return m_region_lookup_cache.region;
+
     size_t size = PAGE_ROUND_UP(range.size());
     for (auto& region : m_regions) {
-        if (region.vaddr() == range.base() && region.size() == size)
+        if (region.vaddr() == range.base() && region.size() == size) {
+            m_region_lookup_cache.range = range;
+            m_region_lookup_cache.region = &region;
             return &region;
+        }
     }
     return nullptr;
 }

+ 5 - 0
Kernel/Process.h

@@ -452,6 +452,11 @@ private:
     Region* region_containing(const Range&);
 
     NonnullOwnPtrVector<Region> m_regions;
+    struct RegionLookupCache {
+        Range range;
+        Region* region { nullptr };
+    };
+    RegionLookupCache m_region_lookup_cache;
 
     pid_t m_ppid { 0 };
     mode_t m_umask { 022 };