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.
This commit is contained in:
Andreas Kling 2020-01-19 16:44:37 +01:00
parent ae0c435e68
commit 8d9dd1b04b
Notes: sideshowbarker 2024-07-19 09:57:16 +09:00
2 changed files with 14 additions and 1 deletions

View file

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

View file

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