Skip to content

Commit 2fd891f

Browse files
Use the pre-processor to generate slot sizes and reciprocals
1 parent 80e3a8d commit 2fd891f

1 file changed

Lines changed: 21 additions & 25 deletions

File tree

gc/default/default.c

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -194,33 +194,28 @@ static RB_THREAD_LOCAL_SPECIFIER int malloc_increase_local;
194194
# endif
195195
#endif
196196

197+
/* The reciprocal table and pool_slot_sizes array are both generated from this
198+
* single definition, so they can never get out of sync. */
199+
#if SIZEOF_VALUE >= 8
200+
# define EACH_POOL_SLOT_SIZE(SLOT) \
201+
SLOT(32) SLOT(40) SLOT(64) SLOT(80) SLOT(96) SLOT(128) \
202+
SLOT(160) SLOT(256) SLOT(512) SLOT(640) SLOT(768) SLOT(1024)
203+
#else
204+
# define EACH_POOL_SLOT_SIZE(SLOT) \
205+
SLOT(32) SLOT(64) SLOT(128) SLOT(256) SLOT(512)
206+
#endif
207+
197208
/* Precomputed reciprocals for fast slot index calculation.
198209
* For slot size d: reciprocal = ceil(2^48 / d).
199210
* Then offset / d == (uint32_t)((offset * reciprocal) >> 48)
200211
* for all offset < HEAP_PAGE_SIZE. */
201212
#define SLOT_RECIPROCAL_SHIFT 48
213+
#define SLOT_RECIPROCAL(size) (((1ULL << SLOT_RECIPROCAL_SHIFT) + (size) - 1) / (size))
202214

203215
static const uint64_t heap_slot_reciprocal_table[HEAP_COUNT] = {
204-
#if SIZEOF_VALUE >= 8
205-
/* 32 */ (1ULL << 48) / 32,
206-
/* 40 */ (1ULL << 48) / 40 + 1,
207-
/* 64 */ (1ULL << 48) / 64,
208-
/* 80 */ (1ULL << 48) / 80 + 1,
209-
/* 96 */ (1ULL << 48) / 96 + 1,
210-
/* 128 */ (1ULL << 48) / 128,
211-
/* 160 */ (1ULL << 48) / 160 + 1,
212-
/* 256 */ (1ULL << 48) / 256,
213-
/* 512 */ (1ULL << 48) / 512,
214-
/* 640 */ (1ULL << 48) / 640 + 1,
215-
/* 768 */ (1ULL << 48) / 768 + 1,
216-
/* 1024 */ (1ULL << 48) / 1024,
217-
#else
218-
/* 32 */ (1ULL << 48) / 32,
219-
/* 64 */ (1ULL << 48) / 64,
220-
/* 128 */ (1ULL << 48) / 128,
221-
/* 256 */ (1ULL << 48) / 256,
222-
/* 512 */ (1ULL << 48) / 512,
223-
#endif
216+
#define SLOT(size) SLOT_RECIPROCAL(size),
217+
EACH_POOL_SLOT_SIZE(SLOT)
218+
#undef SLOT
224219
};
225220
typedef struct ractor_newobj_heap_cache {
226221
struct free_slot *freelist;
@@ -723,15 +718,16 @@ size_t rb_gc_impl_obj_slot_size(VALUE obj);
723718

724719
#define RVALUE_SLOT_SIZE (sizeof(struct RBasic) + sizeof(VALUE[RBIMPL_RVALUE_EMBED_LEN_MAX]) + RVALUE_OVERHEAD)
725720

726-
#if SIZEOF_VALUE >= 8
727721
static const size_t pool_slot_sizes[HEAP_COUNT] = {
728-
32, 40, 64, 80, 96, 128, 160, 256, 512, 640, 768, 1024,
722+
#define SLOT(size) size,
723+
EACH_POOL_SLOT_SIZE(SLOT)
724+
#undef SLOT
729725
};
726+
727+
728+
#if SIZEOF_VALUE >= 8
730729
static uint8_t size_to_heap_idx[1024 / 8 + 1];
731730
#else
732-
static const size_t pool_slot_sizes[HEAP_COUNT] = {
733-
32, 64, 128, 256, 512,
734-
};
735731
static uint8_t size_to_heap_idx[512 / 8 + 1];
736732
#endif
737733

0 commit comments

Comments
 (0)