2372 Commits

Author SHA1 Message Date
fuhsnn
98aa2a6608 atomic.h: Add C11 <stdatomic.h> implementation
The implementation is only active if `HAVE_STDATOMIC_H` is defined,
and only after the compiler fails to match all currently supported
systems.
2025-07-17 15:46:37 +09:00
fuhsnn
a7992400f1 atomic.h: Use explicit logic for 32-bit #else branches
These branches are only active for 32-bit Windows and Solaris platforms,
codify the fact by changing `#else` to `#elif`'s that explicitly include
those targets and `#error`-out otherwise.
2025-07-17 15:46:37 +09:00
Jeremy Evans
2ab38691a2 Add Set C-API
This should be a minimal C-API needed to deal with Set objects. It
supports creating the sets, checking whether an element is the set,
adding and removing elements, iterating over the elements, clearing
a set, and returning the size of the set.

Co-authored-by: Nobuyoshi Nakada <nobu.nakada@gmail.com>
2025-07-11 15:24:23 +09:00
John Hawthorn
6b7f56d2db Never use flags on T_NODE
Previously, any time we used FL_TEST or variations we would need to add
an additional test that it wasn't a T_NODE. I think that was only needed
historically and we can avoid generating that extra code.
2025-06-25 15:58:53 -07:00
Jean Boussier
15084fbc3c Get rid of FL_EXIVAR
Now that the shape_id gives us all the same information, it's no
longer needed.
2025-06-13 23:50:30 +02:00
Samuel Williams
ead14b19aa Fix blocking_operation_wait use-after-free bug. 2025-06-06 13:13:16 +09: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
Jean Boussier
e9fd44dd72 shape.c: Implement a lock-free version of get_next_shape_internal
Whenever we run into an inline cache miss when we try to set
an ivar, we may need to take the global lock, just to be able to
lookup inside `shape->edges`.

To solve that, when we're in multi-ractor mode, we can treat
the `shape->edges` as immutable. When we need to add a new
edge, we first copy the table, and then replace it with
CAS.

This increases memory allocations, however we expect that
creating new transitions becomes increasingly rare over time.

```ruby
class A
  def initialize(bool)
    @a = 1
    if bool
      @b = 2
    else
      @c = 3
    end
  end

  def test
    @d = 4
  end
end

def bench(iterations)
  i = iterations
  while i > 0
    A.new(true).test
    A.new(false).test
    i -= 1
  end
end

if ARGV.first == "ractor"
  ractors = 8.times.map do
    Ractor.new do
      bench(20_000_000 / 8)
    end
  end
  ractors.each(&:take)
else
  bench(20_000_000)
end
```

The above benchmark takes 27 seconds in Ractor mode on Ruby 3.4,
and only 1.7s with this branch.

