From 22e9fa81ca27243922ec9a32fdd96065becb35f9 Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Mon, 30 Dec 2024 11:10:51 +0100 Subject: [PATCH] gc/default/default.c: don't call `malloc_usable_size` when hint is present Depending on the allocator, `malloc_usable_size` may be very cheap or quite expensive. On `macOS` for instance, it's about as expensive as `malloc`. In many case we call `objspace_malloc_size` with as size we initially requested as `hint`. The real usable size may be a few bytes bigger, but since we only use that data to feed GC heuristics, I don't think it's very important to be perfectly accurate. It would make sense to call `malloc_usable_size` after growing a String or Array to use the extra capacity, but here we don't do that, so the call isn't worth its cost. --- gc/default/default.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/gc/default/default.c b/gc/default/default.c index 94828be55d..e8d43986b9 100644 --- a/gc/default/default.c +++ b/gc/default/default.c @@ -7936,10 +7936,11 @@ static inline size_t objspace_malloc_size(rb_objspace_t *objspace, void *ptr, size_t hint) { #ifdef HAVE_MALLOC_USABLE_SIZE - return malloc_usable_size(ptr); -#else - return hint; + if (!hint) { + hint = malloc_usable_size(ptr); + } #endif + return hint; } enum memop_type {