Skip to content

Commit 749bda9

Browse files
committed
Refactor attr_index_t caches
Ensure the same helpers are used for packing and unpacking.
1 parent 658fcbe commit 749bda9

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

shape.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ typedef uint32_t redblack_id_t;
3030
# define SHAPE_MAX_VARIATIONS 8
3131

3232
# define INVALID_SHAPE_ID (((uintptr_t)1 << SHAPE_ID_NUM_BITS) - 1)
33+
#define ATTR_INDEX_NOT_SET (attr_index_t)-1
3334

3435
#define ROOT_SHAPE_ID 0x0
3536
#define SPECIAL_CONST_SHAPE_ID 0x1

vm_callinfo.h

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -416,21 +416,24 @@ vm_cc_call(const struct rb_callcache *cc)
416416
}
417417

418418
static inline void
419-
vm_cc_atomic_shape_and_index(const struct rb_callcache *cc, shape_id_t * shape_id, attr_index_t * index)
419+
vm_unpack_shape_and_index(uintptr_t cache_value, shape_id_t *shape_id, attr_index_t *index)
420420
{
421-
uintptr_t cache_value = cc->aux_.attr.value; // Atomically read 64 bits
422421
*shape_id = (shape_id_t)(cache_value >> SHAPE_FLAG_SHIFT);
423422
*index = (attr_index_t)(cache_value & SHAPE_FLAG_MASK) - 1;
424-
return;
425423
}
426424

427425
static inline void
428-
vm_ic_atomic_shape_and_index(const struct iseq_inline_iv_cache_entry *ic, shape_id_t * shape_id, attr_index_t * index)
426+
vm_cc_atomic_shape_and_index(const struct rb_callcache *cc, shape_id_t *shape_id, attr_index_t *index)
429427
{
430-
uintptr_t cache_value = ic->value; // Atomically read 64 bits
431-
*shape_id = (shape_id_t)(cache_value >> SHAPE_FLAG_SHIFT);
432-
*index = (attr_index_t)(cache_value & SHAPE_FLAG_MASK) - 1;
433-
return;
428+
// Atomically read uintptr_t
429+
vm_unpack_shape_and_index(cc->aux_.attr.value, shape_id, index);
430+
}
431+
432+
static inline void
433+
vm_ic_atomic_shape_and_index(const struct iseq_inline_iv_cache_entry *ic, shape_id_t *shape_id, attr_index_t *index)
434+
{
435+
// Atomically read uintptr_t
436+
vm_unpack_shape_and_index(ic->value, shape_id, index);
434437
}
435438

436439
static inline unsigned int
@@ -467,17 +470,23 @@ set_vm_cc_ivar(const struct rb_callcache *cc)
467470
*(VALUE *)&cc->flags |= VM_CALLCACHE_IVAR;
468471
}
469472

473+
static inline uintptr_t
474+
vm_pack_shape_and_index(shape_id_t shape_id, attr_index_t index)
475+
{
476+
return (attr_index_t)(index + 1) | ((uintptr_t)(shape_id) << SHAPE_FLAG_SHIFT);
477+
}
478+
470479
static inline void
471480
vm_cc_attr_index_set(const struct rb_callcache *cc, attr_index_t index, shape_id_t dest_shape_id)
472481
{
473482
uintptr_t *attr_value = (uintptr_t *)&cc->aux_.attr.value;
474483
if (!vm_cc_markable(cc)) {
475-
*attr_value = (uintptr_t)INVALID_SHAPE_ID << SHAPE_FLAG_SHIFT;
484+
*attr_value = vm_pack_shape_and_index(INVALID_SHAPE_ID, ATTR_INDEX_NOT_SET);
476485
return;
477486
}
478487
VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
479488
VM_ASSERT(cc != vm_cc_empty());
480-
*attr_value = (attr_index_t)(index + 1) | ((uintptr_t)(dest_shape_id) << SHAPE_FLAG_SHIFT);
489+
*attr_value = vm_pack_shape_and_index(dest_shape_id, index);
481490
set_vm_cc_ivar(cc);
482491
}
483492

@@ -490,13 +499,13 @@ vm_cc_ivar_p(const struct rb_callcache *cc)
490499
static inline void
491500
vm_ic_attr_index_set(const rb_iseq_t *iseq, const struct iseq_inline_iv_cache_entry *ic, attr_index_t index, shape_id_t dest_shape_id)
492501
{
493-
*(uintptr_t *)&ic->value = ((uintptr_t)dest_shape_id << SHAPE_FLAG_SHIFT) | (attr_index_t)(index + 1);
502+
*(uintptr_t *)&ic->value = vm_pack_shape_and_index(dest_shape_id, index);
494503
}
495504

496505
static inline void
497506
vm_ic_attr_index_initialize(const struct iseq_inline_iv_cache_entry *ic, shape_id_t shape_id)
498507
{
499-
*(uintptr_t *)&ic->value = (uintptr_t)shape_id << SHAPE_FLAG_SHIFT;
508+
*(uintptr_t *)&ic->value = vm_pack_shape_and_index(shape_id, ATTR_INDEX_NOT_SET);
500509
}
501510

502511
static inline void

vm_insnhelper.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,8 +1209,6 @@ fill_ivar_cache(const rb_iseq_t *iseq, IVC ic, const struct rb_callcache *cc, in
12091209
#define ractor_object_incidental_shareable_p(obj, val) \
12101210
ractor_incidental_shareable_p(rb_ractor_shareable_p(obj), val)
12111211

1212-
#define ATTR_INDEX_NOT_SET (attr_index_t)-1
1213-
12141212
ALWAYS_INLINE(static VALUE vm_getivar(VALUE, ID, const rb_iseq_t *, IVC, const struct rb_callcache *, int, VALUE));
12151213
static inline VALUE
12161214
vm_getivar(VALUE obj, ID id, const rb_iseq_t *iseq, IVC ic, const struct rb_callcache *cc, int is_attr, VALUE default_value)

0 commit comments

Comments
 (0)