128 Commits

Author SHA1 Message Date
Koichi Sasada
3c3a805c35 mark internal cross-Ractor structures as shareable
The concurrent set, managed id-table dups and symbol id-entry buckets are
reachable from every Ractor via VM-global state (the frozen-string/symbol
tables, shape-tree edge tables, the symbol table), so flag them shareable --
as enc_list_update already does for the encoding list.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-07-03 23:50:37 +09:00
John Hawthorn
daffaaee73 Mark most TypedData types as THREAD_SAFE_FREE
Set RUBY_TYPED_THREAD_SAFE_FREE on TypedData types whose dfree function
is trivially safe, and only frees its own memory

Types not yet marked as THREAD_SAFE_FREE:
- id2ref_tbl_type: sets the process-global id2ref_tbl to NULL.
- mutex_data_type: unlinks itself from another thread's keeping_mutexes list.
- autoload_data_type: deletes nodes from a shared intrusive list.
- autoload_const_type: deletes itself from a shared list.
- rb_cont_data_type: mutates the shared fiber pool and the global first_jit_cont list.
- rb_fiber_data_type: delegates to cont_free.
- FiberPoolDataType: mutates shared fiber pool free-lists.
- ractor_data_type: reconfigures global VM event-hook flags and call caches.
- exported_object_registry: frees a global table under the VM lock.
- rb_box_data_type (Box::Entry): unlinks classext from other live class and module objects.
- box_ext_cleanup_type: dereferences another String object and calls unlink.
- monitor_data_type: uses the default free, so the flag has no effect.

Co-authored-by: Luke Gruber <luke.gruber@shopify.com>
2026-06-23 13:30:26 -07:00
Jean Boussier
e4667e5109 Refactor NEWOB_OF for the common protected use case
Almost every objects are now WB protected, with just a few
exceptions, hence we can provide a much simpler interface.

