@@ -388,24 +388,18 @@ static void sweep_weak_refs(void)
388388 jl_ptls_t ptls2 = gc_all_tls_states [i ];
389389 if (ptls2 != NULL ) {
390390 size_t n = 0 ;
391- size_t ndel = 0 ;
391+ size_t i = 0 ;
392392 size_t l = ptls2 -> gc_tls_common .heap .weak_refs .len ;
393393 void * * lst = ptls2 -> gc_tls_common .heap .weak_refs .items ;
394- if ( l == 0 )
395- continue ;
396- while ( 1 ) {
397- jl_weakref_t * wr = ( jl_weakref_t * ) lst [ n ];
398- if ( gc_marked ( jl_astaggedvalue ( wr ) -> bits . gc ))
394+ // filter with preserving order
395+ for ( i = 0 ; i < l ; i ++ ) {
396+ jl_weakref_t * wr = ( jl_weakref_t * ) lst [ i ];
397+ if ( gc_marked ( jl_astaggedvalue ( wr ) -> bits . gc )) {
398+ lst [ n ] = wr ;
399399 n ++ ;
400- else
401- ndel ++ ;
402- if (n >= l - ndel )
403- break ;
404- void * tmp = lst [n ];
405- lst [n ] = lst [n + ndel ];
406- lst [n + ndel ] = tmp ;
400+ }
407401 }
408- ptls2 -> gc_tls_common .heap .weak_refs .len -= ndel ;
402+ ptls2 -> gc_tls_common .heap .weak_refs .len = n ;
409403 }
410404 }
411405}
@@ -629,10 +623,9 @@ void jl_gc_reset_alloc_count(void) JL_NOTSAFEPOINT
629623 reset_thread_gc_counts ();
630624}
631625
632- static void jl_gc_free_memory (jl_value_t * v , int isaligned ) JL_NOTSAFEPOINT
626+ static void jl_gc_free_memory (jl_genericmemory_t * m , int isaligned ) JL_NOTSAFEPOINT
633627{
634- assert (jl_is_genericmemory (v ));
635- jl_genericmemory_t * m = (jl_genericmemory_t * )v ;
628+ assert (jl_is_genericmemory (m ));
636629 assert (jl_genericmemory_how (m ) == 1 || jl_genericmemory_how (m ) == 2 );
637630 char * d = (char * )m -> ptr ;
638631 size_t freed_bytes = memory_block_usable_size (d , isaligned );
@@ -654,25 +647,23 @@ static void sweep_malloced_memory(void) JL_NOTSAFEPOINT
654647 for (int t_i = 0 ; t_i < gc_n_threads ; t_i ++ ) {
655648 jl_ptls_t ptls2 = gc_all_tls_states [t_i ];
656649 if (ptls2 != NULL ) {
657- mallocmemory_t * ma = ptls2 -> gc_tls_common . heap . mallocarrays ;
658- mallocmemory_t * * pma = & ptls2 -> gc_tls_common .heap .mallocarrays ;
659- while ( ma != NULL ) {
660- mallocmemory_t * nxt = ma -> next ;
661- jl_value_t * a = ( jl_value_t * )(( uintptr_t ) ma -> a & ~ 1 );
662- int bits = jl_astaggedvalue ( a ) -> bits . gc ;
663- if (gc_marked (bits )) {
664- pma = & ma -> next ;
650+ size_t n = 0 ;
651+ size_t l = ptls2 -> gc_tls_common .heap .mallocarrays . len ;
652+ void * * lst = ptls2 -> gc_tls_common . heap . mallocarrays . items ;
653+ // filter without preserving order
654+ while ( n < l ) {
655+ jl_genericmemory_t * m = ( jl_genericmemory_t * )(( uintptr_t ) lst [ n ] & ~ 1 ) ;
656+ if (gc_marked (jl_astaggedvalue ( m ) -> bits . gc )) {
657+ n ++ ;
665658 }
666659 else {
667- * pma = nxt ;
668- int isaligned = (uintptr_t )ma -> a & 1 ;
669- jl_gc_free_memory (a , isaligned );
670- ma -> next = ptls2 -> gc_tls_common .heap .mafreelist ;
671- ptls2 -> gc_tls_common .heap .mafreelist = ma ;
660+ int isaligned = (uintptr_t )lst [n ] & 1 ;
661+ jl_gc_free_memory (m , isaligned );
662+ l -- ;
663+ lst [n ] = lst [l ];
672664 }
673- gc_time_count_mallocd_memory (bits );
674- ma = nxt ;
675665 }
666+ ptls2 -> gc_tls_common .heap .mallocarrays .len = l ;
676667 }
677668 }
678669 gc_time_mallocd_memory_end ();
@@ -3439,8 +3430,7 @@ void jl_init_thread_heap(jl_ptls_t ptls)
34393430 small_arraylist_new (& common_heap -> live_tasks , 0 );
34403431 for (int i = 0 ; i < JL_N_STACK_POOLS ; i ++ )
34413432 small_arraylist_new (& common_heap -> free_stacks [i ], 0 );
3442- common_heap -> mallocarrays = NULL ;
3443- common_heap -> mafreelist = NULL ;
3433+ small_arraylist_new (& common_heap -> mallocarrays , 0 );
34443434 heap -> young_generation_of_bigvals = (bigval_t * )calloc_s (sizeof (bigval_t )); // sentinel
34453435 assert (gc_bigval_sentinel_tag != 0 ); // make sure the sentinel is initialized
34463436 heap -> young_generation_of_bigvals -> header = gc_bigval_sentinel_tag ;
0 commit comments