summaryrefslogtreecommitdiff
path: root/include/ruby/internal
AgeCommit message (Collapse)Author
9 days[DOC] Update docs for rb_interned_str and related functions (#15897)Herwin
Related to [Bug #21842]. * rb_interned_str: document what decides whether the returned string is in US-ASCII or BINARY encoding. * rb_interned_str_cstr: include the same description as rb_interned_str for the encoding. This one was still missing the update for US-ASCII and erroneously said the returned string was alwasy in BINARY encoding * rb_str_to_interned_str: document how the encoding of the result is defined. Co-authored-by: Herwin <herwinw@users.noreply.github.com>
11 days[Bug #21842] Let `rb_interned_str` return US-ASCII if possibleNobuyoshi Nakada
12 daysExpand `rb_data_type_t::reserved` for future useNobuyoshi Nakada
2026-01-13[nit] refactor rename inline functions卜部昌平
Pretty sure commit 0f64da9672d88921439f6fdb306d16fece9b9c90 didn't intend to welcome extension libraries to use these functions.
2026-01-05[Bug #18433] Remove unneeded declarationNobuyoshi Nakada
This `rb_cObject` declaration was only for `rb_cData()` that was removed at 7c738ce5e649b82bdc1305d5c347e81886ee759a.
2026-01-04Add field handle_weak_references to TypedDataPeter Zhu
This commit adds a field handle_weak_references to rb_data_type_struct for the callback when handling weak references. This avoids TypedData objects from needing to expose their rb_data_type_struct and weak references function.
2026-01-01Extract `RBIMPL_TYPEDDATA_PRECONDITION`Nobuyoshi Nakada
2025-12-31[DOC] Move typed-data related macrosNobuyoshi Nakada
The flags for `rb_data_type_t::flags` are public constants for defining `rb_data_type_t`. The embedded data flag and mask are internal implementation detail.
2025-12-31Make `RTYPEDDATA_EMBEDDABLE_P` internal-use onlyNobuyoshi Nakada
It should not be exposed because it is so implementation specific that it is only used in gc.c even within the entire Ruby source tree.
2025-12-29[DOC] State that `rb_unexpected_type` is privateNobuyoshi Nakada
2025-12-29Return `NULL` in a `void *` functionNobuyoshi Nakada
2025-12-29Make `rb_check_typeddata` and `rbimpl_check_typeddata` identicalNobuyoshi Nakada
2025-12-29Declare `rb_data_typed_t` parameters and return values as nonullNobuyoshi Nakada
2025-12-29rtypeddata.h: Add missing `RBIMPL_CAST`Nobuyoshi Nakada
In public headers, casts should be enclosed in `RBIMPL_CAST` for compilation in C++.
2025-12-26Remove an useless macro definition `RSTRUCT_PTR`Nobuyoshi Nakada
The underlying definition, `rb_struct_ptr`, was removed four years ago.
2025-12-26Remove `rb_iterate` deprecated since 1.9Nobuyoshi Nakada
2025-12-26Remove `RUBY_FL_DUPPED` deprecated for 4 yearsNobuyoshi Nakada
2025-12-26Remove taintedness/trustedness enums/macros deprecated for 4 yearsNobuyoshi Nakada
2025-12-26Remove `rb_gc_force_recycle` deprecated as "removed soon"Nobuyoshi Nakada
2025-12-26Remove `rb_clone_setup` and `rb_dup_setup` deprecated for 4 yearsNobuyoshi Nakada
2025-12-26Remove `rb_complex_polar` deprecated for 7 yearsNobuyoshi Nakada
2025-12-26Remove old APIs to allocate a data object deprecated for 5 yearsNobuyoshi Nakada
2025-12-26Deprecate old VCNobuyoshi Nakada
2025-12-26Development of 4.1.0 started.Yukihiro "Matz" Matsumoto
2025-12-25Implement declaring weak referencesPeter Zhu
[Feature #21084] # Summary The current way of marking weak references uses `rb_gc_mark_weak(VALUE *ptr)`. This presents challenges because Ruby's GC is incremental, meaning that if the `ptr` changes (e.g. realloc'd or free'd), then we could have an invalid memory access. This also overwrites `*ptr = Qundef` if `*ptr` is dead, which prevents any cleanup to be run (e.g. freeing memory or deleting entries from hash tables). This ticket proposes `rb_gc_declare_weak_references` which declares that an object has weak references and calls a cleanup function after marking, allowing the object to clean up any memory for dead objects. # Introduction In [[Feature #19783]](https://bugs.ruby-lang.org/issues/19783), I introduced an API allowing objects to mark weak references, the function signature looks like this: ```c void rb_gc_mark_weak(VALUE *ptr); ``` `rb_gc_mark_weak` is called during the marking phase of the GC to specify that the memory at `ptr` holds a pointer to a Ruby object that is weakly referenced. `rb_gc_mark_weak` appends this pointer to a list that is processed after the marking phase of the GC. If the object at `*ptr` is no longer alive, then it overwrites the object reference with a special value (`*ptr = Qundef`). However, this API resulted in two challenges: 1. Ruby's default GC is incremental, which means that the GC is not ran in one phase, but rather split into chunks of work that interleaves with Ruby execution. The `ptr` passed into `rb_gc_mark_weak` could be on the malloc heap, and that memory could be realloc'd or even free'd. We had to use workarounds such as `rb_gc_remove_weak` to ensure that there were no illegal memory accesses. This made `rb_gc_mark_weak` difficult to use, impacted runtime performance, and increased memory usage. 2. When an object dies, `rb_gc_mark_weak` only overwites the reference with `Qundef`. This means that if we want to do any cleanup (e.g. free a piece of memory or delete a hash table entry), we could not do that and had to defer this process elsewhere (e.g. during marking or runtime). In this ticket, I'm proposing a new API for weak references. Instead of an object marking its weak references during the marking phase, the object declares that it has weak references using the `rb_gc_declare_weak_references` function. This declaration occurs during runtime (e.g. after the object has been created) rather than during GC. After an object declares that it has weak references, it will have its callback function called after marking as long as that object is alive. This callback function can then call a special function `rb_gc_handle_weak_references_alive_p` to determine whether its references are alive. This will allow the callback function to do whatever it wants on the object, allowing it to perform any cleanup work it needs. This significantly simplifies the code for `ObjectSpace::WeakMap` and `ObjectSpace::WeakKeyMap` because it no longer needs to have the workarounds for the limitations of `rb_gc_mark_weak`. # Performance The performance results below demonstrate that `ObjectSpace::WeakMap#[]=` is now about 60% faster because the implementation has been simplified and the number of allocations has been reduced. We can see that there is not a significant impact on the performance of `ObjectSpace::WeakMap#[]`. Base: ``` ObjectSpace::WeakMap#[]= 4.620M (± 6.4%) i/s (216.44 ns/i) - 23.342M in 5.072149s ObjectSpace::WeakMap#[] 30.967M (± 1.9%) i/s (32.29 ns/i) - 154.998M in 5.007157s ``` Branch: ``` ObjectSpace::WeakMap#[]= 7.336M (± 2.8%) i/s (136.31 ns/i) - 36.755M in 5.013983s ObjectSpace::WeakMap#[] 30.902M (± 5.4%) i/s (32.36 ns/i) - 155.901M in 5.064060s ``` Code: ``` require "bundler/inline" gemfile do source "https://rubygems.org" gem "benchmark-ips" end wmap = ObjectSpace::WeakMap.new key = Object.new val = Object.new wmap[key] = val Benchmark.ips do |x| x.report("ObjectSpace::WeakMap#[]=") do |times| i = 0 while i < times wmap[Object.new] = Object.new i += 1 end end x.report("ObjectSpace::WeakMap#[]") do |times| i = 0 while i < times wmap[key] wmap[val] # does not exist i += 1 end end end ``` # Alternative designs Currently, `rb_gc_declare_weak_references` is designed to be an internal-only API. This allows us to assume the object types that call `rb_gc_declare_weak_references`. In the future, if we want to open up this API to third parties, we may want to change this function to something like: ```c void rb_gc_add_cleaner(VALUE obj, void (*callback)(VALUE obj)); ``` This will allow the third party to implement a custom `callback` that gets called after the marking phase of GC to clean up any dead references. I chose not to implement this design because it is less efficient as we would need to store a mapping from `obj` to `callback`, which requires extra memory.
2025-12-18thread_sync.c: declare queue_data_type as parent of szqueue_data_type.Jean Boussier
Allows to remove some duplicated code like szqueue_length, etc.
2025-12-18[DOC] Fix documents of `rb_intern_str` and so onNobuyoshi Nakada
* `rb_intern_str`: the argument must be `T_STRING`, no conversion. * `rb_intern_str`, `rb_check_id`, `rb_to_id`, `rb_check_symbol`: raise `EncodingError` unless the "name" argument is a valid string in its encoding.
2025-12-18[DOC] Re-fill the paragraphNobuyoshi Nakada
2025-12-18[DOC] Fix double-word typos in commentsv4.0.0-preview3Augustin Gottlieb
Found via `grep` for repeated words. * set.c: Fix "or or" * include/ruby/internal/symbol.h: Fix "is is" * include/ruby/internal/ctype.h: Fix "in in"
2025-12-17Rename to `struct rbimpl_size_overflow_tag`Nobuyoshi Nakada
This struct is used for addition not only for multiplication, so remove the word `mul`, and make the member names more descriptive.
2025-12-16gc.h: Reintroduce immediate guard in `rb_obj_written`Jean Boussier
This guard was removed in https://github.com/ruby/ruby/pull/13497 on the justification that some GC may need to be notified even for immediate. But the two currently available GCs don't, and there are plenty of assumtions GCs don't everywhere, notably in YJIT and ZJIT. This optimization is also not so micro (but not huge either). I routinely see 1-2% wasted there on micro-benchmarks. So perhaps if in the future we actually need this, it might make sense to introduce a way for GCs to declare that as an option, but in the meantime it's extra overhead with little gain.
2025-12-13Fix documentation of RB_PASS_CALLED_KEYWORDS in C APIBenoit Daloze
2025-12-12Fewer calls to `GET_EC()` and `GET_THREAD()` (#15506)Luke Gruber
The changes are to `io.c` and `thread.c`. I changed the API of 2 exported thread functions from `internal/thread.h` that didn't look like they had any use in C extensions: * rb_thread_wait_for_single_fd * rb_thread_io_wait I didn't change the following exported internal function because it's used in C extensions: * rb_thread_fd_select I added a comment to note that this function, although internal, is used in C extensions.
2025-12-10Add `NUM2PTR` and `PTR2NUM` macrosNobuyoshi Nakada
These macros have been defined here and there, so collect them.
2025-12-09Don't declare `rbimpl_check_typeddata` as pureJean Boussier
[Bug #21771] It may raise so it's incorrect and can lead to the compiler optimizing the call out.
2025-12-09Fix a typo in the deprecation warning messageNobuyoshi Nakada
2025-12-08Move RBIMPL_ATTR_DEPRECATED_* macros to the appropriate header fileKazuki Yamaguchi
Move these macros from include/ruby/backward.h to include/ruby/internal/attr/deprecated.h, alongside the other similar macros. include/ruby/internal/intern/vm.h cannot currently use them because include/ruby/backward.h is included too late.
2025-12-05fix typo s/sharable/shareable/Koichi Sasada
2025-12-05(experimental) RUBY_TYPED_FROZEN_SHAREABLE_NO_RECKoichi Sasada
`T_DATA` has a flag `RUBY_TYPED_FROZEN_SHAREABLE` which means if the `T_DATA` object is frozen, it can be sharable. On the `Ractor.make_sharable(obj)`, rechable objects from the `T_DATA` object will be apply `Ractor.make_shareable` recursively. `RUBY_TYPED_FROZEN_SHAREABLE_NO_REC` is similar to the `RUBY_TYPED_FROZEN_SHAREABLE`, but doesn't apply `Ractor.make_sharable` recursively for children. If it refers to unshareable objects, it will simply raise an error. I'm not sure this pattern is common or not, so it is not in public. If we find more cases, we can discuss publication.
2025-12-04Deprecate `rb_eval_cmd_kw`Nobuyoshi Nakada
2025-12-03Speedup TypedData_Get_StructJean Boussier
While profiling `Monitor#synchronize` and `Mutex#synchronize` I noticed a fairly significant amount of time spent in `rb_check_typeddata`. By implementing a fast path that assumes the object is valid and that can be inlined, it does make a significant difference: Before: ``` Mutex 13.548M (± 3.6%) i/s (73.81 ns/i) - 68.566M in 5.067444 Monitor 10.497M (± 6.5%) i/s (95.27 ns/i) - 52.529M in 5.032698s ``` After: ``` Mutex 20.887M (± 0.3%) i/s (47.88 ns/i) - 106.021M in 5.075989s Monitor 16.245M (±13.3%) i/s (61.56 ns/i) - 80.705M in 5.099680s ``` ```ruby require 'bundler/inline' gemfile do gem "benchmark-ips" end mutex = Mutex.new require "monitor" monitor = Monitor.new Benchmark.ips do |x| x.report("Mutex") { mutex.synchronize { } } x.report("Monitor") { monitor.synchronize { } } end ```
2025-11-19Win32: Drop support for older than MSVC 8.0/_MSC_VER 1400Nobuyoshi Nakada
Visual C++ 2005 (8.0): - _MSC_VER: 1400 - MSVCRT_VERSION: 80
2025-11-11simplify RSRING_GETMEM() definition.nagachika
2025-11-11include/ruby/internal/core/rstring.h: Remove rbimpl_rstring_getmem() definition.nagachika
2025-11-11Remove rbimpl_rstring_getmem() usage as workaround for GCC 15.2.1 ↵nagachika
optimization bug. [Bug #21655]
2025-11-07Remove `rb_path_check` declarationEarlopain
Implementation was removed in https://github.com/ruby/ruby/commit/a4c051b870ac7f7b3c5482baf05600e1f6751b47
2025-11-07Development of 4.0.0 started.Yukihiro "Matz" Matsumoto
2025-11-07Rename Namespace to Ruby::BoxSatoshi Tagomori
2025-10-25Use pointer to the memberNobuyoshi Nakada
Instead of the offset calculation.
2025-10-25[DOC] Follow up GH-14470Nobuyoshi Nakada
`IS_TYPED_DATA` is no longer a flag in `type`, and the "embedded" flag has been shifted accordingly. ruby/ruby#14470