summaryrefslogtreecommitdiff
path: root/vm_callinfo.h
diff options
context:
space:
mode:
authorJemma Issroff <[email protected]>2022-09-23 13:54:42 -0400
committerAaron Patterson <[email protected]>2022-09-26 09:21:30 -0700
commit9ddfd2ca004d1952be79cf1b84c52c79a55978f4 ()
treefe5fa943d9a2dc7438db920a09173ab06f869993 /vm_callinfo.h
parent2e88bca24ff4cafeb6afe5b062ff7181bc4b3a9b (diff)
This commit implements the Object Shapes technique in CRuby.
Object Shapes is used for accessing instance variables and representing the "frozenness" of objects. Object instances have a "shape" and the shape represents some attributes of the object (currently which instance variables are set and the "frozenness"). Shapes form a tree data structure, and when a new instance variable is set on an object, that object "transitions" to a new shape in the shape tree. Each shape has an ID that is used for caching. The shape structure is independent of class, so objects of different types can have the same shape. For example: ```ruby class Foo def initialize # Starts with shape id 0 @a = 1 # transitions to shape id 1 @b = 1 # transitions to shape id 2 end end class Bar def initialize # Starts with shape id 0 @a = 1 # transitions to shape id 1 @b = 1 # transitions to shape id 2 end end foo = Foo.new # `foo` has shape id 2 bar = Bar.new # `bar` has shape id 2 ``` Both `foo` and `bar` instances have the same shape because they both set instance variables of the same name in the same order. This technique can help to improve inline cache hits as well as generate more efficient machine code in JIT compilers. This commit also adds some methods for debugging shapes on objects. See `RubyVM::Shape` for more details. For more context on Object Shapes, see [Feature: #18776] Co-Authored-By: Aaron Patterson <[email protected]> Co-Authored-By: Eileen M. Uchitelle <[email protected]> Co-Authored-By: John Hawthorn <[email protected]>
Notes: Merged: https://.com/ruby/ruby/pull/6386
-rw-r--r--vm_callinfo.h108
1 files changed, 84 insertions, 24 deletions
@@ -10,6 +10,7 @@
#include "debug_counter.h"
#include "internal/class.h"
enum vm_call_flag_bits {
VM_CALL_ARGS_SPLAT_bit, /* m(*args) */
@@ -284,14 +285,32 @@ struct rb_callcache {
const vm_call_handler call_;
union {
- const unsigned int attr_index;
const enum method_missing_reason method_missing_reason; /* used by method_missing */
VALUE v;
} aux_;
};
-#define VM_CALLCACHE_UNMARKABLE IMEMO_FL_USER0
-#define VM_CALLCACHE_ON_STACK IMEMO_FL_USER1
static inline const struct rb_callcache *
vm_cc_new(VALUE klass,
@@ -299,6 +318,7 @@ vm_cc_new(VALUE klass,
vm_call_handler call)
{
const struct rb_callcache *cc = (const struct rb_callcache *)rb_imemo_new(imemo_callcache, (VALUE)cme, (VALUE)call, 0, klass);
RB_DEBUG_COUNTER_INC(cc_new);
return cc;
}
@@ -350,30 +370,71 @@ vm_cc_call(const struct rb_callcache *cc)
return cc->call_;
}
-static inline unsigned int
vm_cc_attr_index(const struct rb_callcache *cc)
{
VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
- return cc->aux_.attr_index - 1;
}
static inline bool
vm_cc_attr_index_p(const struct rb_callcache *cc)
{
VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
- return cc->aux_.attr_index > 0;
}
-static inline uint32_t
-vm_ic_entry_index(const struct iseq_inline_iv_cache_entry *ic)
{
- return ic->entry->index;
}
static inline bool
-vm_ic_entry_p(const struct iseq_inline_iv_cache_entry *ic)
{
- return ic->entry;
}
static inline unsigned int
@@ -407,10 +468,6 @@ vm_cc_valid_p(const struct rb_callcache *cc, const rb_callable_method_entry_t *c
}
}
-extern const struct rb_callcache *rb_vm_empty_cc(void);
-extern const struct rb_callcache *rb_vm_empty_cc_for_super(void);
-#define vm_cc_empty() rb_vm_empty_cc()
-
/* callcache: mutate */
static inline void
@@ -422,26 +479,29 @@ vm_cc_call_set(const struct rb_callcache *cc, vm_call_handler call)
}
static inline void
-vm_cc_attr_index_set(const struct rb_callcache *cc, int index)
{
VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
VM_ASSERT(cc != vm_cc_empty());
- *(int *)&cc->aux_.attr_index = index + 1;
}
static inline void
-vm_ic_entry_set(struct iseq_inline_iv_cache_entry *ic, struct rb_iv_index_tbl_entry *entry, const rb_iseq_t *iseq)
{
- ic->entry = entry;
- RB_OBJ_WRITTEN(iseq, Qundef, entry->class_value);
}
static inline void
-vm_cc_attr_index_initialize(const struct rb_callcache *cc)
{
- VM_ASSERT(IMEMO_TYPE_P(cc, imemo_callcache));
- VM_ASSERT(cc != vm_cc_empty());
- *(int *)&cc->aux_.attr_index = 0;
}
static inline void