Skip to content

Commit f9966b9

Browse files
committed
Get rid of gen_fields_tbl.fields_count
This data is redundant because the shape already contains both the length and capacity of the object's fields. So it both waste space and create the possibility of a desync between the two. We also do not need to initialize everything to Qundef, this seem to be a left-over from pre-shape instance variables.
1 parent e210a70 commit f9966b9

File tree

4 files changed

+13
-31
lines changed

4 files changed

+13
-31
lines changed

gc.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4093,7 +4093,8 @@ vm_weak_table_gen_fields_foreach(st_data_t key, st_data_t value, st_data_t data)
40934093
);
40944094
}
40954095
else {
4096-
for (uint32_t i = 0; i < fields_tbl->as.shape.fields_count; i++) {
4096+
uint32_t fields_count = RSHAPE_LEN(RBASIC_SHAPE_ID((VALUE)key));
4097+
for (uint32_t i = 0; i < fields_count; i++) {
40974098
if (SPECIAL_CONST_P(fields_tbl->as.shape.fields[i])) continue;
40984099

40994100
int ivar_ret = iter_data->callback(fields_tbl->as.shape.fields[i], iter_data->data);

ractor.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1658,10 +1658,9 @@ obj_traverse_replace_i(VALUE obj, struct obj_traverse_replace_data *data)
16581658
if (d.stop) return 1;
16591659
}
16601660
else {
1661-
for (uint32_t i = 0; i < fields_tbl->as.shape.fields_count; i++) {
1662-
if (!UNDEF_P(fields_tbl->as.shape.fields[i])) {
1663-
CHECK_AND_REPLACE(fields_tbl->as.shape.fields[i]);
1664-
}
1661+
uint32_t fields_count = RSHAPE_LEN(RBASIC_SHAPE_ID(obj));
1662+
for (uint32_t i = 0; i < fields_count; i++) {
1663+
CHECK_AND_REPLACE(fields_tbl->as.shape.fields[i]);
16651664
}
16661665
}
16671666
}

variable.c

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1228,19 +1228,10 @@ gen_fields_tbl_bytes(size_t n)
12281228
}
12291229

12301230
static struct gen_fields_tbl *
1231-
gen_fields_tbl_resize(struct gen_fields_tbl *old, uint32_t n)
1231+
gen_fields_tbl_resize(struct gen_fields_tbl *old, uint32_t new_capa)
12321232
{
1233-
RUBY_ASSERT(n > 0);
1234-
1235-
uint32_t len = old ? old->as.shape.fields_count : 0;
1236-
struct gen_fields_tbl *fields_tbl = xrealloc(old, gen_fields_tbl_bytes(n));
1237-
1238-
fields_tbl->as.shape.fields_count = n;
1239-
for (; len < n; len++) {
1240-
fields_tbl->as.shape.fields[len] = Qundef;
1241-
}
1242-
1243-
return fields_tbl;
1233+
RUBY_ASSERT(new_capa > 0);
1234+
return xrealloc(old, gen_fields_tbl_bytes(new_capa));
12441235
}
12451236

12461237
void
@@ -1253,7 +1244,8 @@ rb_mark_generic_ivar(VALUE obj)
12531244
rb_mark_tbl_no_pin(fields_tbl->as.complex.table);
12541245
}
12551246
else {
1256-
for (uint32_t i = 0; i < fields_tbl->as.shape.fields_count; i++) {
1247+
uint32_t fields_count = RSHAPE_LEN(RBASIC_SHAPE_ID(obj));
1248+
for (uint32_t i = 0; i < fields_count; i++) {
12571249
rb_gc_mark_movable(fields_tbl->as.shape.fields[i]);
12581250
}
12591251
}
@@ -1290,7 +1282,7 @@ rb_generic_ivar_memsize(VALUE obj)
12901282
return sizeof(struct gen_fields_tbl) + st_memsize(fields_tbl->as.complex.table);
12911283
}
12921284
else {
1293-
return gen_fields_tbl_bytes(fields_tbl->as.shape.fields_count);
1285+
return gen_fields_tbl_bytes(RSHAPE_CAPACITY(RBASIC_SHAPE_ID(obj)));
12941286
}
12951287
}
12961288
return 0;
@@ -1299,21 +1291,12 @@ rb_generic_ivar_memsize(VALUE obj)
12991291
static size_t
13001292
gen_fields_tbl_count(VALUE obj, const struct gen_fields_tbl *fields_tbl)
13011293
{
1302-
uint32_t i;
1303-
size_t n = 0;
1304-
13051294
if (rb_shape_obj_too_complex_p(obj)) {
1306-
n = st_table_size(fields_tbl->as.complex.table);
1295+
return st_table_size(fields_tbl->as.complex.table);
13071296
}
13081297
else {
1309-
for (i = 0; i < fields_tbl->as.shape.fields_count; i++) {
1310-
if (!UNDEF_P(fields_tbl->as.shape.fields[i])) {
1311-
n++;
1312-
}
1313-
}
1298+
return RSHAPE_LEN(RBASIC_SHAPE_ID(obj));
13141299
}
1315-
1316-
return n;
13171300
}
13181301

13191302
VALUE

variable.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
struct gen_fields_tbl {
1616
union {
1717
struct {
18-
uint32_t fields_count;
1918
VALUE fields[1];
2019
} shape;
2120
struct {

0 commit comments

Comments
 (0)