726 Commits

Author SHA1 Message Date
Peter Zhu
98b36f6f36 Use rb_gc_vm_weak_table_foreach for reference updating
We can use rb_gc_vm_weak_table_foreach for reference updating of weak tables
in the default GC.
2025-01-27 10:28:36 -05:00
Peter Zhu
89240eb2fb Add generic ivar reference updating step
Previously, generic ivars worked differently than the other global tables
during compaction. The other global tables had their references updated
through iteration during rb_gc_update_vm_references. Generic ivars updated
the keys when the object moved and updated the values while reference
updating the object. This is inefficient as this required one lookup for
every moved object and one lookup for every object with generic ivars.

Instead, this commit changes it to iterate over the generic ivar table to
update both the keys and values.
2025-01-22 08:54:52 -05:00
Nobuyoshi Nakada
e5e4db1748 [Feature #20884] Define toplevel "Ruby" module with constants 2024-12-25 19:12:31 +09:00
Peter Zhu
429b867d1b Fix autoload_table_compact
The keys are IDs, so we cannot update references on them.
2024-12-17 11:03:38 -05:00
Nobuyoshi Nakada
4d86f3bf6d [Feature #20884] Reserve "Ruby" toplevel name 2024-12-12 17:45:06 +09:00
Matt Valentine-House
551be8219e Place all non-default GC API behind USE_SHARED_GC
So that it doesn't get included in the generated binaries for builds
that don't support loading shared GC modules

Co-Authored-By: Peter Zhu <peter@peterzhu.ca>
2024-11-25 13:05:23 +00:00
John Hawthorn
34e36a72a7 Ensure global variable traces are freed at exit
ASAN_OPTIONS="detect_leaks=1" RUBY_FREE_AT_EXIT=1 ./miniruby -e 'trace_var(:$x){}'
2024-11-23 01:16:48 -08:00
Nobuyoshi Nakada
9c777f282f
[Bug #20900] Warn deprecated constant when removing 2024-11-19 12:43:38 +09:00
Jean byroot Boussier
6deeec5d45
Mark strings returned by Symbol#to_s as chilled (#12065)
* Use FL_USER0 for ELTS_SHARED

This makes space in RString for two bits for chilled strings.

* Mark strings returned by `Symbol#to_s` as chilled

[Feature #20350]

`STR_CHILLED` now spans on two user flags. If one bit is set it
marks a chilled string literal, if it's the other it marks a
`Symbol#to_s` chilled string.

Since it's not possible, and doesn't make much sense to include
debug info when `--debug-frozen-string-literal` is set, we can't
include allocation source, but we can safely include the symbol
name in the warning message, making it much easier to find the source
of the issue.

Co-Authored-By: Étienne Barrié <etienne.barrie@gmail.com>

---------

Co-authored-by: Étienne Barrié <etienne.barrie@gmail.com>
Co-authored-by: Jean Boussier <jean.boussier@gmail.com>
2024-11-13 09:20:00 -05:00
Koichi Sasada
aa63699d10 support require in non-main Ractors
Many libraries should be loaded on the main ractor because of
setting constants with unshareable objects and so on.

This patch allows to call `requore` on non-main Ractors by
asking the main ractor to call `require` on it. The calling ractor
waits for the result of `require` from the main ractor.

If the `require` call failed with some reasons, an exception
objects will be deliverred from the main ractor to the calling ractor
if it is copy-able.

Same on `require_relative` and `require` by `autoload`.

Now `Ractor.new{pp obj}` works well (the first call of `pp` requires
`pp` library implicitly).

[Feature #20627]
2024-11-08 18:02:46 +09:00
Alan Wu
ded078c2c4
YJIT: Fastpath for Module#name (#11819)
Module#name shows up as a top C method callee in lobsters so probably
common enough. It's also easy to substitute thanks to rb_mod_name()
already having no GC yield points.

    klass = BasicObject
    50_000_000.times { klass.name }

    Benchmark 1: /.rubies/post/bin/ruby --yjit mod_name.rb
      Time (mean ± σ):      1.433 s ±  0.010 s    [User: 1.410 s, System: 0.010 s]
      Range (min … max):    1.421 s …  1.449 s    10 runs

    Benchmark 2: /.rubies/mstr/bin/ruby --yjit mod_name.rb
      Time (mean ± σ):      1.491 s ±  0.012 s    [User: 1.468 s, System: 0.010 s]
      Range (min … max):    1.470 s …  1.511 s    10 runs

    Summary
      /.rubies/post/bin/ruby --yjit mod_name.rb ran
        1.04 ± 0.01 times faster than /.rubies/mstr/bin/ruby --yjit mod_name.rb
2024-10-08 11:44:59 -04:00
Peter Zhu
cd86393ad8 Assume VM locked in rb_mark_generic_ivar
rb_gen_ivtbl_get locks the VM, but the VM must be locked during marking
so we can directly read from the generic_ivtbl without locking the VM.
2024-10-04 08:50:43 -04:00
Alan Wu
99825a539f [DOC] Note that rb_obj_freeze_inline() can raise NoMemoryError
And move it back to a public header because Doxygen might not be
scanning the .c files.

[Feature #18776]
2024-07-17 10:25:20 -04:00
Peter Zhu
32683aa18d Remove use of symbols and arrays when freeing global table
This removes the use of symbol and array objects when freeing the global
table so we can now free symbols and arrays earlier.
2024-06-11 10:33:51 -04:00
Jean Boussier
730e3b2ce0 Stop exposing rb_str_chilled_p
[Feature #20205]

Now that chilled strings no longer appear as frozen, there is no
need to offer an API to check for chilled strings.

We however need to change `rb_check_frozen_internal` to no
longer be a macro, as it needs to check for chilled strings.
2024-06-02 13:53:35 +02:00
Jean Boussier
7380e3d30f RB_OBJ_FREEZE_RAW: Set the object shape 2024-04-16 17:20:35 +02:00
Xavier Noria
077ac25ed8 Iterate the documentation of Module.const_missing 2024-03-15 22:21:43 +09:00
Xavier Noria
9284fe123e Remove unnecessary else branch
This matches the existing style in the rest of the file.
2024-03-15 22:21:43 +09:00
Xavier Noria
a26e3bf360 Minor edits to variable.c 2024-03-15 22:21:43 +09:00
Xavier Noria
0dbe3af417 Fix the signature of Module#name in the API docs 2024-03-15 22:21:43 +09:00
Peter Zhu
6b0434c0f7 Don't create per size pool shapes for non-T_OBJECT 2024-03-13 09:55:52 -04:00
Jean Boussier
a5c5f83b24 Make const_source_location return the real constant as soon as defined
[Bug #20188]

Ref: https://github.com/fxn/zeitwerk/issues/281#issuecomment-1893228355

Previously, it would only return the real constant location once the
autoload was fully completed.
2024-03-13 09:39:09 +01: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
Jean Boussier
b4a69351ec Move FL_SINGLETON to FL_USER1
This frees FL_USER0 on both T_MODULE and T_CLASS.

Note: prior to this, FL_SINGLETON was never set on T_MODULE,
so checking for `FL_SINGLETON` without first checking that
`FL_TYPE` was `T_CLASS` was valid. That's no longer the case.
2024-03-06 13:11:41 -05:00
cui fliter
226a889dc7
[DOC] fix some comments
Signed-off-by: cui fliter <imcusg@gmail.com>
2024-03-05 18:50:47 +09:00
Peter Zhu
df5b8ea4db Remove unneeded RUBY_FUNC_EXPORTED 2024-02-23 10:24:21 -05:00
Yusuke Endoh
a7718c914a Do not show an anonymous class as a receiver 2024-02-15 20:43:11 +09:00
Yusuke Endoh
25d74b9527 Do not include a backtick in error messages and backtraces
[Feature #16495]
2024-02-15 18:42:31 +09:00
Nobuyoshi Nakada
361b3efe16
Use UNDEF_P 2024-01-30 14:48:59 +09:00
Peter Zhu
d0b774cfb8 Remove null checks for xfree
xfree can handle null values, so we don't need to check it.
2024-01-19 10:25:02 -05:00
Peter Zhu
11286ac479 Fix memory leak in autoload_data
If the autoload_data has autoload_const and the autoload_data is freed
before the autoload_const, then the autoload_data will leak.

This commit changes it so that when the autoload_data is freed, it will
clear the whole linked list of autoload_const so that the autoload_data
can be safely freed.

    1000.times do |i|
      str = "foo#{i}".freeze

      autoload(:"B#{i}", str)
      autoload(:"C#{i}", str)
    end

Reports leaked memory with the macOS leaks tool:

    12  ruby                                  0x1006398a4 rb_f_autoload + 96  load.c:1524
    11  ruby                                  0x100639710 rb_mod_autoload + 112  load.c:1460
    10  ruby                                  0x10080a914 rb_autoload_str + 224  variable.c:2666
    9   ruby                                  0x1007c3308 rb_mutex_synchronize + 56  thread_sync.c:637
    8   ruby                                  0x1005acb24 rb_ensure + 312  eval.c:1009
    7   ruby                                  0x10080aac8 autoload_synchronized + 204  variable.c:2630
    6   ruby                                  0x10080f8bc autoload_feature_lookup_or_create + 76  variable.c:2578
    5   ruby                                  0x1005c29a4 rb_data_typed_object_zalloc + 232  gc.c:3186
    4   ruby                                  0x1005c2774 ruby_xcalloc + 32  gc.c:14440
    3   ruby                                  0x1005cddf4 ruby_xcalloc_body + 56  gc.c:12878
    2   ruby                                  0x1005cde7c objspace_xcalloc + 124  gc.c:12871
    1   ruby                                  0x1005c1990 calloc1 + 28  gc.c:1906
    0   libsystem_malloc.dylib                0x18b2ebb78 _malloc_zone_calloc_instrumented_or_legacy + 100
2024-01-07 13:48:59 -05:00
Nobuyoshi Nakada
c30b8ae947
Adjust styles and indents [ci skip] 2024-01-08 00:50:41 +09:00
Victor Shepelev
570d7b2c3e
[DOC] Adjust some new features wording/examples. (#9183)
* Reword Range#overlap? docs last paragraph.

* Docs: add explanation about Queue#freeze

* Docs: Add :rescue event docs for TracePoint

* Docs: Enhance Module#set_temporary_name documentation

* Docs: Slightly expand Process::Status deprecations

* Fix MatchData#named_captures rendering glitch

* Improve Dir.fchdir examples

* Adjust Refinement#target docs
2023-12-14 23:01:48 +02: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
Samuel Williams
7e05e9ff94
Fix incorrect "nested_fake_name" documentation. (#9135) 2023-12-06 06:48:55 +00:00
Alan Wu
0346cbbc14 Fix parameter types for rb_ivar_foreach() callbacks
For this public API, the callback is declared to take
`(ID, VALUE, st_data_t)`, but it so happens that using
`(st_data_t, st_data_t, st_data_t)` also
type checks, because the underlying type is identical.
Use it as declared and get rid of some casts.
2023-12-05 18:19:42 -05:00
Peter Zhu
f40727f4aa Make rb_obj_copy_ivs_to_hash_table_i static 2023-12-04 09:58:41 -05:00
Peter Zhu
43dc8e9012 Fix indentation in ivar_set [ci skip] 2023-11-28 08:12:35 -05:00
Peter Zhu
269c705f93 Fix compaction for generic ivars
When generic instance variable has a shape, it is marked movable. If it
it transitions to too complex, it needs to update references otherwise
it may have incorrect references.
2023-11-24 13:29:04 -05:00
Peter Zhu
7f7613c2c7 Fix compacting during evacuation of generic ivars
When evacuating generic instance variables, the instance variables exist
in both the array and the ST table. We need to ensure it has switched
to the ST table before performing any operations that can trigger GC
compaction.
2023-11-23 09:11:24 -05:00
Aaron Patterson
209a0253f5 Use count macros for counting instance variables
We don't need to check for Qundef because the shape tells us the number
if IVs that are stored on the object
2023-11-21 09:46:50 -08:00
Peter Zhu
7e7e2dde24 Fix memory leak when evacuating generic ivars
The lookup in the table is using the wrong key when converting generic
instance variables to too complex, which means that it never looks up
the entry which leaks memory when the entry is overwritten.
2023-11-21 10:27:02 -05:00
Aaron Patterson
6fce8c7980 Don't try compacting ivars on Classes that are "too complex"
Too complex classes use a hash table to store ivs, and should always pin
their IVs.  We shouldn't touch those classes in compaction.
2023-11-20 16:09:48 -08:00
Peter Zhu
f376163194 Fix crash when evacuating generic ivar
When transitioning generic instance variable objects to too complex, we
set the shape first before performing inserting the new gen_ivtbl. The
st_insert for the new gen_ivtbl could allocate and cause a GC. If that
happens, then it will crash because the object will have a too complex
shape but not yet be backed by a st_table.

This commit changes the order so that the insert happens first before
the new shape is set.

The following script reproduces the issue:

```
o = []
o.instance_variable_set(:@a, 1)

i = 0
o = Object.new
while RubyVM::Shape.shapes_available > 0
  o.instance_variable_set(:"@i#{i}", 1)
  i += 1
end

ary = 1_000.times.map { [] }

GC.stress = true
ary.each do |o|
  o.instance_variable_set(:@a, 1)
  o.instance_variable_set(:@b, 1)
end
```
2023-11-20 16:57:24 -05:00
Peter Zhu
9d51ab8b3d Fix indentation [ci skip] 2023-11-20 13:43:31 -05:00
Peter Zhu
83da4a7e62 Fix crash when iterating over generic ivars 2023-11-20 10:13:18 -05:00
Jean Boussier
94c9f16663 Refactor rb_obj_evacuate_ivs_to_hash_table
That function is a bit too low level to called from multiple
places. It's always used in tandem with `rb_shape_set_too_complex`
and both have to know how the object is laid out to update the
`iv_ptr`.

So instead we can provide two higher level function:

  - `rb_obj_copy_ivs_to_hash_table` to prepare a `st_table` from an
    arbitrary oject.
  - `rb_obj_convert_to_too_complex` to assign the new `st_table`
    to the old object, and safely free the old `iv_ptr`.

Unfortunately both can't be combined into one, because `rb_obj_copy_ivar`
need `rb_obj_copy_ivs_to_hash_table` to copy from one object
to another.
2023-11-17 09:19:21 +01:00
Jean Boussier
81b35fe729 rb_evict_ivars_to_hash: get rid of the sahpe paramater
It's only used to allocate the table with the right size,
but in some case we were passing `rb_shape_get_shape_by_id(SHAPE_OBJ_TOO_COMPLEX)`
which `next_iv_index` is a bit undefined.

So overall we're better to just allocate a table the size of the existing
object, it should be close enough in the vast majority of cases,
and that's already a de-optimizaton path anyway.
2023-11-16 17:49:59 +01:00
Peter Zhu
68869e9bd9 Revert "Revert "Remove SHAPE_CAPACITY_CHANGE shapes""
This reverts commit 5f3fb4f4e397735783743fe52a7899b614bece20.
2023-11-13 18:26:36 -05:00
Peter Zhu
7e6609e8f0 [ci skip] Fix indentation in rb_class_ivar_set 2023-11-10 12:43:06 -05:00