summaryrefslogtreecommitdiff
path: root/vm.c
diff options
context:
space:
mode:
authorAaron Patterson <tenderlove@ruby-lang.org>2023-02-07 17:46:42 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2023-10-24 10:52:06 -0700
commit84e4453436c3549b4fda6014cdd5fcc9e0b80755 (patch)
treef0fdc61a51a8f1cfa5eb9b534103d1534a29b5ae /vm.c
parent5c4978c11c4ea9569d5d99a86936fbef0ab7fa52 (diff)
Use a functional red-black tree for indexing the shapes
This is an experimental commit that uses a functional red-black tree to create an index of the ancestor shapes. It uses an Okasaki style functional red black tree: https://www.cs.tufts.edu/comp/150FP/archive/chris-okasaki/redblack99.pdf This tree is advantageous because: * It offers O(n log n) insertions and O(n log n) lookups. * It shares memory with previous "versions" of the tree When we insert a node in the tree, only the parts of the tree that need to be rebalanced are newly allocated. Parts of the tree that don't need to be rebalanced are not reallocated, so "new trees" are able to share memory with old trees. This is in contrast to a sorted set where we would have to duplicate the set, and also resort the set on each insertion. I've added a new stat to RubyVM.stat so we can understand how the red black tree increases.
Diffstat (limited to 'vm.c')
-rw-r--r--vm.c8
1 files changed, 7 insertions, 1 deletions
diff --git a/vm.c b/vm.c
index ebaee54953..7f43484905 100644
--- a/vm.c
+++ b/vm.c
@@ -633,6 +633,8 @@ rb_dtrace_setup(rb_execution_context_t *ec, VALUE klass, ID id,
return FALSE;
}
+extern unsigned int redblack_buffer_size;
+
/*
* call-seq:
* RubyVM.stat -> Hash
@@ -660,6 +662,7 @@ static VALUE
vm_stat(int argc, VALUE *argv, VALUE self)
{
static VALUE sym_constant_cache_invalidations, sym_constant_cache_misses, sym_global_cvar_state, sym_next_shape_id;
+ static VALUE sym_shape_cache_size;
VALUE arg = Qnil;
VALUE hash = Qnil, key = Qnil;
@@ -681,6 +684,7 @@ vm_stat(int argc, VALUE *argv, VALUE self)
S(constant_cache_misses);
S(global_cvar_state);
S(next_shape_id);
+ S(shape_cache_size);
#undef S
#define SET(name, attr) \
@@ -693,6 +697,7 @@ vm_stat(int argc, VALUE *argv, VALUE self)
SET(constant_cache_misses, ruby_vm_constant_cache_misses);
SET(global_cvar_state, ruby_vm_global_cvar_state);
SET(next_shape_id, (rb_serial_t)GET_SHAPE_TREE()->next_shape_id);
+ SET(shape_cache_size, (rb_serial_t)GET_SHAPE_TREE()->cache_size);
#undef SET
#if USE_DEBUG_COUNTER
@@ -3069,7 +3074,8 @@ vm_memsize(const void *ptr)
vm_memsize_builtin_function_table(vm->builtin_function_table) +
rb_id_table_memsize(vm->negative_cme_table) +
rb_st_memsize(vm->overloaded_cme_table) +
- vm_memsize_constant_cache()
+ vm_memsize_constant_cache() +
+ GET_SHAPE_TREE()->cache_size * sizeof(redblack_node_t)
);
// TODO