Skip to content

Commit 825afb8

Browse files
committed
timing + benchmark
1 parent 662da02 commit 825afb8

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

gcbench.jl

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using PythonCall, Test
2+
3+
# https://github.com/JuliaCI/GCBenchmarks/blob/fc288c696381ebfdef8f002168addd0ec1b08e34/benches/serial/append/append.jl
4+
macro gctime(ex)
5+
quote
6+
local prior = PythonCall.GC.SECONDS_SPENT_IN_GC[]
7+
local ret = @timed $(esc(ex))
8+
Base.time_print(stdout, ret.time * 1e9, ret.gcstats.allocd, ret.gcstats.total_time, Base.gc_alloc_count(ret.gcstats); msg="Runtime")
9+
local after = PythonCall.GC.SECONDS_SPENT_IN_GC[]
10+
println(stdout)
11+
Base.time_print(stdout, (after - prior) * 1e9; msg="Python GC time")
12+
println(stdout)
13+
ret.value
14+
end
15+
end
16+
17+
function append_lots(iters=100 * 1024, size=1596)
18+
v = pylist()
19+
for i = 1:iters
20+
v.append(pylist(rand(size)))
21+
end
22+
return v
23+
end
24+
25+
@time "Total" begin
26+
@gctime append_lots()
27+
@time "Next full GC" begin
28+
GC.gc(true)
29+
end
30+
end

src/GC/GC.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ using ..C: C
1212
const ENABLED = Ref(true)
1313
const QUEUE = C.PyPtr[]
1414

15+
# This is used for basic profiling
16+
const SECONDS_SPENT_IN_GC = Threads.Atomic{Float64}()
17+
18+
1519
"""
1620
PythonCall.GC.disable()
1721
@@ -55,9 +59,10 @@ end
5559
function enqueue(ptr::C.PyPtr)
5660
if ptr != C.PyNULL && C.CTX.is_initialized
5761
if ENABLED[]
58-
C.with_gil(false) do
62+
t = @elapsed C.with_gil(false) do
5963
C.Py_DecRef(ptr)
6064
end
65+
Threads.atomic_add!(SECONDS_SPENT_IN_GC, t)
6166
else
6267
push!(QUEUE, ptr)
6368
end
@@ -68,13 +73,14 @@ end
6873
function enqueue_all(ptrs)
6974
if C.CTX.is_initialized
7075
if ENABLED[]
71-
C.with_gil(false) do
76+
t = @elapsed C.with_gil(false) do
7277
for ptr in ptrs
7378
if ptr != C.PyNULL
7479
C.Py_DecRef(ptr)
7580
end
7681
end
7782
end
83+
Threads.atomic_add!(SECONDS_SPENT_IN_GC, t)
7884
else
7985
append!(QUEUE, ptrs)
8086
end

0 commit comments

Comments
 (0)