Skip to content

Commit e3be189

Browse files
committed
Fix global symbols table handling.
The global symbols table is now implemented as a concurrent_set. We handle it in a way similar to the fstring table.
1 parent 1e68838 commit e3be189

File tree

5 files changed

+79
-24
lines changed

5 files changed

+79
-24
lines changed

concurrent_set.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,17 +444,18 @@ rb_mmtk_concurrent_set_foreach_with_replace_range(VALUE set_obj, size_t begin, s
444444
int deleted = 0;
445445

446446
for (size_t i = begin; i < end; i++) {
447+
struct concurrent_set_entry *entry = &set->entries[i];
447448
VALUE key = set->entries[i].key;
448449

449450
switch (key) {
450451
case CONCURRENT_SET_EMPTY:
451452
case CONCURRENT_SET_DELETED:
452453
continue;
453454
case CONCURRENT_SET_MOVED:
454-
rb_bug("rb_concurrent_set_foreach_with_replace_range: moved entry");
455+
rb_bug("rb_mmtk_concurrent_set_foreach_with_replace_range: moved entry");
455456
break;
456457
default: {
457-
int ret = callback(&set->entries[i].key, data);
458+
int ret = callback(&entry->key, data);
458459
switch (ret) {
459460
case ST_STOP:
460461
return;

internal/mmtk.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ typedef uint32_t MMTk_AllocationSemantics;
3131

3232
#define MMTK_HIDDEN_SIZE_MASK 281474976710655
3333

34+
#define MMTK_WEAK_CONCURRENT_SET_KIND_FSTRING 0
35+
36+
#define MMTK_WEAK_CONCURRENT_SET_KIND_GLOBAL_SYMBOLS 1
37+
3438
typedef struct st_table st_table;
3539

3640
typedef struct RubyBindingOptions {
@@ -103,8 +107,8 @@ typedef struct MMTk_RubyUpcalls {
103107
void (*update_frozen_strings_table)(void);
104108
size_t (*get_cc_refinement_table_size)(void);
105109
void (*update_cc_refinement_table)(void);
106-
MMTk_ObjectReference (*get_fstring_table_obj)(void);
107-
struct st_table *(*get_global_symbols_table)(void);
110+
MMTk_NullableObjectReference (*get_fstring_table_obj)(void);
111+
MMTk_NullableObjectReference (*get_global_symbols_table_obj)(void);
108112
size_t (*st_get_num_entries)(const struct st_table *table);
109113
void (*st_get_size_info)(const struct st_table *table,
110114
size_t *entries_start,
@@ -122,6 +126,7 @@ typedef struct MMTk_RubyUpcalls {
122126
void (*concurrent_set_update_entries_range)(MMTk_ObjectReference set,
123127
size_t begin,
124128
size_t end,
129+
uint8_t kind,
125130
struct MMTk_ConcurrentSetStats *stats);
126131
void (*before_updating_jit_code)(void);
127132
void (*after_updating_jit_code)(void);

internal/symbol.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
/* symbol.c */
2020
void rb_sym_global_symbols_mark(void);
2121
void rb_sym_global_symbols_update_references(void);
22+
#if USE_MMTK
23+
size_t rb_mmtk_get_sym_set_num_entries(void);
24+
VALUE rb_mmtk_get_sym_set(void);
25+
#endif
2226
VALUE rb_to_symbol_type(VALUE obj);
2327
VALUE rb_sym_intern(const char *ptr, long len, rb_encoding *enc);
2428
VALUE rb_sym_intern_ascii(const char *ptr, long len);
@@ -33,6 +37,9 @@ ID rb_make_internal_id(void);
3337
ID rb_make_temporary_id(size_t n);
3438
bool rb_obj_is_symbol_table(VALUE obj);
3539
void rb_sym_global_symbol_table_foreach_weak_reference(int (*callback)(VALUE *key, void *data), void *data);
40+
#if USE_MMTK
41+
void rb_mmtk_sym_global_symbol_table_foreach_weak_reference_range(size_t begin, size_t end, int (*callback)(VALUE *key, void *data), void *data);
42+
#endif
3643
void rb_gc_free_dsymbol(VALUE);
3744
int rb_static_id_valid_p(ID id);
3845

mmtk_support.c

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "internal/gc.h"
88
#include "internal/imemo.h"
99
#include "internal/string.h"
10+
#include "internal/symbol.h"
1011
#include "internal/thread.h"
1112
#include "internal/variable.h"
1213
#include "ruby/ruby.h"
@@ -1272,27 +1273,18 @@ rb_mmtk_update_overloaded_cme_table(void)
12721273

12731274
//////// Global symbols table
12741275

1275-
extern rb_symbols_t ruby_global_symbols;
1276-
12771276
size_t
12781277
rb_mmtk_get_global_symbols_table_size(void)
12791278
{
1280-
return ruby_global_symbols.str_sym->num_entries;
1279+
return rb_mmtk_get_sym_set_num_entries();
12811280
}
12821281

12831282
void
12841283
rb_mmtk_update_global_symbols_table(void)
12851284
{
1286-
// String-to-symbol table.
1287-
// Keys are the strings, hasshed by content (rb_str_hash).
1288-
// Values are symbol objects. A symbol holds a reference to its
1289-
// corresponding string, so if the value is live, the key must be live.
1290-
// We need to remove entries for dead symbols.
1291-
rb_mmtk_update_weak_table(ruby_global_symbols.str_sym,
1292-
false,
1293-
RB_MMTK_VALUES_WEAK_REF,
1294-
NULL,
1295-
NULL);
1285+
// The global symbols table is a weak set of symbols.
1286+
// It is backed by a concurrent_set.
1287+
rb_gc_vm_weak_table_foreach(rb_mmtk_update_table_i, rb_mmtk_update_table_replace_i, NULL, false, RB_GC_VM_GLOBAL_SYMBOLS_TABLE);
12961288
}
12971289

12981290
//////// Finalizer and id2ref tables
@@ -1920,10 +1912,10 @@ rb_mmtk_get_fstring_table_obj_wrapper(void)
19201912
return (MMTk_ObjectReference)rb_mmtk_get_fstring_table_obj();
19211913
}
19221914

1923-
static st_table*
1924-
rb_mmtk_get_global_symbols_table(void)
1915+
static MMTk_ObjectReference
1916+
rb_mmtk_get_global_symbols_table_obj(void)
19251917
{
1926-
return ruby_global_symbols.str_sym;
1918+
return (MMTk_ObjectReference)rb_mmtk_get_sym_set();
19271919
}
19281920

19291921
static size_t
@@ -1944,7 +1936,11 @@ rb_mmtk_concurrent_set_update_entries_range_i(VALUE *key, void *data)
19441936
MMTk_ConcurrentSetStats *stats = (MMTk_ConcurrentSetStats*)data;
19451937
VALUE old_key = *key;
19461938

1947-
assert(!rb_special_const_p(old_key));
1939+
if (rb_special_const_p(old_key)) {
1940+
// The global symbols table may contain integers which encode static symbols.
1941+
// We skip them.
1942+
return ST_CONTINUE;
1943+
}
19481944

19491945
if (mmtk_is_reachable((MMTk_ObjectReference)old_key)) {
19501946
stats->live++;
@@ -1961,9 +1957,24 @@ rb_mmtk_concurrent_set_update_entries_range_i(VALUE *key, void *data)
19611957
}
19621958

19631959
static void
1964-
rb_mmtk_concurrent_set_update_entries_range(MMTk_ObjectReference set_obj, size_t begin, size_t end, MMTk_ConcurrentSetStats *stats)
1960+
rb_mmtk_concurrent_set_update_entries_range(MMTk_ObjectReference set_obj, size_t begin, size_t end, uint8_t kind, MMTk_ConcurrentSetStats *stats)
19651961
{
1966-
rb_mmtk_concurrent_set_foreach_with_replace_range((VALUE)set_obj, begin, end, rb_mmtk_concurrent_set_update_entries_range_i, stats);
1962+
int (*callback)(VALUE*, void*) = rb_mmtk_concurrent_set_update_entries_range_i;
1963+
void *data = stats;
1964+
1965+
switch (kind) {
1966+
case MMTK_WEAK_CONCURRENT_SET_KIND_GLOBAL_SYMBOLS: {
1967+
// Global symbols table need special handling.
1968+
// rb_mmtk_sym_global_symbol_table_foreach_weak_reference_range will handle static symbols.
1969+
assert((VALUE)set_obj == rb_mmtk_get_sym_set());
1970+
rb_mmtk_sym_global_symbol_table_foreach_weak_reference_range(begin, end, callback, data);
1971+
break;
1972+
}
1973+
default: {
1974+
rb_mmtk_concurrent_set_foreach_with_replace_range((VALUE)set_obj, begin, end, callback, data);
1975+
break;
1976+
}
1977+
}
19671978
}
19681979

19691980
MMTk_RubyUpcalls ruby_upcalls = {
@@ -2008,7 +2019,7 @@ MMTk_RubyUpcalls ruby_upcalls = {
20082019
rb_mmtk_update_cc_refinement_table,
20092020
// Get tables for specialized processing
20102021
rb_mmtk_get_fstring_table_obj_wrapper,
2011-
rb_mmtk_get_global_symbols_table,
2022+
rb_mmtk_get_global_symbols_table_obj,
20122023
// Detailed st_table info queries and operations
20132024
rb_mmtk_st_get_num_entries,
20142025
rb_mmtk_st_get_size_info,

symbol.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,22 @@ rb_sym_global_symbols_update_references(void)
412412
symbols->ids = rb_gc_location(symbols->ids);
413413
}
414414

415+
#if USE_MMTK
416+
size_t
417+
rb_mmtk_get_sym_set_num_entries(void)
418+
{
419+
rb_symbols_t *symbols = &ruby_global_symbols;
420+
return rb_mmtk_concurrent_set_get_num_entries(symbols->sym_set);
421+
}
422+
423+
VALUE
424+
rb_mmtk_get_sym_set(void)
425+
{
426+
rb_symbols_t *symbols = &ruby_global_symbols;
427+
return symbols->sym_set;
428+
}
429+
#endif
430+
415431
WARN_UNUSED_RESULT(static ID lookup_str_id(VALUE str));
416432
WARN_UNUSED_RESULT(static VALUE lookup_id_str(ID id));
417433

@@ -951,6 +967,21 @@ rb_sym_global_symbol_table_foreach_weak_reference(int (*callback)(VALUE *key, vo
951967
rb_concurrent_set_foreach_with_replace(ruby_global_symbols.sym_set, rb_sym_global_symbol_table_foreach_weak_reference_i, &foreach_data);
952968
}
953969

970+
#if USE_MMTK
971+
void
972+
rb_mmtk_sym_global_symbol_table_foreach_weak_reference_range(size_t begin, size_t end, int (*callback)(VALUE *key, void *data), void *data)
973+
{
974+
if (!ruby_global_symbols.sym_set) return;
975+
976+
struct global_symbol_table_foreach_weak_reference_data foreach_data = {
977+
.callback = callback,
978+
.data = data,
979+
};
980+
981+
rb_mmtk_concurrent_set_foreach_with_replace_range(ruby_global_symbols.sym_set, begin, end, rb_sym_global_symbol_table_foreach_weak_reference_i, &foreach_data);
982+
}
983+
#endif
984+
954985
void
955986
rb_gc_free_dsymbol(VALUE sym)
956987
{

0 commit comments

Comments
 (0)