Skip to content

Commit 012138e

Browse files
authored
Small sizeclass lookup improvement, (#777)
* Fix off by one in deciding where to switch from small to large. * Improve codegen on Windows by making it simpler for compiler to remove a redundant branch
1 parent 2d33e4f commit 012138e

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/snmalloc/mem/sizeclasstable.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace snmalloc
3232
}
3333

3434
constexpr size_t NUM_SMALL_SIZECLASSES =
35-
size_to_sizeclass_const(MAX_SMALL_SIZECLASS_SIZE);
35+
size_to_sizeclass_const(MAX_SMALL_SIZECLASS_SIZE) + 1;
3636

3737
// Large classes range from [MAX_SMALL_SIZECLASS_SIZE, ADDRESS_SPACE).
3838
constexpr size_t NUM_LARGE_CLASSES =
@@ -41,7 +41,7 @@ namespace snmalloc
4141
// How many bits are required to represent either a large or a small
4242
// sizeclass.
4343
constexpr size_t TAG_SIZECLASS_BITS = bits::max<size_t>(
44-
bits::next_pow2_bits_const(NUM_SMALL_SIZECLASSES + 1),
44+
bits::next_pow2_bits_const(NUM_SMALL_SIZECLASSES),
4545
bits::next_pow2_bits_const(NUM_LARGE_CLASSES + 1));
4646

4747
// Number of bits required to represent a tagged sizeclass that can be
@@ -410,7 +410,7 @@ namespace snmalloc
410410
}
411411

412412
constexpr size_t sizeclass_lookup_size =
413-
sizeclass_lookup_index(MAX_SMALL_SIZECLASS_SIZE);
413+
sizeclass_lookup_index(MAX_SMALL_SIZECLASS_SIZE) + 1;
414414

415415
/**
416416
* This struct is used to statically initialise a table for looking up
@@ -470,9 +470,10 @@ namespace snmalloc
470470

471471
constexpr smallsizeclass_t size_to_sizeclass(size_t size)
472472
{
473-
auto index = sizeclass_lookup_index(size);
474-
if (index < sizeclass_lookup_size)
473+
if (SNMALLOC_LIKELY(is_small_sizeclass(size)))
475474
{
475+
auto index = sizeclass_lookup_index(size);
476+
SNMALLOC_ASSERT(index < sizeclass_lookup_size);
476477
return sizeclass_lookup.table[index];
477478
}
478479

0 commit comments

Comments
 (0)