@@ -2216,6 +2216,17 @@ rb_gc_impl_size_allocatable_p(size_t size)
22162216}
22172217
22182218static const size_t ALLOCATED_COUNT_STEP = 1024 ;
2219+ static void
2220+ ractor_cache_flush_count (rb_objspace_t * objspace , rb_ractor_newobj_cache_t * cache )
2221+ {
2222+ for (int heap_idx = 0 ; heap_idx < HEAP_COUNT ; heap_idx ++ ) {
2223+ rb_ractor_newobj_heap_cache_t * heap_cache = & cache -> heap_caches [heap_idx ];
2224+
2225+ rb_heap_t * heap = & heaps [heap_idx ];
2226+ RUBY_ATOMIC_SIZE_ADD (heap -> total_allocated_objects , heap_cache -> allocated_objects_count );
2227+ heap_cache -> allocated_objects_count = 0 ;
2228+ }
2229+ }
22192230
22202231static inline VALUE
22212232ractor_cache_allocate_slot (rb_objspace_t * objspace , rb_ractor_newobj_cache_t * cache ,
@@ -2240,19 +2251,11 @@ ractor_cache_allocate_slot(rb_objspace_t *objspace, rb_ractor_newobj_cache_t *ca
22402251 rb_asan_unpoison_object (obj , true);
22412252 heap_cache -> freelist = p -> next ;
22422253
2243- if (rb_gc_multi_ractor_p ()) {
2244- heap_cache -> allocated_objects_count ++ ;
2245- rb_heap_t * heap = & heaps [heap_idx ];
2246- if (heap_cache -> allocated_objects_count >= ALLOCATED_COUNT_STEP ) {
2247- RUBY_ATOMIC_SIZE_ADD (heap -> total_allocated_objects , heap_cache -> allocated_objects_count );
2248- heap_cache -> allocated_objects_count = 0 ;
2249- }
2250- }
2251- else {
2252- rb_heap_t * heap = & heaps [heap_idx ];
2253- heap -> total_allocated_objects ++ ;
2254- GC_ASSERT (heap -> total_slots >=
2255- (heap -> total_allocated_objects - heap -> total_freed_objects - heap -> final_slots_count ));
2254+ heap_cache -> allocated_objects_count ++ ;
2255+ rb_heap_t * heap = & heaps [heap_idx ];
2256+ if (heap_cache -> allocated_objects_count >= ALLOCATED_COUNT_STEP ) {
2257+ RUBY_ATOMIC_SIZE_ADD (heap -> total_allocated_objects , heap_cache -> allocated_objects_count );
2258+ heap_cache -> allocated_objects_count = 0 ;
22562259 }
22572260
22582261#if RGENGC_CHECK_MODE
@@ -5172,6 +5175,8 @@ gc_verify_internal_consistency_(rb_objspace_t *objspace)
51725175
51735176 /* check counters */
51745177
5178+ ractor_cache_flush_count (objspace , rb_gc_get_ractor_newobj_cache ());
5179+
51755180 if (!is_lazy_sweeping (objspace ) &&
51765181 !finalizing &&
51775182 !rb_gc_multi_ractor_p ()) {
@@ -7510,6 +7515,8 @@ rb_gc_impl_stat(void *objspace_ptr, VALUE hash_or_sym)
75107515
75117516 setup_gc_stat_symbols ();
75127517
7518+ ractor_cache_flush_count (objspace , rb_gc_get_ractor_newobj_cache ());
7519+
75137520 if (RB_TYPE_P (hash_or_sym , T_HASH )) {
75147521 hash = hash_or_sym ;
75157522 }
@@ -7662,6 +7669,8 @@ rb_gc_impl_stat_heap(void *objspace_ptr, VALUE heap_name, VALUE hash_or_sym)
76627669{
76637670 rb_objspace_t * objspace = objspace_ptr ;
76647671
7672+ ractor_cache_flush_count (objspace , rb_gc_get_ractor_newobj_cache ());
7673+
76657674 setup_gc_stat_heap_symbols ();
76667675
76677676 if (NIL_P (heap_name )) {
0 commit comments