Skip to content

Commit a59835e

Browse files
committed
Refactor rb_shape_get_iv_index to take a shape_id_t
Further reduce exposure of `rb_shape_t`.
1 parent e535f82 commit a59835e

File tree

6 files changed

+51
-55
lines changed

6 files changed

+51
-55
lines changed

shape.c

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,35 @@ rb_shape_get_next_iv_shape(shape_id_t shape_id, ID id)
731731
return rb_shape_id(next_shape);
732732
}
733733

734+
static bool
735+
shape_get_iv_index(rb_shape_t *shape, ID id, attr_index_t *value)
736+
{
737+
while (shape->parent_id != INVALID_SHAPE_ID) {
738+
if (shape->edge_name == id) {
739+
enum shape_type shape_type;
740+
shape_type = (enum shape_type)shape->type;
741+
742+
switch (shape_type) {
743+
case SHAPE_IVAR:
744+
RUBY_ASSERT(shape->next_field_index > 0);
745+
*value = shape->next_field_index - 1;
746+
return true;
747+
case SHAPE_ROOT:
748+
case SHAPE_T_OBJECT:
749+
return false;
750+
case SHAPE_OBJ_TOO_COMPLEX:
751+
case SHAPE_OBJ_ID:
752+
case SHAPE_FROZEN:
753+
rb_bug("Ivar should not exist on transition");
754+
}
755+
}
756+
757+
shape = RSHAPE(shape->parent_id);
758+
}
759+
760+
return false;
761+
}
762+
734763
static inline rb_shape_t *
735764
shape_get_next(rb_shape_t *shape, VALUE obj, ID id, bool emit_warnings)
736765
{
@@ -741,7 +770,7 @@ shape_get_next(rb_shape_t *shape, VALUE obj, ID id, bool emit_warnings)
741770

742771
#if RUBY_DEBUG
743772
attr_index_t index;
744-
if (rb_shape_get_iv_index(shape, id, &index)) {
773+
if (shape_get_iv_index(shape, id, &index)) {
745774
rb_bug("rb_shape_get_next: trying to create ivar that already exists at index %u", index);
746775
}
747776
#endif
@@ -803,14 +832,14 @@ bool
803832
rb_shape_get_iv_index_with_hint(shape_id_t shape_id, ID id, attr_index_t *value, shape_id_t *shape_id_hint)
804833
{
805834
attr_index_t index_hint = *value;
806-
rb_shape_t *shape = RSHAPE(shape_id);
807-
rb_shape_t *initial_shape = shape;
808835

809836
if (*shape_id_hint == INVALID_SHAPE_ID) {
810837
*shape_id_hint = shape_id;
811-
return rb_shape_get_iv_index(shape, id, value);
838+
return rb_shape_get_iv_index(shape_id, id, value);
812839
}
813840

841+
rb_shape_t *shape = RSHAPE(shape_id);
842+
rb_shape_t *initial_shape = shape;
814843
rb_shape_t *shape_hint = RSHAPE(*shape_id_hint);
815844

816845
// We assume it's likely shape_id_hint and shape_id have a close common
@@ -850,36 +879,7 @@ rb_shape_get_iv_index_with_hint(shape_id_t shape_id, ID id, attr_index_t *value,
850879
shape = initial_shape;
851880
}
852881
*shape_id_hint = shape_id;
853-
return rb_shape_get_iv_index(shape, id, value);
854-
}
855-
856-
static bool
857-
shape_get_iv_index(rb_shape_t *shape, ID id, attr_index_t *value)
858-
{
859-
while (shape->parent_id != INVALID_SHAPE_ID) {
860-
if (shape->edge_name == id) {
861-
enum shape_type shape_type;
862-
shape_type = (enum shape_type)shape->type;
863-
864-
switch (shape_type) {
865-
case SHAPE_IVAR:
866-
RUBY_ASSERT(shape->next_field_index > 0);
867-
*value = shape->next_field_index - 1;
868-
return true;
869-
case SHAPE_ROOT:
870-
case SHAPE_T_OBJECT:
871-
return false;
872-
case SHAPE_OBJ_TOO_COMPLEX:
873-
case SHAPE_OBJ_ID:
874-
case SHAPE_FROZEN:
875-
rb_bug("Ivar should not exist on transition");
876-
}
877-
}
878-
879-
shape = RSHAPE(shape->parent_id);
880-
}
881-
882-
return false;
882+
return shape_get_iv_index(shape, id, value);
883883
}
884884

885885
static bool
@@ -909,11 +909,13 @@ shape_cache_get_iv_index(rb_shape_t *shape, ID id, attr_index_t *value)
909909
}
910910

911911
bool
912-
rb_shape_get_iv_index(rb_shape_t *shape, ID id, attr_index_t *value)
912+
rb_shape_get_iv_index(shape_id_t shape_id, ID id, attr_index_t *value)
913913
{
914+
rb_shape_t *shape = RSHAPE(shape_id);
915+
914916
// It doesn't make sense to ask for the index of an IV that's stored
915917
// on an object that is "too complex" as it uses a hash for storing IVs
916-
RUBY_ASSERT(rb_shape_id(shape) != ROOT_TOO_COMPLEX_SHAPE_ID);
918+
RUBY_ASSERT(!rb_shape_too_complex_p(shape));
917919

918920
if (!shape_cache_get_iv_index(shape, id, value)) {
919921
// If it wasn't in the ancestor cache, then don't do a linear search

shape.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ int32_t rb_shape_id_offset(void);
122122
RUBY_FUNC_EXPORTED rb_shape_t *rb_shape_lookup(shape_id_t shape_id);
123123
RUBY_FUNC_EXPORTED shape_id_t rb_obj_shape_id(VALUE obj);
124124
shape_id_t rb_shape_get_next_iv_shape(shape_id_t shape_id, ID id);
125-
bool rb_shape_get_iv_index(rb_shape_t *shape, ID id, attr_index_t *value);
125+
bool rb_shape_get_iv_index(shape_id_t shape_id, ID id, attr_index_t *value);
126126
bool rb_shape_get_iv_index_with_hint(shape_id_t shape_id, ID id, attr_index_t *value, shape_id_t *shape_id_hint);
127127
RUBY_FUNC_EXPORTED bool rb_shape_obj_too_complex_p(VALUE obj);
128128
bool rb_shape_too_complex_p(rb_shape_t *shape);

variable.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,8 +1376,6 @@ rb_ivar_lookup(VALUE obj, ID id, VALUE undef)
13761376

13771377
shape_id_t shape_id;
13781378
VALUE * ivar_list;
1379-
rb_shape_t * shape;
1380-
13811379
shape_id = RBASIC_SHAPE_ID(obj);
13821380

13831381
switch (BUILTIN_TYPE(obj)) {
@@ -1399,8 +1397,7 @@ rb_ivar_lookup(VALUE obj, ID id, VALUE undef)
13991397
}
14001398
else {
14011399
attr_index_t index = 0;
1402-
shape = RSHAPE(shape_id);
1403-
found = rb_shape_get_iv_index(shape, id, &index);
1400+
found = rb_shape_get_iv_index(shape_id, id, &index);
14041401

14051402
if (found) {
14061403
ivar_list = RCLASS_PRIME_FIELDS(obj);
@@ -1463,8 +1460,7 @@ rb_ivar_lookup(VALUE obj, ID id, VALUE undef)
14631460
}
14641461

14651462
attr_index_t index = 0;
1466-
shape = RSHAPE(shape_id);
1467-
if (rb_shape_get_iv_index(shape, id, &index)) {
1463+
if (rb_shape_get_iv_index(shape_id, id, &index)) {
14681464
return ivar_list[index];
14691465
}
14701466

@@ -1717,15 +1713,16 @@ general_ivar_set(VALUE obj, ID id, VALUE val, void *data,
17171713
.existing = true
17181714
};
17191715

1720-
rb_shape_t *current_shape = rb_obj_shape(obj);
1716+
shape_id_t current_shape_id = RBASIC_SHAPE_ID(obj);
17211717

1722-
if (UNLIKELY(rb_shape_too_complex_p(current_shape))) {
1718+
if (UNLIKELY(rb_shape_id_too_complex_p(current_shape_id))) {
17231719
goto too_complex;
17241720
}
17251721

17261722
attr_index_t index;
1727-
if (!rb_shape_get_iv_index(current_shape, id, &index)) {
1723+
if (!rb_shape_get_iv_index(current_shape_id, id, &index)) {
17281724
result.existing = false;
1725+
rb_shape_t *current_shape = RSHAPE(current_shape_id);
17291726

17301727
index = current_shape->next_field_index;
17311728
if (index >= SHAPE_MAX_FIELDS) {
@@ -2172,7 +2169,7 @@ rb_ivar_defined(VALUE obj, ID id)
21722169
return Qtrue;
21732170
}
21742171
else {
2175-
return RBOOL(rb_shape_get_iv_index(rb_obj_shape(obj), id, &index));
2172+
return RBOOL(rb_shape_get_iv_index(RBASIC_SHAPE_ID(obj), id, &index));
21762173
}
21772174
}
21782175

yjit/src/codegen.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2904,9 +2904,8 @@ fn gen_get_ivar(
29042904

29052905
let ivar_index = unsafe {
29062906
let shape_id = comptime_receiver.shape_id_of();
2907-
let shape = rb_shape_lookup(shape_id);
29082907
let mut ivar_index: u32 = 0;
2909-
if rb_shape_get_iv_index(shape, ivar_name, &mut ivar_index) {
2908+
if rb_shape_get_iv_index(shape_id, ivar_name, &mut ivar_index) {
29102909
Some(ivar_index as usize)
29112910
} else {
29122911
None
@@ -3107,9 +3106,8 @@ fn gen_set_ivar(
31073106
let shape_too_complex = comptime_receiver.shape_too_complex();
31083107
let ivar_index = if !shape_too_complex {
31093108
let shape_id = comptime_receiver.shape_id_of();
3110-
let shape = unsafe { rb_shape_lookup(shape_id) };
31113109
let mut ivar_index: u32 = 0;
3112-
if unsafe { rb_shape_get_iv_index(shape, ivar_name, &mut ivar_index) } {
3110+
if unsafe { rb_shape_get_iv_index(shape_id, ivar_name, &mut ivar_index) } {
31133111
Some(ivar_index as usize)
31143112
} else {
31153113
None
@@ -3397,9 +3395,8 @@ fn gen_definedivar(
33973395

33983396
let shape_id = comptime_receiver.shape_id_of();
33993397
let ivar_exists = unsafe {
3400-
let shape = rb_shape_lookup(shape_id);
34013398
let mut ivar_index: u32 = 0;
3402-
rb_shape_get_iv_index(shape, ivar_name, &mut ivar_index)
3399+
rb_shape_get_iv_index(shape_id, ivar_name, &mut ivar_index)
34033400
};
34043401

34053402
// Guard heap object (recv_opnd must be used before stack_pop)

yjit/src/cruby_bindings.inc.rs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

zjit/src/cruby_bindings.inc.rs

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)