Skip to content

Commit

Permalink
Modify heuristic for adding new slabs. (#429)
Browse files Browse the repository at this point in the history
If there is only one slab remaining, then we probabalisticly allocator a
new one. If a slab is barely in use, then this could cause us to
effectively double the number of slabs in use.

This commit checks if the remaining slab has enough remaining elements
to provide randomisation.
  • Loading branch information
mjp41 authored Nov 25, 2021
1 parent c299826 commit 5fd3288
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/ds/seqset.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,5 +159,13 @@ namespace snmalloc
v.end = &(item->next);
}
}

/**
* Peek at next element in the set.
*/
SNMALLOC_FAST_PATH const T* peek()
{
return v.head;
}
};
} // namespace snmalloc
13 changes: 9 additions & 4 deletions src/mem/corealloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -689,11 +689,16 @@ namespace snmalloc
{
#ifdef SNMALLOC_CHECK_CLIENT
// Occassionally don't use the last list.
if (
SNMALLOC_UNLIKELY(alloc_classes[sizeclass].length == 1) &&
(entropy.next_bit() == 0))
if (SNMALLOC_UNLIKELY(alloc_classes[sizeclass].length == 1))
{
return small_alloc_slow<zero_mem>(sizeclass, fast_free_list);
// If the slab has a lot of free space, then we shouldn't allocate a
// new slab.
auto min = alloc_classes[sizeclass]
.available.peek()
->free_queue.min_list_length();
if ((min * 2) < threshold_for_waking_slab(sizeclass))
if (entropy.next_bit() == 0)
return small_alloc_slow<zero_mem>(sizeclass, fast_free_list);
}
#endif

Expand Down
14 changes: 14 additions & 0 deletions src/mem/freelist.h
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,20 @@ namespace snmalloc
UNUSED(domesticate);
#endif
}

/**
* Returns length of the shorter free list.
*
* This method is only usable if the free list is adding randomisation
* as that is when it has two lists.
*/
template<bool RANDOM_ = RANDOM>
[[nodiscard]] std::enable_if_t<RANDOM_, size_t> min_list_length() const
{
static_assert(RANDOM_ == RANDOM, "Don't set SFINAE parameter!");

return length[0] < length[1] ? length[0] : length[1];
}
};
} // namespace freelist
} // namespace snmalloc

0 comments on commit 5fd3288

Please sign in to comment.