From 8dc1d3c4662e9009ed56483fd4254ee1154b6e23 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Fri, 17 Jan 2020 17:19:56 +0100 Subject: mmap: Do not look for empty pool unless we need to Given that we have a maximum of 12 pools, and much more items to insert, it does not make sense to have two branches in the hot path. Move the search for an empty pool into the unlikely case that no matching pool has been created yet - a condition that is guaranteed to only happens up to 12 times. --- apt-pkg/contrib/mmap.cc | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'apt-pkg') diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index ee6a21c83..020491172 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -357,25 +357,26 @@ unsigned long DynamicMMap::Allocate(unsigned long ItemSize) // Look for a matching pool entry Pool *I; - Pool *Empty = 0; for (I = Pools; I != Pools + PoolCount; ++I) { - if (I->ItemSize == 0) - Empty = I; if (I->ItemSize == ItemSize) break; } - // No pool is allocated, use an unallocated one - if (I == Pools + PoolCount) + // No pool is allocated, use an unallocated one. + if (unlikely(I == Pools + PoolCount)) { + for (I = Pools; I != Pools + PoolCount; ++I) + { + if (I->ItemSize == 0) + break; + } // Woops, we ran out, the calling code should allocate more. - if (Empty == 0) + if (I == Pools + PoolCount) { _error->Error("Ran out of allocation pools"); return 0; } - - I = Empty; + I->ItemSize = ItemSize; I->Count = 0; } -- cgit v1.2.3