summaryrefslogtreecommitdiff
path: root/vm.c
diff options
context:
space:
mode:
authorJemma Issroff <jemmaissroff@gmail.com>2022-09-23 13:54:42 -0400
committerAaron Patterson <tenderlove@ruby-lang.org>2022-09-28 08:26:21 -0700
commitd594a5a8bd0756f65c078fcf5ce0098250cba141 (patch)
tree3930e12366c80e7bcbc330fe880205a3d212b5aa /vm.c
parenta05b2614645594df896aaf44a2e5701ee7fb5fec (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 <tenderlove@ruby-lang.org> Co-Authored-By: Eileen M. Uchitelle <eileencodes@gmail.com> Co-Authored-By: John Hawthorn <john@hawthorn.email>
Diffstat (limited to 'vm.c')
-rw-r--r--vm.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/vm.c b/vm.c
index 0de461392f..a52d3efa7b 100644
--- a/vm.c
+++ b/vm.c
@@ -26,6 +26,7 @@
#include "internal/thread.h"
#include "internal/vm.h"
#include "internal/sanitizers.h"
+#include "internal/variable.h"
#include "iseq.h"
#include "mjit.h"
#include "yjit.h"
@@ -4021,6 +4022,11 @@ Init_BareVM(void)
rb_native_cond_initialize(&vm->ractor.sync.terminate_cond);
}
+#ifndef _WIN32
+#include <unistd.h>
+#include <sys/mman.h>
+#endif
+
void
Init_vm_objects(void)
{
@@ -4032,6 +4038,31 @@ Init_vm_objects(void)
vm->mark_object_ary = rb_ary_hidden_new(128);
vm->loading_table = st_init_strtable();
vm->frozen_strings = st_init_table_with_size(&rb_fstring_hash_type, 10000);
+
+#if HAVE_MMAP
+ vm->shape_list = (rb_shape_t *)mmap(NULL, rb_size_mul_or_raise(SHAPE_BITMAP_SIZE * 32, sizeof(rb_shape_t), rb_eRuntimeError),
+ PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
+ if (vm->shape_list == MAP_FAILED) {
+ vm->shape_list = 0;
+ }
+#else
+ vm->shape_list = xcalloc(SHAPE_BITMAP_SIZE * 32, sizeof(rb_shape_t));
+#endif
+
+ if (!vm->shape_list) {
+ rb_memerror();
+ }
+
+ // Root shape
+ vm->root_shape = rb_shape_alloc(0, 0);
+ RUBY_ASSERT(rb_shape_id(vm->root_shape) == ROOT_SHAPE_ID);
+
+ // Frozen root shape
+ vm->frozen_root_shape = rb_shape_alloc(rb_make_internal_id(), vm->root_shape);
+ vm->frozen_root_shape->type = (uint8_t)SHAPE_FROZEN;
+ RUBY_ASSERT(rb_shape_id(vm->frozen_root_shape) == FROZEN_ROOT_SHAPE_ID);
+
+ vm->next_shape_id = 2;
}
/* Stub for builtin function when not building YJIT units*/