Co-Authored-By: Étienne Barrié <etienne.barrie@gmail.com>
2025-06-02 17:49:53 +02:00
Nobuyoshi Nakada
18a036a613
[Feature #21205] Define File::Stat#birthtime by statx 2025-05-30 20:55:12 +09:00
John Hawthorn
d1343e12d2 Use flag for RCLASS_IS_INITIALIZED
Previously we used a flag to set whether a module was uninitialized.
When checked whether a class was initialized, we first had to check that
it had a non-zero superclass, as well as that it wasn't BasicObject.

With the advent of namespaces, RCLASS_SUPER is now an expensive
operation, and though we could just check for the prime superclass, we
might as well take this opportunity to use a flag so that we can perform
the initialized check with as few instructions as possible.

It's possible in the future that we could prevent uninitialized classes
from being available to the user, but currently there are a few ways to
do that.
2025-05-28 11:44:07 -04: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
Samuel Williams
627a5ac53b
Bump fiber scheduler version and add missing documentation. (#13424) 2025-05-23 16:20:33 +09:00
Samuel Williams
73c9d6ccaa
Allow IO#close to interrupt IO operations on fibers using fiber_interrupt hook. (#12839) 2025-05-23 14:55:05 +09:00
Nobuyoshi Nakada
ec41b1e823 Fix for old mingw without clock_gettime and clock_getres 2025-05-22 13:08:39 +09:00
Alan Wu
2297afda7f
Include stdbool.h without checking with autoconf
As reported in <https://bugs.ruby-lang.org/issues/21340>, older autoconf
have an AC_HEADER_STDBOOL that's incompatible with C23. Autoconf 2.72
fixed the macro, but also mentions that it's obsolescent since all
current compilers have this header.

Since we require C99 [1] and VS 2015 [2], we might actually be able take
that suggestion and include stdbool.h without a check. I want to try
this on rubyci.org and will revert if this cause any issues. Not
touching AC_HEADER_STDBOOL in configure.ac for now.

[1]: https://bugs.ruby-lang.org/issues/15347
[2]: https://bugs.ruby-lang.org/issues/19982
2025-05-20 15:58:32 +00:00
Jean Boussier
76ec41bf3d Bump ABI_VERSION
`struct RTypedData` was changed significantly in https://github.com/ruby/ruby/pull/13190
which breaks many extensions.

Bumping the ABI version might save some people from needlessly
investigating crashes.
2025-05-14 21:01:32 +02:00
Jean Boussier
b5575a80bc Reduce Object#object_id contention.
If the object isn't shareable and already has a object_id
we can access it without a lock.

If we need to generate an ID, we may need to lock to find
the child shape.

We also generate the next `object_id` using atomics.
2025-05-14 14:41:46 +02:00
Jean Boussier
5974841d11 Remove outdated references to FL_SEEN_OBJ_ID 2025-05-13 12:02:19 +02:00
Samuel Williams
425fa0aeb5
Make waiting_fd behaviour per-IO. (#13127)
- `rb_thread_fd_close` is deprecated and now a no-op.
- IO operations (including close) no longer take a vm-wide lock.
2025-05-13 19:02:03 +09:00
Nobuyoshi Nakada
3e47e7a499 Fix redefinition of clock_gettime and clock_getres
winpthreads-git 12.0.0.r720 provides `clock_gettime` and
`clock_getres` as inline functions.
2025-05-12 02:50:25 +09:00
Satoshi Tagomori
382645d440 namespace on read 2025-05-11 23:32:50 +09: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
Nobuyoshi Nakada
bbf1130f91 Add RBIMPL_ATTR_NONSTRING_ARRAY() macro for GCC 15 2025-05-05 18:25:04 +09:00
Jeremy Evans
ce51ef30df Save one VALUE per embedded RTypedData
This halves the amount of memory used for embedded RTypedData if they
are one VALUE (8 bytes on 64-bit platforms) over the slot size limit.

For Set, on 64-bit it uses an embedded 56-byte struct.  With the
previous implementation, the embedded structs starts at offset 32,
resulting in a total size of 88.  Since that is over the 80 byte
limit, it goes to the next highest bucket, 160 bytes, wasting 72
bytes.  This allows it to fit in a 80 byte bucket, which reduces
the total size for small sets of from 224 bytes (160 bytes
embedded, 64 bytes malloc, 72 bytes wasted in embedding) to 144
bytes (80 bytes embedded, 64 bytes malloc, 0 bytes wasted in
embedding).

Any other embedded RTypedData will see similar advantages if they
are currently one VALUE over the limit.

To implement this, remove the typed_flag from struct RTypedData.
Embed the typed_flag information in the type member, which is
now a tagged pointer using VALUE type, using the bottom low 2 bits
as flags (1 bit for typed flag, the other for the embedded flag).
To get the actual pointer, RTYPEDDATA_TYPE masks out
the low 2 bits and then casts.  That moves the RTypedData data
pointer from offset 32 to offset 24 (on 64-bit).

Vast amount of code in the internals (and probably external C
extensions) expects the following code to work for both RData and
non-embedded RTypedData:

```c
DATA_PTR(obj) = some_pointer;
```

Allow this to work by moving the data pointer in RData between
the dmark and dfree pointers, so it is at the same offset (24
on 64-bit).

Other than these changes to the include files, the only changes
needed were to gc.c, to account for the new struct layouts,
handle setting the low bits in the type member, and to use
RTYPEDDATA_TYPE(obj) instead of RTYPEDDATA(obj)->type.
2025-05-05 09:46:32 +09:00
Jean Boussier
c65991978b get_next_shape_internal: Skip VM lock for single child case
If the shape has only one child, we check it lock-free without
compromising thread safety.

I haven't computed hard data as to how often that it the case,
but we can assume that it's not too rare for shapes to have
a single child that is often requested, typically when freezing
and object.
2025-04-30 23:32:33 +02:00
Nobuyoshi Nakada
b42afa1dbc
Suppress gcc 15 unterminated-string-initialization warnings 2025-04-30 20:04:10 +09:00
Alan Wu
719486a642 Fix C23 (GCC 15) WIN32 compatibility for rb_define_* functions
Fixes [Bug #21286]
2025-04-30 19:44:59 +09:00
Matt Valentine-House
5e8b744dbc RUBY_T_{TRUE,FALSE} comments were reversed
[ci skip]
2025-04-30 08:08:54 +02:00
John Hawthorn
b28363a838 Work on ATOMIC_VALUE_SET 2025-04-18 13:03:54 +09:00
Samuel Williams
8d21f666b8
Introduce enum rb_io_mode. (#7894) 2025-04-16 07:50:37 +00:00
Samuel Williams
4e970c5d5a Expose ruby_thread_has_gvl_p. 2025-04-14 18:28:09 +09:00
Richard Böhme
3aee7b982b Mark first argument to all C-API tracepoint functions as nonnull 2025-03-28 23:08:28 +09:00
Richard Böhme
04ebedf7f0 Make rb_tracearg_(parameters|eval_script|instruction_sequence) public C-API
This allows C-Extension developers to call those methods to retrieve
information about a TracePoint's parameters, eval script and
instruction sequence.

Implements [Feature #20757]
2025-03-28 23:08:28 +09:00
Nobuyoshi Nakada
bb7f1619d2
Suppress sign-conversion warning [ci skip] 2025-03-18 16:22:49 +09:00
Nobuyoshi Nakada
47d75b65bf Make wrapper of main for wasm more generic 2025-03-16 17:33:58 +09:00
Nobuyoshi Nakada
453f88f7f1 Make ASAN default option string built-in libruby
The content depends on ruby internal, not responsibility of the
caller.  Revive `RUBY_GLOBAL_SETUP` macro to define the hook function.
2025-03-16 17:33:58 +09:00
Nobuyoshi Nakada
42c0722f83
[DOC] Fix the comment for RUBY_CONST_ID and rb_intern
RUBY_CONST_ID has never been deprecated; `rb_intern` is handy but it
is using non-standard GCC extensions and does not cache the ID with
other compilers.
2025-02-28 12:55:46 +09:00
Peter Zhu
16f41eca53 Remove dead iv_index_tbl field in RObject 2025-02-12 14:03:07 -05:00
Nobuyoshi Nakada
c961d093b1 [Bug #21024] <cstdbool> header has been useless
And finally deprecated at C++-17.
Patched by jprokop (Jarek Prokop).
2025-01-14 21:56:14 +09:00
Nobuyoshi Nakada
8891890bff
Mark rb_path_check as internal only 2025-01-14 11:26:29 +09:00
Nobuyoshi Nakada
f7fd42ce74
Move the declaration of rb_path_check
Although this function is unrelated to hash, it was defined in hash.c
to check PATH environment variable originally.  Then the definition
was moeved to file.c but the declaration was left in the hash.c block.
2025-01-13 19:10:26 +09:00
Nobuyoshi Nakada
d9d08484d2
[DOC] Fix the description of rb_path_check
c.f. #20971
2025-01-12 13:53:15 +09:00
Nobuyoshi Nakada
1b3037081e
[Bug #21024] <cstdbool> header is deprecated in C++17 2025-01-11 12:21:57 +09:00
Peter Zhu
99ff0224a5 Move rbimpl_size_add_overflow from gc.c to memory.h 2025-01-02 11:03:04 -05:00
Yukihiro "Matz" Matsumoto
2f064b3b4b
Development of 3.5.0 started. 2024-12-25 18:15:17 +09:00
Naohisa Goto
528ec70604 use RBIMPL_ATTR_MAYBE_UNUSED
The macro MAYBE_UNUSED, prepared by ./configure, may not be defined in
some environments such as Oracle Developer Studio 12.5 on Solaris 10.

This fixes [Bug #20963]
2024-12-18 23:37:22 +09:00
Alan Wu
6336431a64 [DOC] rb_id2name(): Note truncation danger (+minor copyediting)
Thanks, nobu!
2024-12-17 21:50:00 -05:00
Peter Zhu
375fec7c53 [DOC] Add note to rb_id2name about GC compaction 2024-12-17 16:32:13 -05:00
Nobuyoshi Nakada
5a7a1a4a13
Win32: Fix rbimpl_size_mul_overflow on arm64
`_umul128` is specific to x86_64 platform, see higher words by
`__umulh` on arm64.
2024-12-17 20:25:06 +09:00