Skip to content

Commit c5ab7c9

Browse files
authored
[release-1.10] dont reset maxsize in jl_array_to_string (#55689)
Let's change `jl_array_to_string` so that we make the consequences of calling it in a thread-unsafe way less disastrous (i.e. let's avoid corrupting GC internal metrics). Strictly speaking, this is not a bug-fix because calling `jl_array_to_string` concurrently from two threads is UB in any case. To see how a race here may lead to negative `live_bytes`, consider this MWE from @NHDaly: - 1.10: ```Julia julia> GC.gc(true); Base.gc_live_bytes() 1842370 julia> g_vecs = Any[ UInt8['a' for _ in 1:1000000000] for _ in 1:10 ]; julia> GC.gc(true); Base.gc_live_bytes() 10001774906 julia> Threads.@threads for _ in 1:1000 for v in g_vecs String(v) end end julia> GC.gc(true); Base.gc_live_bytes() -1997600207 ``` - This patch: ```Julia julia> GC.gc(true); Base.gc_live_bytes() 1862440 julia> g_vecs = Any[ UInt8['a' for _ in 1:1000000000] for _ in 1:10 ]; julia> GC.gc(true); Base.gc_live_bytes() 10001796440 julia> Threads.@threads for _ in 1:1000 for v in g_vecs String(v) end end julia> GC.gc(true); Base.gc_live_bytes() 10002390952 ```
1 parent c7f5f2d commit c5ab7c9

File tree

3 files changed

+0
-10
lines changed

3 files changed

+0
-10
lines changed

src/array.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,10 +479,8 @@ JL_DLLEXPORT jl_value_t *jl_array_to_string(jl_array_t *a)
479479
return o;
480480
}
481481
}
482-
jl_gc_count_freed(jl_array_nbytes(a));
483482
a->nrows = 0;
484483
a->length = 0;
485-
a->maxsize = 0;
486484
return jl_pchar_to_string((const char*)jl_array_data(a), len);
487485
}
488486

src/gc.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,13 +1128,6 @@ void jl_gc_count_allocd(size_t sz) JL_NOTSAFEPOINT
11281128
jl_atomic_load_relaxed(&ptls->gc_num.allocd) + sz);
11291129
}
11301130

1131-
void jl_gc_count_freed(size_t sz) JL_NOTSAFEPOINT
1132-
{
1133-
jl_ptls_t ptls = jl_current_task->ptls;
1134-
jl_atomic_store_relaxed(&ptls->gc_num.freed,
1135-
jl_atomic_load_relaxed(&ptls->gc_num.freed) + sz);
1136-
}
1137-
11381131
static void combine_thread_gc_counts(jl_gc_num_t *dest) JL_NOTSAFEPOINT
11391132
{
11401133
int gc_n_threads;

src/julia_internal.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,6 @@ JL_DLLEXPORT int64_t jl_gc_sync_total_bytes(int64_t offset) JL_NOTSAFEPOINT;
573573
void jl_gc_track_malloced_array(jl_ptls_t ptls, jl_array_t *a) JL_NOTSAFEPOINT;
574574
size_t jl_array_nbytes(jl_array_t *a) JL_NOTSAFEPOINT;
575575
void jl_gc_count_allocd(size_t sz) JL_NOTSAFEPOINT;
576-
void jl_gc_count_freed(size_t sz) JL_NOTSAFEPOINT;
577576
void jl_gc_run_all_finalizers(jl_task_t *ct);
578577
void jl_release_task_stack(jl_ptls_t ptls, jl_task_t *task);
579578
void jl_gc_add_finalizer_(jl_ptls_t ptls, void *v, void *f) JL_NOTSAFEPOINT;

0 commit comments

Comments
 (0)