Remove lock for dynamic symbol

Benchmark:

    ARGV[0].to_i.times.map do
      Ractor.new do
        1_000_000.times do |i|
          "hello#{i}".to_sym
        end
      end
    end.map(&:value)

Results:

| Ractor count | Branch (s) | Master (s) |
|--------------|------------|------------|
| 1            | 0.364      | 0.401      |
| 2            | 0.555      | 1.149      |
| 3            | 0.583      | 3.890      |
| 4            | 0.680      | 3.288      |
| 5            | 0.789      | 5.107      |
This commit is contained in:
Peter Zhu 2025-07-17 10:19:33 -04:00
parent efc232241e
commit 061224f3cb

View file

@ -262,8 +262,6 @@ set_id_entry(rb_symbols_t *symbols, rb_id_serial_t num, VALUE str, VALUE sym)
static VALUE static VALUE
sym_set_create(VALUE sym, void *data) sym_set_create(VALUE sym, void *data)
{ {
ASSERT_vm_locking();
bool create_dynamic_symbol = (bool)data; bool create_dynamic_symbol = (bool)data;
struct sym_set_static_sym_entry *static_sym_entry = sym_set_static_sym_untag(sym); struct sym_set_static_sym_entry *static_sym_entry = sym_set_static_sym_untag(sym);
@ -309,7 +307,9 @@ sym_set_create(VALUE sym, void *data)
} }
new_static_sym_entry->sym = static_sym; new_static_sym_entry->sym = static_sym;
set_id_entry(&ruby_global_symbols, rb_id_to_serial(STATIC_SYM2ID(static_sym)), str, static_sym); RB_VM_LOCKING() {
set_id_entry(&ruby_global_symbols, rb_id_to_serial(STATIC_SYM2ID(static_sym)), str, static_sym);
}
return sym_set_static_sym_tag(new_static_sym_entry); return sym_set_static_sym_tag(new_static_sym_entry);
} }
@ -979,12 +979,7 @@ rb_gc_free_dsymbol(VALUE sym)
VALUE VALUE
rb_str_intern(VALUE str) rb_str_intern(VALUE str)
{ {
VALUE sym = 0; return sym_find_or_insert_dynamic_symbol(&ruby_global_symbols, str);
GLOBAL_SYMBOLS_LOCKING(symbols) {
sym = sym_find_or_insert_dynamic_symbol(symbols, str);
}
return sym;
} }
ID ID