From 3135eddb4e8c6975b6fa5345f15fdaa55257851a Mon Sep 17 00:00:00 2001 From: Jean Boussier Date: Fri, 9 May 2025 12:02:48 +0200 Subject: [PATCH] Refactor `FIRST_T_OBJECT_SHAPE_ID` to not be used outside `shape.c` --- gc.c | 2 +- object.c | 2 +- shape.c | 2 +- shape.h | 6 ++++++ 4 files changed, 9 insertions(+), 3 deletions(-) diff --git a/gc.c b/gc.c index 4bff60b743..43cd87e9de 100644 --- a/gc.c +++ b/gc.c @@ -384,7 +384,7 @@ rb_gc_rebuild_shape(VALUE obj, size_t heap_id) return (uint32_t)orig_shape_id; } - shape_id_t initial_shape_id = (shape_id_t)(heap_id + FIRST_T_OBJECT_SHAPE_ID); + shape_id_t initial_shape_id = rb_shape_root(heap_id); shape_id_t new_shape_id = rb_shape_traverse_from_new_root(initial_shape_id, orig_shape_id); if (new_shape_id == INVALID_SHAPE_ID) { diff --git a/object.c b/object.c index e21f579364..ed0a3f812e 100644 --- a/object.c +++ b/object.c @@ -135,7 +135,7 @@ rb_class_allocate_instance(VALUE klass) RUBY_ASSERT(rb_obj_shape(obj)->type == SHAPE_ROOT); // Set the shape to the specific T_OBJECT shape. - ROBJECT_SET_SHAPE_ID(obj, (shape_id_t)(rb_gc_heap_id_for_size(size) + FIRST_T_OBJECT_SHAPE_ID)); + ROBJECT_SET_SHAPE_ID(obj, rb_shape_root(rb_gc_heap_id_for_size(size))); #if RUBY_DEBUG RUBY_ASSERT(!rb_shape_obj_too_complex_p(obj)); diff --git a/shape.c b/shape.c index 5755446cad..e2a6180ad9 100644 --- a/shape.c +++ b/shape.c @@ -1419,7 +1419,7 @@ Init_default_shapes(void) t_object_shape->capacity = (uint32_t)((sizes[i] - offsetof(struct RObject, as.ary)) / sizeof(VALUE)); t_object_shape->edges = rb_id_table_create(0); t_object_shape->ancestor_index = LEAF; - RUBY_ASSERT(rb_shape_id(t_object_shape) == (shape_id_t)(i + FIRST_T_OBJECT_SHAPE_ID)); + RUBY_ASSERT(rb_shape_id(t_object_shape) == rb_shape_root(i)); } // Prebuild TOO_COMPLEX variations so that they already exist if we ever need them after we diff --git a/shape.h b/shape.h index 329ade6f8f..359df83eec 100644 --- a/shape.h +++ b/shape.h @@ -187,6 +187,12 @@ rb_shape_canonical_p(rb_shape_t *shape) return !shape->flags; } +static inline shape_id_t +rb_shape_root(size_t heap_id) +{ + return (shape_id_t)(heap_id + FIRST_T_OBJECT_SHAPE_ID); +} + static inline uint32_t ROBJECT_FIELDS_CAPACITY(VALUE obj) {