summaryrefslogtreecommitdiff
path: root/gc.c
diff options
context:
space:
mode:
authorAaron Patterson <tenderlove@ruby-lang.org>2021-02-22 16:18:10 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2021-05-03 14:11:48 -0700
commit9a6226c61ea8a8ae7b3516b693a0d6e73526a99f (patch)
tree7cb87204a9fd0acc988a0eb84f05eb0a526aad2b /gc.c
parenta6ff1dc6f98b29661fd1147d84bc3b928bed618f (diff)
Eagerly allocate instance variable tables along with object
This allows us to allocate the right size for the object in advance, meaning that we don't have to pay the cost of ivar table extension later. The idea is that if an object type ever became "extended" at some point, then it is very likely it will become extended again. So we may as well allocate the ivar table up front.
Notes
Notes: Merged: https://github.com/ruby/ruby/pull/4216
Diffstat (limited to 'gc.c')
-rw-r--r--gc.c20
1 files changed, 18 insertions, 2 deletions
diff --git a/gc.c b/gc.c
index 4d78b85543..1507be6642 100644
--- a/gc.c
+++ b/gc.c
@@ -2390,7 +2390,14 @@ VALUE
rb_newobj_of(VALUE klass, VALUE flags)
{
if ((flags & RUBY_T_MASK) == T_OBJECT) {
- return newobj_of(klass, (flags | ROBJECT_EMBED) & ~FL_WB_PROTECTED , Qundef, Qundef, Qundef, flags & FL_WB_PROTECTED);
+ st_table *index_tbl = RCLASS_IV_INDEX_TBL(klass);
+
+ VALUE obj = newobj_of(klass, (flags | ROBJECT_EMBED) & ~FL_WB_PROTECTED , Qundef, Qundef, Qundef, flags & FL_WB_PROTECTED);
+
+ if (index_tbl && index_tbl->num_entries > ROBJECT_EMBED_LEN_MAX) {
+ rb_init_iv_list(obj);
+ }
+ return obj;
}
else {
return newobj_of(klass, flags & ~FL_WB_PROTECTED, 0, 0, 0, flags & FL_WB_PROTECTED);
@@ -2501,8 +2508,17 @@ rb_imemo_new_debug(enum imemo_type type, VALUE v1, VALUE v2, VALUE v3, VALUE v0,
VALUE
rb_class_allocate_instance(VALUE klass)
{
+ st_table *index_tbl = RCLASS_IV_INDEX_TBL(klass);
+
VALUE flags = T_OBJECT | ROBJECT_EMBED;
- return newobj_of(klass, flags, Qundef, Qundef, Qundef, RGENGC_WB_PROTECTED_OBJECT);
+
+ VALUE obj = newobj_of(klass, flags, Qundef, Qundef, Qundef, RGENGC_WB_PROTECTED_OBJECT);
+
+ if (index_tbl && index_tbl->num_entries > ROBJECT_EMBED_LEN_MAX) {
+ rb_init_iv_list(obj);
+ }
+
+ return obj;
}
VALUE