Refactor rb_shape_depth to take an ID rather than a pointer.

As well as `rb_shape_edges_count` and `rb_shape_memsize`.
This commit is contained in:
Jean Boussier 2025-05-08 18:20:35 +02:00
parent f8b3fc520f
commit e4f97ce387
Notes: git 2025-05-09 08:23:13 +00:00
4 changed files with 19 additions and 18 deletions

View File

@ -786,7 +786,7 @@ shape_i(rb_shape_t *shape, void *data)
{
struct dump_config *dc = (struct dump_config *)data;
size_t shape_id = rb_shape_id(shape);
shape_id_t shape_id = rb_shape_id(shape);
if (shape_id < dc->shapes_since) {
return;
}
@ -803,7 +803,7 @@ shape_i(rb_shape_t *shape, void *data)
}
dump_append(dc, ", \"depth\":");
dump_append_sizet(dc, rb_shape_depth(shape));
dump_append_sizet(dc, rb_shape_depth(shape_id));
switch((enum shape_type)shape->type) {
case SHAPE_ROOT:
@ -831,10 +831,10 @@ shape_i(rb_shape_t *shape, void *data)
}
dump_append(dc, ", \"edges\":");
dump_append_sizet(dc, rb_shape_edges_count(shape));
dump_append_sizet(dc, rb_shape_edges_count(shape_id));
dump_append(dc, ", \"memsize\":");
dump_append_sizet(dc, rb_shape_memsize(shape));
dump_append_sizet(dc, rb_shape_memsize(shape_id));
dump_append(dc, "}\n");
}

View File

@ -718,15 +718,13 @@ w_ivar_each(VALUE obj, st_index_t num, struct dump_call_arg *arg)
if (!num) return;
rb_ivar_foreach(obj, w_obj_each, (st_data_t)&ivarg);
if (shape_id != rb_shape_get_shape_id(arg->obj)) {
rb_shape_t * expected_shape = rb_shape_get_shape_by_id(shape_id);
rb_shape_t * actual_shape = rb_shape_get_shape(arg->obj);
shape_id_t actual_shape_id = rb_shape_get_shape_id(arg->obj);
if (shape_id != actual_shape_id) {
// If the shape tree got _shorter_ then we probably removed an IV
// If the shape tree got longer, then we probably added an IV.
// The exception message might not be accurate when someone adds and
// removes the same number of IVs, but they will still get an exception
if (rb_shape_depth(expected_shape) > rb_shape_depth(actual_shape)) {
if (rb_shape_depth(shape_id) > rb_shape_depth(rb_shape_get_shape_id(arg->obj))) {
rb_raise(rb_eRuntimeError, "instance variable removed from %"PRIsVALUE" instance",
CLASS_OF(arg->obj));
}

15
shape.c
View File

@ -382,9 +382,10 @@ rb_shape_get_shape_id(VALUE obj)
}
size_t
rb_shape_depth(rb_shape_t *shape)
rb_shape_depth(shape_id_t shape_id)
{
size_t depth = 1;
rb_shape_t *shape = rb_shape_get_shape_by_id(shape_id);
while (shape->parent_id != INVALID_SHAPE_ID) {
depth++;
@ -1120,8 +1121,9 @@ rb_shape_too_complex_p(rb_shape_t *shape)
}
size_t
rb_shape_edges_count(rb_shape_t *shape)
rb_shape_edges_count(shape_id_t shape_id)
{
rb_shape_t *shape = rb_shape_get_shape_by_id(shape_id);
if (shape->edges) {
if (SINGLE_CHILD_P(shape->edges)) {
return 1;
@ -1134,8 +1136,10 @@ rb_shape_edges_count(rb_shape_t *shape)
}
size_t
rb_shape_memsize(rb_shape_t *shape)
rb_shape_memsize(shape_id_t shape_id)
{
rb_shape_t *shape = rb_shape_get_shape_by_id(shape_id);
size_t memsize = sizeof(rb_shape_t);
if (shape->edges && !SINGLE_CHILD_P(shape->edges)) {
memsize += rb_id_table_memsize(shape->edges);
@ -1244,9 +1248,8 @@ rb_shape_edge_name(rb_shape_t *shape)
static VALUE
rb_shape_export_depth(VALUE self)
{
rb_shape_t *shape;
shape = rb_shape_get_shape_by_id(NUM2INT(rb_struct_getmember(self, rb_intern("id"))));
return SIZET2NUM(rb_shape_depth(shape));
shape_id_t shape_id = NUM2INT(rb_struct_getmember(self, rb_intern("id")));
return SIZET2NUM(rb_shape_depth(shape_id));
}
static VALUE

View File

@ -248,9 +248,9 @@ VALUE rb_obj_debug_shape(VALUE self, VALUE obj);
RUBY_SYMBOL_EXPORT_BEGIN
typedef void each_shape_callback(rb_shape_t *shape, void *data);
void rb_shape_each_shape(each_shape_callback callback, void *data);
size_t rb_shape_memsize(rb_shape_t *shape);
size_t rb_shape_edges_count(rb_shape_t *shape);
size_t rb_shape_depth(rb_shape_t *shape);
size_t rb_shape_memsize(shape_id_t shape);
size_t rb_shape_edges_count(shape_id_t shape_id);
size_t rb_shape_depth(shape_id_t shape_id);
shape_id_t rb_shape_id(rb_shape_t *shape);
RUBY_SYMBOL_EXPORT_END