8000 vm_getivar: normalize shape_id to ignore frozen state · ruby/ruby@fa04a3e · GitHub
[go: up one dir, main page]

Skip to content

Commit fa04a3e

Browse files
committed
vm_getivar: normalize shape_id to ignore frozen state
Freezing an object changes its `shape_id` This is necessary so that `setivar` routines can use the `shape_id` as a cache key and save on checking the frozen status every time. However for `getivar` routines, this causes needless cache misses. By clearing that bit we increase hit rate in codepaths that see both frozen and mutable objects.
1 parent 7e969b8 commit fa04a3e

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

shape.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ STATIC_ASSERT(shape_id_num_bits, SHAPE_ID_NUM_BITS == sizeof(shape_id_t) * CHAR_
2727
#define SHAPE_ID_OFFSET_MASK (SHAPE_BUFFER_SIZE - 1)
2828
#define SHAPE_ID_FLAGS_MASK (shape_id_t)(((1 << (SHAPE_ID_NUM_BITS - SHAPE_ID_OFFSET_NUM_BITS)) - 1) << SHAPE_ID_OFFSET_NUM_BITS)
2929
#define SHAPE_ID_FL_FROZEN (SHAPE_FL_FROZEN << SHAPE_ID_OFFSET_NUM_BITS)
30+
#define SHAPE_ID_READ_ONLY_MASK (~SHAPE_ID_FL_FROZEN)
3031

3132
typedef uint32_t redblack_id_t;
3233

@@ -115,6 +116,14 @@ RBASIC_SHAPE_ID(VALUE obj)
115116
#endif
116117
}
117118

119+
// Same as RBASIC_SHAPE_ID but with flags that have no impact
120+
// on reads removed. e.g. Remove FL_FROZEN.
121+
static inline shape_id_t
122+
RBASIC_SHAPE_ID_FOR_READ(VALUE obj)
123+
{
124+
return RBASIC_SHAPE_ID(obj) & SHAPE_ID_READ_ONLY_MASK;
125+
}
126+
118127
static inline void
119128
RBASIC_SET_SHAPE_ID(VALUE obj, shape_id_t shape_id)
120129
{

vm_insnhelper.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,14 +1215,13 @@ vm_getivar(VALUE obj, ID id, const rb_iseq_t *iseq, IVC ic, const struct rb_call
12151215
{
12161216
#if OPT_IC_FOR_IVAR
12171217
VALUE val = Qundef;
1218-
shape_id_t shape_id;
12191218
VALUE * ivar_list;
12201219

12211220
if (SPECIAL_CONST_P(obj)) {
12221221
return default_value;
12231222
}
12241223

1225-
shape_id = RBASIC_SHAPE_ID(obj);
1224+
shape_id_t shape_id = RBASIC_SHAPE_ID_FOR_READ(obj);
12261225

12271226
switch (BUILTIN_TYPE(obj)) {
12281227
case T_OBJECT:

0 commit comments

Comments
 (0)
0