Skip to content

Commit 9b49603

Browse files
committed
memctrl: keep address_map sorted by address
We do a linear scan in find_range (which is called on all TLB misses) to find the entries. The largest and most frequently hit entry is the system memory (which starts at 0). By ensuring that it's the first entry in the list, we end up only doing one iteration through the loop.
1 parent f192d11 commit 9b49603

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

devices/memctrl/memctrlbase.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,18 @@ bool MemCtrlBase::add_mem_region(uint32_t start_addr, uint32_t size,
168168
entry->devobj = nullptr;
169169
entry->mem_ptr = reg_content;
170170

171-
this->address_map.push_back(entry);
171+
// Keep address_map sorted, that way the RAM region (which starts at 0 and
172+
// is most often requested) will be found by find_range on the first
173+
// iteration.
174+
this->address_map.insert(
175+
std::upper_bound(
176+
this->address_map.begin(),
177+
this->address_map.end(),
178+
entry,
179+
[](const auto& lhs, const auto& rhs) {
180+
return lhs->start < rhs->start;
181+
}),
182+
entry);
172183

173184
LOG_F(INFO, "Added mem region 0x%X..0x%X (%s%s%s%s) -> 0x%X", start_addr, end,
174185
entry->type & RT_ROM ? "ROM," : "",

0 commit comments

Comments
 (0)