summaryrefslogtreecommitdiff
path: root/gc.c
Commit message (Collapse)AuthorAgeFilesLines
* Fix spelling (#7389)John Bampton2023-02-271-1/+1
|
* [ci skip] Add note in gc.c about ambiguous casePeter Zhu2023-02-241-5/+18
|
* Fix incorrect line numbers in GC hookPeter Zhu2023-02-241-2/+19
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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]
* Fix a warning on typedefTakashi Kokubun2023-02-231-2/+2
| | | | | | ../gc.c:13317:1: warning: ‘typedef’ is not at beginning of declaration [-Wold-style-declaration] 13317 | } typedef weakkeymap_entry_t; | ^
* Implement ObjectSpace::WeakKeyMap basic allocatorJean Boussier2023-02-231-0/+325
| | | | [Feature #18498]
* * remove trailing spaces. [ci skip]git2023-02-221-1/+1
|
* Make GC faster when RGENGC_CHECK_MODE >= 2Peter Zhu2023-02-221-8/+4
| | | | | | 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.
* Add marking and sweeping time to GC.statPeter Zhu2023-02-211-49/+91
| | | | | | | | | | | | 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]
* Refactor to separate marking and sweeping phasesPeter Zhu2023-02-211-47/+49
| | | | | This commit separates the marking and sweeping phases so that marking functions do not directly call sweeping functions.
* Remove USE_RGENGC_LOGGING_WB_UNPROTECTMatt Valentine-House2023-02-171-43/+0
| | | | | | | | | | 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>
* Fix compilation error when USE_RINCGC=0Nobuyoshi Nakada2023-02-161-2/+2
|
* Move `attached_object` into `rb_classext_struct`Jean Boussier2023-02-161-0/+8
| | | | | | 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.
* Check !RCLASS_EXT_EMBEDDED instead of SIZE_POOL_COUNT == 1Jean Boussier2023-02-151-2/+2
| | | | It's much more self documenting and consistent
* Remove unused preprocessor blockPeter Zhu2023-02-091-3/+0
|
* Merge gc.h and internal/gc.hMatt Valentine-House2023-02-091-1/+0
| | | | [Feature #19425]
* Rename iseq_mark_and_update to iseq_mark_and_movePeter Zhu2023-02-081-3/+3
| | | | The new name is more consistent.
* Add RUBY_GC_HEAP_INIT_SIZE_%d_SLOTS to pre-init pools granularlyJean Boussier2023-02-081-4/+20
| | | | | | | | 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.
* Revert "Revert "Consider DATA objects without a mark function as protected""Jean byroot Boussier2023-02-071-2/+3
| | | | This reverts commit 6eae8e5f514db716e52ad06a2ac97e2cc3910d83.
* Revert "Consider DATA objects without a mark function as protected"Jean Boussier2023-02-071-3/+2
| | | | This reverts commit 6e4c242130965de1cf00703c99f8821b0bd19e5b.
* Consider DATA objects without a mark function as protectedJean Boussier2023-02-071-2/+3
| | | | | | | 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.
* [Bug #19398] Memory leak in WeakMapPeter Zhu2023-02-011-0/+1
| | | | | | | | | | | | | | | | 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 ```
* Copying GC support for EXIVARKunshan Wang2023-01-311-1/+5
| | | | | | | | | 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.
* Add rb_gc_mark_and_move and implement on iseqPeter Zhu2023-01-191-4/+25
| | | | | | | | | | 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.
* Move classpath to rb_classext_tPeter Zhu2023-01-111-0/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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) ```
* Fix re-embedding of strings during compactionPeter Zhu2023-01-091-7/+9
| | | | | | | | | 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.
* Allow malloc during gc when GC has been disabledPeter Zhu2023-01-041-3/+1
| | | | | 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.
* [ci skip] Remove trailing semicolon in gc.cPeter Zhu2023-01-031-1/+1
|
* Fix integer underflow when using HEAP_INIT_SLOTSPeter Zhu2022-12-301-12/+13
| | | | | | | | 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]
* Skip insanely memory consuming testsNobuyoshi Nakada2022-12-261-0/+3
| | | | | These tests do not only consume hundreds GiB bytes memory, result in `rb_bug` when `RUBY_DEBUG` is enabled.
* [DOC] Fix formatting for GC.compactPeter Zhu2022-12-201-1/+1
|
* [DOC] Escape all usages of GCPeter Zhu2022-12-201-7/+7
| | | | | RDoc was making every usage of the word "GC" link to the page for GC (which is the same page).
* [DOC] Fix call-seq for GC methodsPeter Zhu2022-12-201-1/+1
| | | | | 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.
* [DOC] Fix formatting for GC#latest_compact_infoPeter Zhu2022-12-201-1/+1
|
* Fix thrashing of major GC when size pool is smallPeter Zhu2022-12-201-6/+12
| | | | | | | | 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.
* Fix misfire of compaction read barrierPeter Zhu2022-12-191-1/+1
| | | | | | | | 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.
* Fix buffer overrun when re-embedding objectsPeter Zhu2022-12-191-4/+2
| | | | | | | | | | | 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>
* Hard crash when allocating in GC when RUBY_DEBUGPeter Zhu2022-12-171-3/+3
| | | | | Not all builds have RGENGC_CHECK_MODE set, so it should also crash when RUBY_DEBUG is set.
* Move check for GC to xmalloc and xcallocPeter Zhu2022-12-171-7/+14
| | | | Moves the check earlier to before we actually perform the allocation.
* Don't allow allocating memory during GCPeter Zhu2022-12-161-12/+48
| | | | | | | | | 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.
* Refactor to only attempt to move movable objectsPeter Zhu2022-12-151-30/+33
| | | | | | Moves check for gc_is_moveable_obj from try_move to gc_compact_plane. Co-Authored-By: Matt Valentine-House <matt@eightbitraptor.com>
* Fix Object Movement allocation in GCMatt Valentine-House2022-12-151-5/+36
| | | | | | | | | | | | | | | | | | | 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>
* Transition complex objects to "too complex" shapeJemma Issroff2022-12-151-14/+38
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | 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>
* Revert "Fix Object Movement allocation in GC"Peter Zhu2022-12-151-36/+5
| | | | | | This reverts commit 9c54466e299aa91af225bc2d92a3d7755730948f. We're seeing crashes in Shopify CI after this commit.
* Fix Object Movement allocation in GCMatt Valentine-House2022-12-151-5/+36
| | | | | | | | | | | | | | | | | | | 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>
* fix indentation: gc_compact_destination_poolMatt Valentine-House2022-12-131-11/+11
| | | | | | [ci skip] Co-Authored-By: Peter Zhu <peter@peterzhu.ca>
* [DOC] Don't document private methods in objspacePeter Zhu2022-12-121-0/+1
|
* Expose need_major_gc via GC.latest_gc_info (#6791)Mirek Klimos2022-12-101-2/+17
|
* Remove unused counter for heap_page->pinned_slotsMatt Valentine-House2022-12-091-1/+0
|
* Increment max_iv_count on class based on number of set_iv in initialize (#6788)Jemma Issroff2022-11-221-1/+1
| | | | | | 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.
* Add RVALUE_OVERHEAD and move ractor_belonging_idPeter Zhu2022-11-211-14/+39
| | | | | | | 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.