summaryrefslogtreecommitdiff
path: root/gc.c
AgeCommit message (Collapse)Author
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
2022-12-15Refactor to only attempt to move movable objectsPeter Zhu
Moves check for gc_is_moveable_obj from try_move to gc_compact_plane. Co-Authored-By: Matt Valentine-House <matt@eightbitraptor.com> Notes: Merged: https://github.com/ruby/ruby/pull/6938
2022-12-15Fix Object Movement allocation in GCMatt Valentine-House
When moving Objects between size pools we have to assign a new shape. This happened during updating references - we tried to create a new shape tree that mirrored the existing tree, but based on the root shape of the new size pool. This causes allocations to happen if the new tree doesn't already exist, potentially triggering a GC, during GC. This commit changes object movement to look for a pre-existing new tree during object movement, and if that tree does not exist, we don't move the object to the new pool. This allows us to remove the shape allocation from update references. Co-Authored-By: Peter Zhu <peter@peterzhu.ca> Notes: Merged: https://github.com/ruby/ruby/pull/6938
2022-12-15Transition complex objects to "too complex" shapeJemma Issroff
When an object becomes "too complex" (in other words it has too many variations in the shape tree), we transition it to use a "too complex" shape and use a hash for storing instance variables. Without this patch, there were rare cases where shape tree growth could "explode" and cause performance degradation on what would otherwise have been cached fast paths. This patch puts a limit on shape tree growth, and gracefully degrades in the rare case where there could be a factorial growth in the shape tree. For example: ```ruby class NG; end HUGE_NUMBER.times do NG.new.instance_variable_set(:"@unique_ivar_#{_1}", 1) end ``` We consider objects to be "too complex" when the object's class has more than SHAPE_MAX_VARIATIONS (currently 8) leaf nodes in the shape tree and the object introduces a new variation (a new leaf node) associated with that class. For example, new variations on instances of the following class would be considered "too complex" because those instances create more than 8 leaves in the shape tree: ```ruby class Foo; end 9.times { Foo.new.instance_variable_set(":@uniq_#{_1}", 1) } ``` However, the following class is *not* too complex because it only has one leaf in the shape tree: ```ruby class Foo def initialize @a = @b = @c = @d = @e = @f = @g = @h = @i = nil end end 9.times { Foo.new } `` This case is rare, so we don't expect this change to impact performance of most applications, but it needs to be handled. Co-Authored-By: Aaron Patterson <tenderlove@ruby-lang.org> Notes: Merged: https://github.com/ruby/ruby/pull/6931
2022-12-15Revert "Fix Object Movement allocation in GC"Peter Zhu
This reverts commit 9c54466e299aa91af225bc2d92a3d7755730948f. We're seeing crashes in Shopify CI after this commit.
2022-12-15Fix Object Movement allocation in GCMatt Valentine-House
When moving Objects between size pools we have to assign a new shape. This happened during updating references - we tried to create a new shape tree that mirrored the existing tree, but based on the root shape of the new size pool. This causes allocations to happen if the new tree doesn't already exist, potentially triggering a GC, during GC. This commit changes object movement to look for a pre-existing new tree during object movement, and if that tree does not exist, we don't move the object to the new pool. This allows us to remove the shape allocation from update references. Co-Authored-By: Peter Zhu <peter@peterzhu.ca> Notes: Merged: https://github.com/ruby/ruby/pull/6926
2022-12-13fix indentation: gc_compact_destination_poolMatt Valentine-House
[ci skip] Co-Authored-By: Peter Zhu <peter@peterzhu.ca> Notes: Merged: https://github.com/ruby/ruby/pull/6918
2022-12-12[DOC] Don't document private methods in objspacePeter Zhu
2022-12-10Expose need_major_gc via GC.latest_gc_info (#6791)Mirek Klimos
Notes: Merged-By: peterzhu2118 <peter@peterzhu.ca>
2022-12-09Remove unused counter for heap_page->pinned_slotsMatt Valentine-House
Notes: Merged: https://github.com/ruby/ruby/pull/6881
2022-11-22Increment max_iv_count on class based on number of set_iv in initialize (#6788)Jemma Issroff
We can loosely predict the number of ivar sets on a class based on the number of iv set instructions in the initialize method. This should give us a more accurate estimate to use for initial size pool allocation, which should in turn give us more cache hits. Notes: Merged-By: maximecb <maximecb@ruby-lang.org>
2022-11-21Add RVALUE_OVERHEAD and move ractor_belonging_idPeter Zhu
This commit adds RVALUE_OVERHEAD for storing metadata at the end of the slot. This commit moves the ractor_belonging_id in debug builds from the flags to RVALUE_OVERHEAD which frees the 16 bits in the headers for object shapes. Notes: Merged: https://github.com/ruby/ruby/pull/6763
2022-11-18Differentiate T_OBJECT shapes from other objectsAaron Patterson
We would like to differentiate types of objects via their shape. This commit adds a special T_OBJECT shape when we allocate an instance of T_OBJECT. This allows us to avoid testing whether an object is an instance of a T_OBJECT or not, we can just check the shape. Notes: Merged: https://github.com/ruby/ruby/pull/6758
2022-11-16Using UNDEF_P macroS-H-GAMELINKS
Notes: Merged: https://github.com/ruby/ruby/pull/6721
2022-11-10Remove numiv from RObjectJemma Issroff
Since object shapes store the capacity of an object, we no longer need the numiv field on RObjects. This gives us one extra slot which we can use to give embedded objects one more instance variable (for a total of 3 ivs). This commit removes the concept of numiv from RObject. Notes: Merged: https://github.com/ruby/ruby/pull/6699
2022-11-10Transition shape when object's capacity changesJemma Issroff
This commit adds a `capacity` field to shapes, and adds shape transitions whenever an object's capacity changes. Objects which are allocated out of a bigger size pool will also make a transition from the root shape to the shape with the correct capacity for their size pool when they are allocated. This commit will allow us to remove numiv from objects completely, and will also mean we can guarantee that if two objects share shapes, their IVs are in the same positions (an embedded and extended object cannot share shapes). This will enable us to implement ivar sets in YJIT using object shapes. Co-Authored-By: Aaron Patterson <tenderlove@ruby-lang.org> Notes: Merged: https://github.com/ruby/ruby/pull/6699
2022-11-06[wasm] Scan machine stack based on `ec->machine.stack_{start,end}`Yuta Saito
fiber machine stack is placed outside of C stack allocated by wasm-ld, so highest stack address recorded by `rb_wasm_record_stack_base` is invalid when running on non-main fiber. Therefore, we should scan `stack_{start,end}` which always point a valid stack range in any context. Notes: Merged: https://github.com/ruby/ruby/pull/6679
2022-11-04Increment max_iv_count on class in gc marking, not gc freeingJemma Issroff
We were previously incrementing the max_iv_count on a class in gc freeing. By the time we free an object though, we're not guaranteed its class is still valid. Instead, we can do this when marking and we're guaranteed the object still knows its class. Notes: Merged: https://github.com/ruby/ruby/pull/6673
2022-10-31Implement object shapes for T_CLASS and T_MODULE (#6637)John Hawthorn
* Avoid RCLASS_IV_TBL in marshal.c * Avoid RCLASS_IV_TBL for class names * Avoid RCLASS_IV_TBL for autoload * Avoid RCLASS_IV_TBL for class variables * Avoid copying RCLASS_IV_TBL onto ICLASSes * Use object shapes for Class and Module IVs Notes: Merged-By: jhawthorn <john@hawthorn.email>
2022-10-28fix ASAN error in GCAaron Patterson
2022-10-21Rename `iv_count` on shapes to `next_iv_index`Jemma Issroff
`iv_count` is a misleading name because when IVs are unset, the new shape doesn't decrement this value. `next_iv_count` is an accurate, and more descriptive name. Notes: Merged: https://github.com/ruby/ruby/pull/6608
2022-10-21Remove unused class serialJemma Issroff
Before object shapes, we were using class serial to invalidate inline caches. Now that we use shape_id for inline cache keys, the class serial is unnecessary. Co-Authored-By: Aaron Patterson <tenderlove@ruby-lang.org> Notes: Merged: https://github.com/ruby/ruby/pull/6605
2022-10-21Check writebarrier arguments only when RGENGC_CHECK_MODE [ci skip]Nobuyoshi Nakada
The commit 575ae50d16a03ed23357ec4ea0dbf7167fc26c8c was for debugging the failure triggered by f55212bce939f736559709a8cd16c409772389c8, and it was fixed at the commit 39f7eddec4c55711d56f05b085992a83bf23159e.
2022-10-20Check writebarrier argumentsNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/6600
2022-10-19Stop zeroing memory on allocation / copyAaron Patterson
Shapes gives us an almost exact count of instance variables on an object. Since we know the number of instance variables that have been set, we will never access slots that haven't been initialized with an IV. Notes: Merged: https://github.com/ruby/ruby/pull/6585
2022-10-19Fix and improve coroutines for Darwin (macOS) ppc/ppc64. (#5975)Sergey Fedorov
Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2022-10-15More precisely iterate over Object instance variablesAaron Patterson
Shapes provides us with an (almost) exact count of instance variables. We only need to check for Qundef when an IV has been "undefined" Prefer to use ROBJECT_IV_COUNT when iterating IVs Notes: Merged: https://github.com/ruby/ruby/pull/6555
2022-10-14Use `roomof` macro for rounding up divisionsNobuyoshi Nakada
2022-10-11Revert "Revert "This commit implements the Object Shapes technique in CRuby.""Jemma Issroff
This reverts commit 9a6803c90b817f70389cae10d60b50ad752da48f.
2022-10-07Add IO#timeout attribute and use it for blocking IO operations. (#5653)Samuel Williams
Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2022-10-04[Bug #19028] Suppress GCC 12 `-Wuse-after-free` false warningNobuyoshi Nakada
GCC 12 introduced a new warning flag `-Wuse-after-free`, however it has a false positive at `realloc` when optimization is disabled, since the memory requested for reallocation is guaranteed to not be touched. This workaround is very unclear why the false warning is suppressed by a statement-expression GCC extension. Notes: Merged: https://github.com/ruby/ruby/pull/6487
2022-09-30Revert "This commit implements the Object Shapes technique in CRuby."Aaron Patterson
This reverts commit 68bc9e2e97d12f80df0d113e284864e225f771c2.