Commit graph

234 commits

Author SHA1 Message Date
Peter Zhu
e639e5fd1a Make rb_gc_impl_writebarrier_remember Ractor-safe
rb_gc_impl_writebarrier_remember is not Ractor safe because it writes to
bitmaps and also pushes onto the mark stack during incremental marking.
We should acquire the VM lock to prevent race conditions.

In the case that the object is not old, there is no performance impact.
However, we can see a performance impact in this microbenchmark where the
object is old:

    4.times.map do
      Ractor.new do
        ary = []

        3.times { GC.start }

        10_000_000.times do |i|
          ary.push(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17)
          ary.clear
        end
      end
    end.map(&:value)

Before:

    Time (mean ± σ):     682.4 ms ±   5.1 ms    [User: 2564.8 ms, System: 16.0 ms]

After:

    Time (mean ± σ):      5.522 s ±  0.096 s    [User: 8.237 s, System: 7.931 s]

Co-Authored-By: Luke Gruber <luke.gruber@shopify.com>
Co-Authored-By: John Hawthorn <john@hawthorn.email>
2025-08-08 16:04:11 -04:00
Jean Boussier
3ef8d833ab rb_gc_impl_mark_and_move: avoid needless writes
Assuming not all objects are moved during compaction, it
is preferable to avoid rewriting references that haven't moved
as to avoid invalidating potentially shared memory pages.
2025-08-05 18:41:40 +02:00
Peter Zhu
74887a2c12 [ruby/mmtk] Skip weak references that are special consts
If a reference marked weak becomes a special const, it will crash because
it is not a GC handled object. We should skip special consts here.

870a79426b
2025-07-30 13:26:19 +00:00
Peter Zhu
1a4c0a9913 [ruby/mmtk] Fix warnings from cargo fmt
84975a8840
2025-07-29 21:45:11 +00:00
Peter Zhu
f608095b84 [ruby/mmtk] Fix clippy warnings
45f991578e
2025-07-29 21:45:10 +00:00
Peter Zhu
46d106f7ab Fix indentation in switch in rb_gc_impl_mark_maybe [ci skip] 2025-07-29 10:53:52 -04:00
John Hawthorn
973e6770d5 Fix TSAN data race in gc_start
objspace->flags.immediate_sweep shares the same word as
objspace->flags.during_incremental_marking. So in gc_start we need to
assign it after gc_enter() so that we hold the VM lock and have issued a
barrier, as rb_gc_impl_writebarrier is reading
objspace->flags.during_incremental_marking.
2025-07-22 09:37:13 -07:00
Kunshan Wang
51a3ea5ade YJIT: Set code mem permissions in bulk
Some GC modules, notably MMTk, support parallel GC, i.e. multiple GC
threads work in parallel during a GC.  Currently, when two GC threads
scan two iseq objects simultaneously when YJIT is enabled, both threads
will attempt to borrow `CodeBlock::mem_block`, which will result in
panic.

This commit makes one part of the change.