It's also much easier to find the remaining unprotected objects.
2026-04-28 11:11:29 +09:00
Jean Boussier
c202bc08e1 Simplify NEWOBJ_OF and introduce EC_NEWOBJ_OF
We only very rarely have the execution context available, so
it's preferable to provide a simpler default macro.
2026-04-28 11:11:29 +09:00
Earlopain
2d2351af56
Remove unused compile options/jobs (#16583)
SYMBOL_GC removed in 2bcb155b49
THREAD_CACHE removed in be1bbd5b7d
BIGNUM_DEBUG removed in e60cd14d85
2026-03-27 23:25:00 +00:00
Jean Boussier
cbf6e775ee darray.h: use ruby_sized_xfree 2026-02-08 12:47:40 +01:00
Peter Zhu
f7ae32ed3b Pin ID symbols
Symbols with a corresponding ID should be pinned because they can be used
by things that don't support compaction.
2026-01-15 17:57:27 -05:00
Koichi Sasada
bc00c4468e use SET_SHAREABLE
to adopt strict shareable rule.

* (basically) shareable objects only refer shareable objects
* (exception) shareable objects can refere unshareable objects
  but should not leak reference to unshareable objects to Ruby world
2025-10-23 13:08:26 +09:00
Nobuyoshi Nakada
2bb6fe3854
[Bug #21629] Initialize struct RString 2025-10-08 18:19:47 +09:00
Burdette Lamar
6a2964d28c
[DOC] Tweaks for String#intern (#14314) 2025-08-26 14:27:54 -04:00
Jean Boussier
1986d775cd symbol.c: use rb_gc_mark_and_move over rb_gc_location
The `p->field = rb_gc_location(p->field)` isn't ideal because it means all
references are rewritten on compaction, regardless of whether the referenced
object has moved. This isn't good for caches nor for Copy-on-Write.

`rb_gc_mark_and_move` avoid needless writes, and most of the time allow to
have a single function for both marking and updating references.
2025-08-07 21:00:00 +02:00
Peter Zhu
71b46938a7 Fix off-by-one in symbol next_id
Symbol last_id was changed to next_id, but it remained to be set to
tNEXT_ID - 1 initially, causing the initial static symbol to overlap with
the last built-in symbol in id.def.
2025-08-06 13:40:27 -04:00
Peter Zhu
95320f1ddf Fix RUBY_FREE_AT_EXIT for static symbols
Since static symbols allocate memory, we should deallocate them at shutdown
to prevent memory leaks from being reported with RUBY_FREE_AT_EXIT.
2025-08-05 12:04:27 -04:00
Peter Zhu
6c24904a69 Make static symbol ID atomic
We don't need the VM lock if we make static symbol IDs atomic.
2025-07-31 11:09:03 -04:00
Jean Boussier
d488935910 Get rid of ID_JUNK
It has been aliased as ID_INTERNAL for a long time and that alias
is much more descriptive.
2025-07-28 12:22:42 +02:00
Peter Zhu
bd2d6845f1 Remove VM lock in register_static_symid 2025-07-25 09:51:24 -04:00
Peter Zhu
42f95456cc Remove VM lock for sym_find 2025-07-25 09:51:24 -04:00
Peter Zhu
2235fdb6f1 Remove VM lock for rb_id_attrset 2025-07-25 09:51:24 -04:00
Peter Zhu
93be578691 Remove global symbol locks for rb_intern 2025-07-23 10:07:11 -04:00
Peter Zhu
33a849e385 Remove global symbol lock for rb_gc_free_dsymbol 2025-07-23 10:07:11 -04:00
Peter Zhu
66349692f0 Introduce free function to rb_concurrent_set_funcs
If we create a key but don't insert it (due to other Ractor winning the
race), then it would leak memory if we don't free it. This introduces a
new function to free that memory for this case.
2025-07-21 10:58:30 -04:00
Peter Zhu
061224f3cb 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      |
2025-07-21 10:58:30 -04:00
Peter Zhu
a2e165e8a0 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.
2025-07-21 10:58:30 -04:00
Peter Zhu
2bcb155b49 Convert global symbol table to concurrent set 2025-07-21 10:58:30 -04:00
Peter Zhu
116d11062f Assume that symbol in rb_check_symbol is not garbage
rb_check_symbol is a public API, so it is always a bug if the user holds
on to a dead object and passes it in.
2025-07-04 17:41:57 -04:00
Peter Zhu
8b2d76136b Assume that the symbol is not garbage in rb_sym2id
rb_sym2id is a public API, so it is always a bug if the user holds on to
a dead object and passes it in.
2025-07-03 09:05:23 -04:00
Jean Boussier
1f976509a5 symbol.c: enforce intern_str is always called with a lock
Add missing locks in `rb_intern_str`, `rb_id_attrset` and `rb_intern3`.
2025-07-03 12:19:04 +02:00
Nobuyoshi Nakada
edaa27ce45
Suppress warnings by gcc-13 with -Og 2025-06-05 22:33:02 +09:00
Nobuyoshi Nakada
fc518fe1ff
Delimit the scopes using encoding/symbol tables 2025-05-25 15:22:43 +09:00
Nobuyoshi Nakada
bbf1130f91 Add RBIMPL_ATTR_NONSTRING_ARRAY() macro for GCC 15 2025-05-05 18:25:04 +09:00
Takashi Kokubun
67b91e7807 Drop an ignored attribute
GCC 13.3.0 (Ubuntu 24.04) emits the following warning:

../symbol.c: In function ‘rb_id_attrset’:
../symbol.c:175:9: warning: ‘nonstring’ attribute ignored on objects of type ‘const char[][8]’ [-Wattributes]
  175 |         RBIMPL_ATTR_NONSTRING() static const char id_types[][8] = {
      |         ^~~~~~~~~~~~~~~~~~~~~
2025-05-01 10:26:20 -07:00
Nobuyoshi Nakada
b42afa1dbc
Suppress gcc 15 unterminated-string-initialization warnings 2025-04-30 20:04:10 +09:00
Peter Zhu
3fb455adab Move global symbol reference updating to rb_sym_global_symbols_update_references 2025-02-10 08:47:44 -05:00
Peter Zhu
8d0416ae0b Make ruby_global_symbols movable
The `ids` array and `dsymbol_fstr_hash` were pinned because they were
kept alive by rb_vm_register_global_object. This prevented the GC from
moving them even though there were reference updating code.

This commit changes it to be marked movable by marking it as a root object.
2025-02-10 08:47:44 -05:00
Nobuyoshi Nakada
4dd9e5cf74 Add builtin type assertion 2024-04-08 11:13:29 +09:00
Peter Zhu
43dcf4d1a6 Assert correct types in get_id_serial_entry 2024-04-05 16:15:48 -04:00
Peter Zhu
a80e8ba1c4 Assert correct types in set_id_entry 2024-04-05 16:15:40 -04:00
Peter Zhu
37490474c4 Assert that rb_sym2str returns 0 or a T_STRING 2024-04-05 16:15:33 -04:00
Jean Boussier
d4f3dcf4df Refactor VM root modules
This `st_table` is used to both mark and pin classes
defined from the C API. But `vm->mark_object_ary` already
does both much more efficiently.

Currently a Ruby process starts with 252 rooted classes,
which uses `7224B` in an `st_table` or `2016B` in an `RArray`.

So a baseline of 5kB saved, but since `mark_object_ary` is
preallocated with `1024` slots but only use `405` of them,
it's a net `7kB` save.

`vm->mark_object_ary` is also being refactored.

Prior to this changes, `mark_object_ary` was a regular `RArray`, but
since this allows for references to be moved, it was marked a second
time from `rb_vm_mark()` to pin these objects.

This has the detrimental effect of marking these references on every
minors even though it's a mostly append only list.

But using a custom TypedData we can save from having to mark
all the references on minor GC runs.

Addtionally, immediate values are now ignored and not appended
to `vm->mark_object_ary` as it's just wasted space.
2024-03-06 15:33:43 -05:00
Alan Wu
ee3b4bec0e
YJIT: Simplify Kernel#send guards and admit more cases (#9956)
Previously, our compile time check rejected dynamic symbols (e.g. what
String#to_sym could return) even though we could handle them just fine.
The runtime guards for the type of method name was also overly
restrictive and didn't accept dynamic symbols.

Fold the type check into the rb_get_symbol_id() and take advantage of
the guard already checking for 0. This also avoids generating the same
call twice in case the same method name is presented as different
types.
2024-02-14 11:19:04 -05:00
Burdette Lamar
65f5435540
[DOC] Doc compliance (#9955) 2024-02-14 10:47:42 -05:00
Peter Zhu
4d3fc96b8b Change dsymbol_alloc to use NEWOBJ_OF 2024-02-13 15:30:06 -05:00
Peter Zhu
01fd262e62 Fix crash when checking symbol encoding
[Bug #20245]

We sometimes pass in a fake string to sym_check_asciionly. This can crash
if sym_check_asciionly raises because it creates a CFP with the fake
string as the receiver which will crash if GC tries to mark the CFP.

For example, the following script crashes:

    GC.stress = true
    Object.const_defined?("\xC3")
2024-02-08 10:12:56 -05:00
Adam Hess
6816e8efcf Free everything at shutdown
when the RUBY_FREE_ON_SHUTDOWN environment variable is set, manually free memory at shutdown.

Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
Co-authored-by: Peter Zhu <peter@peterzhu.ca>
2023-12-07 15:52:35 -05:00
Nobuyoshi Nakada
79eb75a8dd
[Bug #20025] Check if upper/lower before fallback to case-folding 2023-11-29 14:40:21 +09:00
Nobuyoshi Nakada
e7dc8f0b27
Compile debugging code for symbol and ID always 2023-06-30 23:59:05 +09:00
Nobuyoshi Nakada
ac0163949a
Compile code without Symbol GC always 2023-06-30 23:59:05 +09:00
Matt Valentine-House
72aba64fff Merge gc.h and internal/gc.h
[Feature #19425]
2023-02-09 10:32:29 -05:00
Takashi Kokubun
e7443dbbca
Rewrite Symbol#to_sym and #intern in Ruby (#6683) 2022-11-15 21:34:30 -08:00
Jimmy Miller
467992ee35
Implement optimize send in yjit (#6488)
* Implement optimize send in yjit

This successfully makes all our benchmarks exit way less for optimize send reasons.
It makes some benchmarks faster, but not by as much as I'd like. I think this implementation
works, but there are definitely more optimial arrangements. For example, what if we compiled
send to a jump table? That seems like perhaps the most optimal we could do, but not obvious (to me)
how to implement give our current setup.

Co-authored-by: Alan Wu <XrXr@users.noreply.github.com>

* Attempt at fixing the issues raised by @XrXr

* fix allowlist

* returns 0 instead of nil when not found

* remove comment about encoding exception

* Fix up c changes

* Update assert

Co-authored-by: Alan Wu <XrXr@users.noreply.github.com>

* get rid of unneeded code and fix the flags

* Apply suggestions from code review

Co-authored-by: Alan Wu <XrXr@users.noreply.github.com>

* rename and fix typo

Co-authored-by: Alan Wu <XrXr@users.noreply.github.com>
2022-10-11 16:37:05 -04:00