Skip to content

Commit a2e165e

Browse files
committed
Remove dsymbol_fstr_hash
We don't need to delay the freeing of the fstr for the symbol if we store the hash of the fstr in the dynamic symbol and we use compare-by-identity for removing the dynamic symbol from the sym_set.
1 parent 2bcb155 commit a2e165e

File tree

3 files changed

+15
-31
lines changed

3 files changed

+15
-31
lines changed

gc.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3168,11 +3168,6 @@ rb_gc_mark_children(void *objspace, VALUE obj)
31683168
switch (BUILTIN_TYPE(obj)) {
31693169
case T_FLOAT:
31703170
case T_BIGNUM:
3171-
case T_SYMBOL:
3172-
/* Not immediates, but does not have references and singleton class.
3173-
*
3174-
* RSYMBOL(obj)->fstr intentionally not marked. See log for 96815f1e
3175-
* ("symbol.c: remove rb_gc_mark_symbols()") */
31763171
return;
31773172

31783173
case T_NIL:
@@ -3230,6 +3225,10 @@ rb_gc_mark_children(void *objspace, VALUE obj)
32303225
mark_hash(obj);
32313226
break;
32323227

3228+
case T_SYMBOL:
3229+
gc_mark_internal(RSYMBOL(obj)->fstr);
3230+
break;
3231+
32333232
case T_STRING:
32343233
if (STR_SHARED_P(obj)) {
32353234
if (STR_EMBED_P(RSTRING(obj)->as.heap.aux.shared)) {

hash.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ any_hash(VALUE a, st_index_t (*other_func)(VALUE))
184184
hnum = rb_hash_start(hnum);
185185
}
186186
else {
187-
hnum = RSYMBOL(a)->hashval;
187+
hnum = RSHIFT(RSYMBOL(a)->hashval, 1);
188188
}
189189
break;
190190
case T_FIXNUM:

symbol.c

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ typedef struct {
9898
VALUE sym_set;
9999

100100
VALUE ids;
101-
VALUE dsymbol_fstr_hash;
102101
} rb_symbols_t;
103102

104103
rb_symbols_t ruby_global_symbols = {tNEXT_ID-1};
@@ -154,7 +153,12 @@ sym_set_sym_get_str(VALUE sym)
154153
static VALUE
155154
sym_set_hash(VALUE sym)
156155
{
157-
return (VALUE)rb_str_hash(sym_set_sym_get_str(sym));
156+
if (sym_set_sym_static_p(sym)) {
157+
return (VALUE)rb_str_hash(sym_set_static_sym_untag(sym)->str);
158+
}
159+
else {
160+
return (VALUE)RSYMBOL(sym)->hashval;
161+
}
158162
}
159163

160164
static bool
@@ -278,10 +282,7 @@ sym_set_create(VALUE sym, void *data)
278282
if (id < 0) id = ID_JUNK;
279283
obj->id = id;
280284

281-
/* we want hashval to be in Fixnum range [ruby-core:15713] r15672 */
282-
long hashval = (long)rb_str_hash(str);
283-
obj->hashval = RSHIFT((long)hashval, 1);
284-
rb_hash_aset(ruby_global_symbols.dsymbol_fstr_hash, str, Qtrue);
285+
obj->hashval = rb_str_hash(str);
285286
RUBY_DTRACE_CREATE_HOOK(SYMBOL, RSTRING_PTR(obj->fstr));
286287

287288
return (VALUE)obj;
@@ -377,10 +378,6 @@ Init_sym(void)
377378
{
378379
rb_symbols_t *symbols = &ruby_global_symbols;
379380

380-
VALUE dsym_fstrs = rb_ident_hash_new();
381-
symbols->dsymbol_fstr_hash = dsym_fstrs;
382-
rb_obj_hide(dsym_fstrs);
383-
384381
symbols->sym_set = rb_concurrent_set_new(&sym_set_funcs, 1024);
385382
symbols->ids = rb_ary_hidden_new(0);
386383

@@ -395,7 +392,6 @@ rb_sym_global_symbols_mark(void)
395392

396393
rb_gc_mark_movable(symbols->sym_set);
397394
rb_gc_mark_movable(symbols->ids);
398-
rb_gc_mark_movable(symbols->dsymbol_fstr_hash);
399395
}
400396

401397
void
@@ -405,7 +401,6 @@ rb_sym_global_symbols_update_references(void)
405401

406402
symbols->sym_set = rb_gc_location(symbols->sym_set);
407403
symbols->ids = rb_gc_location(symbols->ids);
408-
symbols->dsymbol_fstr_hash = rb_gc_location(symbols->dsymbol_fstr_hash);
409404
}
410405

411406
WARN_UNUSED_RESULT(static ID lookup_str_id(VALUE str));
@@ -955,7 +950,6 @@ rb_gc_free_dsymbol(VALUE sym)
955950
if (str) {
956951
GLOBAL_SYMBOLS_LOCKING(symbols) {
957952
rb_concurrent_set_delete_by_identity(symbols->sym_set, sym);
958-
rb_hash_delete_entry(symbols->dsymbol_fstr_hash, str);
959953
}
960954

961955
RSYMBOL(sym)->fstr = 0;
@@ -1013,7 +1007,6 @@ rb_sym2id(VALUE sym)
10131007
/* make it permanent object */
10141008

10151009
set_id_entry(symbols, rb_id_to_serial(num), fstr, sym);
1016-
rb_hash_delete_entry(symbols->dsymbol_fstr_hash, fstr);
10171010
}
10181011
}
10191012
}
@@ -1098,25 +1091,17 @@ symbols_i(VALUE *key, void *data)
10981091
VALUE ary = (VALUE)data;
10991092
VALUE sym = (VALUE)*key;
11001093

1101-
if (STATIC_SYM_P(sym)) {
1102-
rb_ary_push(ary, sym);
1103-
return ST_CONTINUE;
1104-
}
1105-
else if (!DYNAMIC_SYM_P(sym)) {
1106-
rb_bug("invalid symbol: %s", RSTRING_PTR((VALUE)key));
1107-
}
1108-
else if (!SYMBOL_PINNED_P(sym) && rb_objspace_garbage_object_p(sym)) {
1109-
RSYMBOL(sym)->fstr = 0;
1110-
return ST_DELETE;
1094+
if (sym_set_sym_static_p(sym)) {
1095+
rb_ary_push(ary, sym_set_static_sym_untag(sym)->sym);
11111096
}
11121097
else if (rb_objspace_garbage_object_p(sym)) {
11131098
return ST_DELETE;
11141099
}
11151100
else {
11161101
rb_ary_push(ary, sym);
1117-
return ST_CONTINUE;
11181102
}
11191103

1104+
return ST_CONTINUE;
11201105
}
11211106

11221107
VALUE

0 commit comments

Comments
 (0)