Skip to content

Commit c0417bd

Browse files
committed
Use set_table to track const caches
Now that we have a `set_table` implementation, we can use it to track const caches and save some memory. We could even save some more memory if `numtable` didn't store a copy of the `hash` and instead recomputed it every time, but this is a quick win.
1 parent e4f85bf commit c0417bd

6 files changed

Lines changed: 22 additions & 10 deletions

File tree

common.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8903,6 +8903,7 @@ iseq.$(OBJEXT): $(top_srcdir)/internal/rational.h
89038903
iseq.$(OBJEXT): $(top_srcdir)/internal/ruby_parser.h
89048904
iseq.$(OBJEXT): $(top_srcdir)/internal/sanitizers.h
89058905
iseq.$(OBJEXT): $(top_srcdir)/internal/serial.h
8906+
iseq.$(OBJEXT): $(top_srcdir)/internal/set_table.h
89068907
iseq.$(OBJEXT): $(top_srcdir)/internal/static_assert.h
89078908
iseq.$(OBJEXT): $(top_srcdir)/internal/string.h
89088909
iseq.$(OBJEXT): $(top_srcdir)/internal/symbol.h
@@ -19549,6 +19550,7 @@ vm.$(OBJEXT): $(top_srcdir)/internal/re.h
1954919550
vm.$(OBJEXT): $(top_srcdir)/internal/ruby_parser.h
1955019551
vm.$(OBJEXT): $(top_srcdir)/internal/sanitizers.h
1955119552
vm.$(OBJEXT): $(top_srcdir)/internal/serial.h
19553+
vm.$(OBJEXT): $(top_srcdir)/internal/set_table.h
1955219554
vm.$(OBJEXT): $(top_srcdir)/internal/static_assert.h
1955319555
vm.$(OBJEXT): $(top_srcdir)/internal/string.h
1955419556
vm.$(OBJEXT): $(top_srcdir)/internal/struct.h

internal/set_table.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ typedef int set_update_callback_func(st_data_t *key, st_data_t arg, int existing
3535
size_t rb_set_table_size(const struct set_table *tbl);
3636
#define set_init_table_with_size rb_set_init_table_with_size
3737
set_table *rb_set_init_table_with_size(set_table *tab, const struct st_hash_type *, st_index_t);
38+
#define set_init_numtable rb_set_init_numtable
39+
set_table *rb_set_init_numtable(void);
3840
#define set_delete rb_set_delete
3941
int rb_set_delete(set_table *, st_data_t *); /* returns 0:notfound 1:deleted */
4042
#define set_insert rb_set_insert

iseq.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "internal/io.h"
3232
#include "internal/ruby_parser.h"
3333
#include "internal/sanitizers.h"
34+
#include "internal/set_table.h"
3435
#include "internal/symbol.h"
3536
#include "internal/thread.h"
3637
#include "internal/variable.h"
@@ -110,14 +111,14 @@ remove_from_constant_cache(ID id, IC ic)
110111
st_data_t ic_data = (st_data_t)ic;
111112

112113
if (rb_id_table_lookup(vm->constant_cache, id, &lookup_result)) {
113-
st_table *ics = (st_table *)lookup_result;
114-
st_delete(ics, &ic_data, NULL);
114+
set_table *ics = (set_table *)lookup_result;
115+
set_delete(ics, &ic_data);
115116

116117
if (ics->num_entries == 0 &&
117118
// See comment in vm_track_constant_cache on why we need this check
118119
id != vm->inserting_constant_cache_id) {
119120
rb_id_table_delete(vm->constant_cache, id);
120-
st_free_table(ics);
121+
set_free_table(ics);
121122
}
122123
}
123124
}

st.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2459,6 +2459,12 @@ set_init_table_with_size(set_table *tab, const struct st_hash_type *type, st_ind
24592459
return tab;
24602460
}
24612461

2462+
set_table *
2463+
set_init_numtable(void)
2464+
{
2465+
return set_init_table_with_size(NULL, &type_numhash, 0);
2466+
}
2467+
24622468
size_t
24632469
set_table_size(const struct set_table *tbl)
24642470
{

vm_insnhelper.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "internal/proc.h"
2727
#include "internal/random.h"
2828
#include "internal/variable.h"
29+
#include "internal/set_table.h"
2930
#include "internal/struct.h"
3031
#include "variable.h"
3132

@@ -6332,13 +6333,13 @@ vm_track_constant_cache(ID id, void *ic)
63326333
rb_vm_t *vm = GET_VM();
63336334
struct rb_id_table *const_cache = vm->constant_cache;
63346335
VALUE lookup_result;
6335-
st_table *ics;
6336+
set_table *ics;
63366337

63376338
if (rb_id_table_lookup(const_cache, id, &lookup_result)) {
6338-
ics = (st_table *)lookup_result;
6339+
ics = (set_table *)lookup_result;
63396340
}
63406341
else {
6341-
ics = st_init_numtable();
6342+
ics = set_init_numtable();
63426343
rb_id_table_insert(const_cache, id, (VALUE)ics);
63436344
}
63446345

@@ -6356,7 +6357,7 @@ vm_track_constant_cache(ID id, void *ic)
63566357
*/
63576358
vm->inserting_constant_cache_id = id;
63586359

6359-
st_insert(ics, (st_data_t) ic, (st_data_t) Qtrue);
6360+
set_insert(ics, (st_data_t)ic);
63606361

63616362
vm->inserting_constant_cache_id = (ID)0;
63626363
}

vm_method.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ vm_cme_invalidate(rb_callable_method_entry_t *cme)
125125
}
126126

127127
static int
128-
rb_clear_constant_cache_for_id_i(st_data_t ic, st_data_t idx, st_data_t arg)
128+
rb_clear_constant_cache_for_id_i(st_data_t ic, st_data_t arg)
129129
{
130130
((IC) ic)->entry = NULL;
131131
return ST_CONTINUE;
@@ -141,8 +141,8 @@ rb_clear_constant_cache_for_id(ID id)
141141
rb_vm_t *vm = GET_VM();
142142

143143
if (rb_id_table_lookup(vm->constant_cache, id, &lookup_result)) {
144-
st_table *ics = (st_table *)lookup_result;
145-
st_foreach(ics, rb_clear_constant_cache_for_id_i, (st_data_t) NULL);
144+
set_table *ics = (set_table *)lookup_result;
145+
set_foreach(ics, rb_clear_constant_cache_for_id_i, (st_data_t) NULL);
146146
ruby_vm_constant_cache_invalidations += ics->num_entries;
147147
}
148148

0 commit comments

Comments
 (0)