We now set the YJIT code memory to writable in bulk before the
reference-updating phase, and reset it to executable in bulk after the
reference-updating phase.  Previously, YJIT lazily sets memory pages
writable while updating object references embedded in JIT-compiled
machine code, and sets the memory back to executable by calling
`mark_all_executable`.  This approach is inherently unfriendly to
parallel GC because (1) it borrows `CodeBlock::mem_block`, and (2) it
sets the whole `CodeBlock` as executable which races with other GC
threads that are updating other iseq objects.  It also has performance
overhead due to the frequent invocation of system calls.  We now set the
permission of all the code memory in bulk before and after the reference
updating phase.  Multiple GC threads can now perform raw memory writes
in parallel.  We should also see performance improvement during moving
GC because of the reduced number of `mprotect` system calls.
2025-07-14 16:21:55 -04:00
Peter Zhu
9d41541b0c Fix unused variable warnings in default.c in modular GC
The asan and valgrind macros when BUILDING_MODULAR_GC don't use the variables
which could the compiler to emit unused variable warnings.
2025-07-10 11:56:22 -04:00
Peter Zhu
a4948c30fd Add debug message to assertion for checking GC mode
We assert that the GC is not in a cycle in gc_start, but it does not show
what phase we're in if the assertion fails. This commit adds a debug
message for when the assertion fails.
2025-06-26 09:37:55 -04:00
Peter Zhu
80f53eba40 Support message in GC_ASSERT
RUBY_ASSERT_MESG_WHEN supports a format string at the end for additional
information. This commit adds support for that in GC_ASSERT.
2025-06-26 09:37:55 -04:00
Nobuyoshi Nakada
dbc596938a
Move a comment to the corresponding conditional block [ci skip] 2025-06-21 13:14:42 +09:00
Jean Boussier
545e99da66 mmtk: Get rid of unused reference to FL_EXIVAR 2025-06-13 17:13:08 +02:00
Peter Zhu
6184793ea9 [DOC] Split building docs for modular GC 2025-06-09 22:30:43 -04:00
Peter Zhu
837699e160 Take file and line in GC VM locks
This commit adds file and line to GC VM locking functions for debugging
purposes and adds upper case macros to pass __FILE__ and __LINE__.
2025-06-09 13:57:18 -04:00
alpaca-tc
c8ddc0a843 Optimize callcache invalidation for refinements
Fixes [Bug #21201]

This change addresses a performance regression where defining methods
inside `refine` blocks caused severe slowdowns. The issue was due to
`rb_clear_all_refinement_method_cache()` triggering a full object
space scan via `rb_objspace_each_objects` to find and invalidate
affected callcaches, which is very inefficient.

To fix this, I introduce `vm->cc_refinement_table` to track
callcaches related to refinements. This allows us to invalidate
only the necessary callcaches without scanning the entire heap,
resulting in significant performance improvement.
2025-06-09 12:33:35 +09:00
Peter Zhu
347e581a4c Introduce MODULAR_GC_FN
MODULAR_GC_FN allows functions in gc.c to be defined as static when not
building with modular GC.
2025-06-06 10:46:55 -04:00
Peter Zhu
ea8b53a539 Allow pass special constants to the write barrier
Some GC implementations want to always know when an object is written to,
even if the written value is a special constant. Checking special constants
in rb_obj_written was a micro-optimization that made assumptions about
the GC implementation.
2025-06-03 12:04:24 -04:00
Peter Zhu
acc273a8a4 Remove dependancy of default.c on internal/object.h
We don't want the default GC to depend on Ruby internals so we can build
it as a modular GC.
2025-06-02 09:52:25 -04:00
Koichi Sasada
ef2bb61018 Ractor::Port
* Added `Ractor::Port`
  * `Ractor::Port#receive` (support multi-threads)
  * `Rcator::Port#close`
  * `Ractor::Port#closed?`
* Added some methods
  * `Ractor#join`
  * `Ractor#value`
  * `Ractor#monitor`
  * `Ractor#unmonitor`
* Removed some methods
  * `Ractor#take`
  * `Ractor.yield`
* Change the spec
  * `Racotr.select`

You can wait for multiple sequences of messages with `Ractor::Port`.

```ruby
ports = 3.times.map{ Ractor::Port.new }
ports.map.with_index do |port, ri|
  Ractor.new port,ri do |port, ri|
    3.times{|i| port << "r#{ri}-#{i}"}
  end
end

p ports.each{|port| pp 3.times.map{port.receive}}

```

In this example, we use 3 ports, and 3 Ractors send messages to them respectively.
We can receive a series of messages from each port.

You can use `Ractor#value` to get the last value of a Ractor's block:

```ruby
result = Ractor.new do
  heavy_task()
end.value
```

You can wait for the termination of a Ractor with `Ractor#join` like this:

```ruby
Ractor.new do
  some_task()
end.join
```

`#value` and `#join` are similar to `Thread#value` and `Thread#join`.

To implement `#join`, `Ractor#monitor` (and `Ractor#unmonitor`) is introduced.

This commit changes `Ractor.select()` method.
It now only accepts ports or Ractors, and returns when a port receives a message or a Ractor terminates.

We removes `Ractor.yield` and `Ractor#take` because:
* `Ractor::Port` supports most of similar use cases in a simpler manner.
* Removing them significantly simplifies the code.

We also change the internal thread scheduler code (thread_pthread.c):
* During barrier synchronization, we keep the `ractor_sched` lock to avoid deadlocks.
  This lock is released by `rb_ractor_sched_barrier_end()`
  which is called at the end of operations that require the barrier.
* fix potential deadlock issues by checking interrupts just before setting UBF.

https://bugs.ruby-lang.org/issues/21262
2025-05-31 04:01:33 +09:00
Kunshan Wang
d2a1ad00cb [ruby/mmtk] Fix environment variable parsing
Ues more idiomatic rust approaches.

ef125f9eae
2025-05-30 14:55:42 +00:00
Kunshan Wang
94688bdc7d [ruby/mmtk] Fix clippy warnings and formatting.
We also enable `#![warn(unsafe_op_in_unsafe_fn)]` in the whole mmtk_ruby
crate.

8b8025f71a
2025-05-30 14:55:42 +00:00
Kunshan Wang
d8774ec98f [ruby/mmtk] Bump MMTk and dependencies version
de252637ec
2025-05-30 14:55:41 +00:00
Kunshan Wang
60de513d05 [ruby/mmtk] Remove unused constant
Remove the unused constant HAS_MOVED_GFIELDSTBL and related methods.

In the mmtk/mmtk-ruby repo, we are now able to find the global field
(IV) table of a moved object during copying GC without using the
HAS_MOVED_GFIELDSTBL bit.  We synchronize some of the code, although we
haven't implemented moving GC in ruby/mmtk, yet.

See: 13080acdf5

400ba4e747
2025-05-30 14:55:41 +00:00
Peter Zhu
38ecaca155 [ruby/mmtk] Remove dependance on internal/object.h
fdc13963f0
2025-05-29 14:46:49 +00:00
John Hawthorn
f483befd90 Add shape_id to RBasic under 32 bit
This makes `RBobject` `4B` larger on 32 bit systems
but simplifies the implementation a lot.

[Feature #21353]

Co-authored-by: Jean Boussier <byroot@ruby-lang.org>
2025-05-26 10:31:54 +02:00
Peter Zhu
e00c46017b Drop unnecessary compiler guards for memory_sanitizer
We unpoison slots allocated out of the GC, so we don't need to disable
the assertions that read from the memory.
2025-05-23 14:35:56 -04:00
Peter Zhu
9130023cf5 Remove dependency on bits.h in default.c when BUILDING_MODULAR_GC
We can assume that the compiler will have __builtin_clzll so we can implement
nlz_int64 using that.
2025-05-23 14:35:56 -04:00
Peter Zhu
3487117e55 [ruby/mmtk] Fix object ID in rb_gc_impl_define_finalizer
The 0th element of the finalizer table array should be the object ID.

75e4a82652
2025-05-21 14:35:36 +00:00
Peter Zhu
27b0638386 [ruby/mmtk] Fix object ID for finalizers
We should get the object ID for finalizers in rb_gc_impl_define_finalizer
instead of when we create the finalizer job in make_final_job because
when we are in multi-Ractor mode, object ID needs to walk the references
which allocates an identity hash table. We cannot allocate in make_final_job
because it is in a MMTk worker thread.

922f22a690
2025-05-21 02:30:33 +00:00
Peter Zhu
b043abc048 Only define RVALUE_OVERHEAD if undefined
This allows RVALUE_OVERHEAD to be defined elsewhere.
2025-05-20 12:57:23 -04:00
Jean Boussier
1e33a451bb gc: Execute run_final with the lock held
The finalizer table can't be read nor modified without the VM lock.
2025-05-16 20:16:52 +02:00
Jean Boussier
ec8900e3eb rb_gc_impl_copy_finalizer: generate a new object id
Fix a regression introduced by: https://github.com/ruby/ruby/pull/13155
2025-05-16 20:16:52 +02:00
Jean Boussier
a294427017 Add missing lock to rb_gc_impl_copy_finalizer 2025-05-16 20:16:52 +02:00
Peter Zhu
04f538c144 Remove dependency on sanitizers.h in default.c when BUILDING_MODULAR_GC 2025-05-15 14:13:53 -04:00
Jean Boussier
ed632cd0ba Add missing lock in rb_gc_impl_undefine_finalizer
The table is global so accesses must be synchronized.
2025-05-15 13:32:08 +02:00
Jean Boussier
3d1b8e7298 newobj_fill: don't assume RBasic size
The previous implementation assumed `RBasic` size is `2 * sizeof(VALUE)`,
might as well not make assumption and use a proper `sizeof`.

Co-Authored-By: John Hawthorn <john@hawthorn.email>
2025-05-15 13:26:26 +02:00
Alan Wu
92b218fbc3 YJIT: ZJIT: Allow both JITs in the same build
This commit allows building YJIT and ZJIT simultaneously, a "combo
build". Previously, `./configure --enable-yjit --enable-zjit` failed. At
runtime, though, only one of the two can be enabled at a time.

Add a root Cargo workspace that contains both the yjit and zjit crate.
The common Rust build integration mechanisms are factored out into
defs/jit.mk.

Combo YJIT+ZJIT dev builds are supported; if either JIT uses
`--enable-*=dev`, both of them are built in dev mode.

The combo build requires Cargo, but building one JIT at a time with only
rustc in release build remains supported.
2025-05-15 00:39:03 +09:00
Jean Boussier
f9c3feccf4 Rename id_to_obj_tbl -> id2ref_tbl
As well as associated functions, this should make it more obvious
what the purpose is.
2025-05-14 11:41:14 +02:00
John Hawthorn
3306d7d2f9 Only clear Ractor cache in child
This avoids a race condition where we were clearing the cache from
another ractor while it was in use. Oops!

Fixes this failure http://ci.rvm.jp/results/master@oci-aarch64/5750416
2025-05-09 16:15:54 -07:00
Jean Boussier
15e3675ecc Remove dead code in rb_gc_impl_ractor_cache_free
Followup: https://github.com/ruby/ruby/pull/13286
2025-05-09 11:13:52 +02:00
John Hawthorn
30ef0f180b Fix allocation count when forking with Ractors
After fork we reset to single ractor mode (which IMO we shouldn't do,
but it requires more work to fix) and so we need to add the pending
object counts back to the main heap.
2025-05-09 00:55:14 -07:00
Peter Zhu
c18bedcdbb Remove dependency on debug_counter.h when BUILDING_MODULAR_GC
This allows the default GC to not need debug_counter.h when building as a
modular GC.
2025-05-08 10:36:27 -04:00
Peter Zhu
3f5080e767 Stop checking for USE_DEBUG_COUNTER in default.c
We don't need to check for USE_DEBUG_COUNTER because the code is no-op
if USE_DEBUG_COUNTER is not enabled.
2025-05-08 10:36:27 -04:00
Jean Boussier
2d1241ba97 Get rid of RB_GC_VM_ID_TO_OBJ_TABLE_KEYS 2025-05-08 07:58:05 +02:00
Jean Boussier
f48e45d1e9 Move object_id in object fields.
And get rid of the `obj_to_id_tbl`

It's no longer needed, the `object_id` is now stored inline
in the object alongside instance variables.

We still need the inverse table in case `_id2ref` is invoked, but
we lazily build it by walking the heap if that happens.

The `object_id` concern is also no longer a GC implementation
concern, but a generic implementation.

Co-Authored-By: Matt Valentine-House <matt@eightbitraptor.com>
2025-05-08 07:58:05 +02:00
Jean Boussier
0ea210d1ea Rename ivptr -> fields, next_iv_index -> next_field_index
Ivars will longer be the only thing stored inline
via shapes, so keeping the `iv_index` and `ivptr` names
would be confusing.

Instance variables won't be the only thing stored inline
via shapes, so keeping the `ivptr` name would be confusing.

`field` encompass anything that can be stored in a VALUE array.

Similarly, `gen_ivtbl` becomes `gen_fields_tbl`.
2025-05-08 07:58:05 +02:00
Peter Zhu
3e94b5f9c0 Remove dependence on internal/hash.h for default GC 2025-05-07 11:47:13 -04:00
Étienne Barrié
cb772247e7 Improve correctness contention for allocated object counts
Currently the count of allocated object for a heap is incremented
without regards to parallelism which leads to incorrect counts.

By maintaining a local counter in the ractor newobj cache, and only
syncing atomically with some granularity, we can improve the correctness
without increasing contention.

The allocated object count is also synced when the ractor is freed.

Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
2025-05-06 19:13:59 +02:00
Nobuyoshi Nakada
c772d2691d
Count metadata entries automatically from the names list 2025-04-25 19:40:04 +09:00