summaryrefslogtreecommitdiff
path: root/gc.c
AgeCommit message (Collapse)Author
2023-03-10Remove duplicate code in gc_marks_finishPeter Zhu
There is an identical block a few lines down that does the exact same thing. Notes: Merged: https://github.com/ruby/ruby/pull/7488
2023-03-10Revert "Allow classes and modules to become too complex"Aaron Patterson
This reverts commit 69465df4242f3b2d8e55fbe18d7c45b47b40a626.
2023-03-10Move WeakMap and WeakKeyMap code to weakmap.cPeter Zhu
These classes don't belong in gc.c as they're not actually part of the GC. This commit refactors the code by moving all the code into a weakmap.c file. Notes: Merged: https://github.com/ruby/ruby/pull/7451
2023-03-09Allow classes and modules to become too complexHParker
This makes the behavior of classes and modules when there are too many instance variables match the behavior of objects with too many instance variables. Notes: Merged: https://github.com/ruby/ruby/pull/7349
2023-03-09Fix interpreter crash caused by RUBY_INTERNAL_EVENT_NEWOBJ + RactorsKJ Tsanaktsidis
When a Ractor is created whilst a tracepoint for RUBY_INTERNAL_EVENT_NEWOBJ is active, the interpreter crashes. This is because during the early setup of the Ractor, the stdio objects are created, which allocates Ruby objects, which fires the tracepoint. However, the tracepoint machinery tries to dereference the control frame (ec->cfp->pc), which isn't set up yet and so crashes with a null pointer dereference. Fix this by not firing GC tracepoints if cfp isn't yet set up. Notes: Merged: https://github.com/ruby/ruby/pull/5990
2023-03-08Fix crash when allocating classes with newobj hookPeter Zhu
We need to zero out the whole slot when running the newobj hook for a newly allocated class because the slot could be filled with garbage, which would cause a crash if a GC runs inside of the newobj hook. For example, the following script crashes: ``` require "objspace" GC.stress = true ObjectSpace.trace_object_allocations { 100.times do Class.new end } ``` [Bug #19482] Notes: Merged: https://github.com/ruby/ruby/pull/7464
2023-03-08Adjust styles [ci skip]Nobuyoshi Nakada
2023-03-07Add function rb_data_freePeter Zhu
This commit adds a function rb_data_free used by obj_free and rb_objspace_call_finalizer to free T_DATA objects. This change also means that RUBY_TYPED_FREE_IMMEDIATELY objects can be freed immediately in rb_objspace_call_finalizer rather than being created into a zombie. Notes: Merged: https://github.com/ruby/ruby/pull/7438
2023-03-06s/mjit/rjit/Takashi Kokubun
Notes: Merged: https://github.com/ruby/ruby/pull/7462
2023-03-06Stop exporting symbols for MJITTakashi Kokubun
Notes: Merged: https://github.com/ruby/ruby/pull/7459
2023-03-06Crash when malloc during GCPeter Zhu
This feature was introduced in commit 2ccf6e5, but I realized that using rb_warn is a bad idea because it allocates objects, which causes a different crash ("object allocation during garbage collection phase"). We should just hard crash here instead. Notes: Merged: https://github.com/ruby/ruby/pull/7441
2023-02-27Fix spelling (#7389)John Bampton
Notes: Merged-By: k0kubun <takashikkbn@gmail.com>
2023-02-24[ci skip] Add note in gc.c about ambiguous casePeter Zhu
2023-02-24Fix incorrect line numbers in GC hookPeter Zhu
If the previous instruction is not a leaf instruction, then the PC was incremented before the instruction was ran (meaning the currently executing instruction is actually the previous instruction), so we should not increment the PC otherwise we will calculate the source line for the next instruction. This bug can be reproduced in the following script: ``` require "objspace" ObjectSpace.trace_object_allocations_start a = 1.0 / 0.0 p [ObjectSpace.allocation_sourceline(a), ObjectSpace.allocation_sourcefile(a)] ``` Which outputs: [4, "test.rb"] This is incorrect because the object was allocated on line 10 and not line 4. The behaviour is correct when we use a leaf instruction (e.g. if we replaced `1.0 / 0.0` with `"hello"`), then the output is: [10, "test.rb"]. [Bug #19456] Notes: Merged: https://github.com/ruby/ruby/pull/7357
2023-02-23Fix a warning on typedefTakashi Kokubun
../gc.c:13317:1: warning: ‘typedef’ is not at beginning of declaration [-Wold-style-declaration] 13317 | } typedef weakkeymap_entry_t; | ^
2023-02-23Implement ObjectSpace::WeakKeyMap basic allocatorJean Boussier
[Feature #18498] Notes: Merged: https://github.com/ruby/ruby/pull/5570
2023-02-22* remove trailing spaces. [ci skip]git
2023-02-22Make GC faster when RGENGC_CHECK_MODE >= 2Peter Zhu
We shouldn't run gc_verify_internal_consistency after every GC step when RGENGC_CHECK_MODE >= 2, only when GC has finished. Running it on every GC step makes it too slow.
2023-02-21Add marking and sweeping time to GC.statPeter Zhu
There is a `time` key in GC.stat that gives us the total time spent in GC. However, we don't know what proportion of the time is spent between marking and sweeping. This makes it difficult to tune the GC as we're not sure where to focus our efforts on. This PR adds keys `marking_time` and `sweeping_time` to GC.stat for the time spent marking and sweeping, in milliseconds. [Feature #19437] Notes: Merged: https://github.com/ruby/ruby/pull/7304
2023-02-21Refactor to separate marking and sweeping phasesPeter Zhu
This commit separates the marking and sweeping phases so that marking functions do not directly call sweeping functions. Notes: Merged: https://github.com/ruby/ruby/pull/7304
2023-02-17Remove USE_RGENGC_LOGGING_WB_UNPROTECTMatt Valentine-House
This macro is broken when set to anything other than 0. And has had a comment saying that it's broken for 3 years. This commit deletes it and the associated logging code. It's clearly not being used. Co-Authored-By: Peter Zhu <peter@peterzhu.ca> Notes: Merged: https://github.com/ruby/ruby/pull/7312
2023-02-16Fix compilation error when USE_RINCGC=0Nobuyoshi Nakada
2023-02-16Move `attached_object` into `rb_classext_struct`Jean Boussier
Given that signleton classes don't have an allocator, we can re-use these bytes to store the attached object in `rb_classext_struct` without making it larger. Notes: Merged: https://github.com/ruby/ruby/pull/7309
2023-02-15Check !RCLASS_EXT_EMBEDDED instead of SIZE_POOL_COUNT == 1Jean Boussier
It's much more self documenting and consistent Notes: Merged: https://github.com/ruby/ruby/pull/7307
2023-02-09Remove unused preprocessor blockPeter Zhu
2023-02-09Merge gc.h and internal/gc.hMatt Valentine-House
[Feature #19425] Notes: Merged: https://github.com/ruby/ruby/pull/7273
2023-02-08Rename iseq_mark_and_update to iseq_mark_and_movePeter Zhu
The new name is more consistent.
2023-02-08Add RUBY_GC_HEAP_INIT_SIZE_%d_SLOTS to pre-init pools granularlyJean Boussier
The old RUBY_GC_HEAP_INIT_SLOTS isn't really usable anymore as it initalize all the pools by the same factor, but it's unlikely that pools will need similar sizes. In production our 40B pool is 5 to 6 times bigger than our 80B pool. Notes: Merged: https://github.com/ruby/ruby/pull/7235
2023-02-07Revert "Revert "Consider DATA objects without a mark function as protected""Jean byroot Boussier
This reverts commit 6eae8e5f514db716e52ad06a2ac97e2cc3910d83. Notes: Merged: https://github.com/ruby/ruby/pull/7263
2023-02-07Revert "Consider DATA objects without a mark function as protected"Jean Boussier
This reverts commit 6e4c242130965de1cf00703c99f8821b0bd19e5b. Notes: Merged: https://github.com/ruby/ruby/pull/7262
2023-02-07Consider DATA objects without a mark function as protectedJean Boussier
It's not uncommon for simple binding to wrap structs without any Ruby object references. Hence with no `mark` function. Might as well mark them as protected by a write barrier. Notes: Merged: https://github.com/ruby/ruby/pull/7255
2023-02-01[Bug #19398] Memory leak in WeakMapPeter Zhu
There's a memory leak in ObjectSpace::WeakMap due to not freeing the `struct weakmap`. It can be seen in the following script: ``` 100.times do 10000.times do ObjectSpace::WeakMap.new end # Output the Resident Set Size (memory usage, in KB) of the current Ruby process puts `ps -o rss= -p #{$$}` end ``` Notes: Merged: https://github.com/ruby/ruby/pull/7223
2023-01-31Copying GC support for EXIVARKunshan Wang
Instance variables held in gen_ivtbl are marked with rb_gc_mark. It prevents the referenced objects from moving, which is bad for copying garbage collectors. This commit allows those instance variables to be updated during gc_update_object_references. Notes: Merged: https://github.com/ruby/ruby/pull/7206
2023-01-19Add rb_gc_mark_and_move and implement on iseqPeter Zhu
This commit adds rb_gc_mark_and_move which takes a pointer to an object and marks it during marking phase and updates references during compaction. This allows for marking and reference updating to be combined into a single function, which reduces code duplication and prevents bugs if marking and reference updating goes out of sync. This commit also implements rb_gc_mark_and_move on iseq as an example. Notes: Merged: https://github.com/ruby/ruby/pull/7140
2023-01-11Move classpath to rb_classext_tPeter Zhu
This commit moves the classpath (and tmp_classpath) from instance variables to the rb_classext_t. This improves performance as we no longer need to set an instance variable when assigning a classpath to a class. I benchmarked with the following script: ```ruby name = :MyClass puts(Benchmark.measure do 10_000_000.times do |i| Object.const_set(name, Class.new) Object.send(:remove_const, name) end end) ``` Before this patch: ``` 5.440119 0.025264 5.465383 ( 5.467105) ``` After this patch: ``` 4.889646 0.028325 4.917971 ( 4.942678) ``` Notes: Merged: https://github.com/ruby/ruby/pull/7096
2023-01-09Fix re-embedding of strings during compactionPeter Zhu
The reference updating code for strings is not re-embedding strings because the code is incorrectly wrapped inside of a `if (STR_SHARED_P(obj))` clause. Shared strings can't be re-embedded so this ends up being a no-op. This means that strings can be moved to a large size pool during compaction, but won't be re-embedded, which would waste the space. Notes: Merged: https://github.com/ruby/ruby/pull/7071
2023-01-04Allow malloc during gc when GC has been disabledPeter Zhu
We should allow malloc during GC when GC has been explicitly disabled since garbage_collect_with_gvl won't do anything if GC has been disabled. Notes: Merged: https://github.com/ruby/ruby/pull/7058
2023-01-03[ci skip] Remove trailing semicolon in gc.cPeter Zhu
2022-12-30Fix integer underflow when using HEAP_INIT_SLOTSPeter Zhu
There is an integer underflow when the environment variable RUBY_GC_HEAP_INIT_SLOTS is less than the number of slots currently in the Ruby heap. [Bug #19284] Notes: Merged: https://github.com/ruby/ruby/pull/7044
2022-12-26Skip insanely memory consuming testsNobuyoshi Nakada
These tests do not only consume hundreds GiB bytes memory, result in `rb_bug` when `RUBY_DEBUG` is enabled.
2022-12-20[DOC] Fix formatting for GC.compactPeter Zhu
2022-12-20[DOC] Escape all usages of GCPeter Zhu
RDoc was making every usage of the word "GC" link to the page for GC (which is the same page).
2022-12-20[DOC] Fix call-seq for GC methodsPeter Zhu
RDoc parses the last arrow in the call-seq as the arrow for the return type. It was getting confused over the arrow in the hash.
2022-12-20[DOC] Fix formatting for GC#latest_compact_infoPeter Zhu
2022-12-20Fix thrashing of major GC when size pool is smallPeter Zhu
If a size pooll is small, then `min_free_slots < heap_init_slots` is true. This means that min_free_slots will be set to heap_init_slots. This causes `swept_slots < min_free_slots` to be true in a later if statement. The if statement could trigger a major GC which could cause major GC thrashing. Notes: Merged: https://github.com/ruby/ruby/pull/6971
2022-12-19Fix misfire of compaction read barrierPeter Zhu
gc_compact_move incorrectly returns false when destination heap is full after sweeping. It returns false even if destination heap is different than source heap (returning false means that the source heap has finished compacting). This causes the source page to get locked, which causes a read barrier fire when we try to compact the source heap again. Notes: Merged: https://github.com/ruby/ruby/pull/6963
2022-12-19Fix buffer overrun when re-embedding objectsPeter Zhu
We eagerly set the new shape of an object when moving an object during compaction. This new shape may have a different capacity than the current original shape capacity. This means that we cannot copy from the original buffer using size of the new capacity. Instead, we should use the ivar count (which is less than or equal to both the new and original capacities). Co-Authored-By: Matt Valentine-House <matt@eightbitraptor.com> Notes: Merged: https://github.com/ruby/ruby/pull/6962
2022-12-17Hard crash when allocating in GC when RUBY_DEBUGPeter Zhu
Not all builds have RGENGC_CHECK_MODE set, so it should also crash when RUBY_DEBUG is set.
2022-12-17Move check for GC to xmalloc and xcallocPeter Zhu
Moves the check earlier to before we actually perform the allocation.
2022-12-16Don't allow allocating memory during GCPeter Zhu
Allocating memory (xmalloc and xrealloc) during GC could cause GC to trigger, which would crash with `[BUG] during_gc != 0`. This is an intermittent bug which could be hard to debug. This commit changes it so that any memory allocation during GC will emit a warning. When debug flags are enabled it will also cause a crash. Notes: Merged: https://github.com/ruby/ruby/pull/6921