Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tell the compiler about the value of the input to f if it is a type (and test) #148

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 28 additions & 23 deletions src/benchmarking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ function _benchmark_2(args1, setup, teardown, gc::Bool, evals::Int, warmup::Bool
rp = ntuple(N) do i
old_gc = gc || GC.enable(false)
sample, ti, args3 = try
_benchmark_3(fs[p[i]], args2, evals, warmup)
_benchmark_3(fs[p[i]], evals, warmup, args2...)
finally
gc || GC.enable(old_gc)
end
Expand All @@ -134,27 +134,32 @@ function _benchmark_2(args1, setup, teardown, gc::Bool, evals::Int, warmup::Bool
end

_div(a, b) = a == b == 0 ? zero(a/b) : a/b
function _benchmark_3(f::F, args::A, evals::Int, warmup::Bool) where {F, A}
cumulative_compile_timing(true)
gcstats0 = Base.gc_num()
ctime, time0, time1, res, gcstats1 = try
ctime = cumulative_compile_time_ns()
time0 = time_ns()
res = @static VERSION >= v"1.8" ? @noinline(f(args...)) : f(args...)
donotdelete(res)
for _ in 2:evals
x = @static VERSION >= v"1.8" ? @noinline(f(args...)) : f(args...)
donotdelete(x)
end
time1 = time_ns()
ctime = cumulative_compile_time_ns() .- ctime
gcstats1 = Base.gc_num()
let body(args) =
quote
gcstats = Base.gc_num()
cumulative_compile_timing(true)
gcstats0 = Base.gc_num()
ctime, time0, time1, res, gcstats1 = try
ctime = cumulative_compile_time_ns()
time0 = time_ns()
res = @static VERSION >= v"1.8" ? @noinline(f($args)) : f($args)
donotdelete(res)
for _ in 2:evals
x = @static VERSION >= v"1.8" ? @noinline(f($args)) : f($args)
donotdelete(x)
end
time1 = time_ns()
ctime = cumulative_compile_time_ns() .- ctime
gcstats1 = Base.gc_num()

ctime, time0, time1, res, gcstats1
finally
cumulative_compile_timing(false)
ctime, time0, time1, res, gcstats1
finally
cumulative_compile_timing(false)
end
rtime = time1 - time0
gcdiff = Base.GC_Diff(gcstats1, gcstats0)
Sample(evals, 1e-9rtime/evals, Base.gc_alloc_count(gcdiff)/evals, gcdiff.allocd/evals, _div(gcdiff.total_time,rtime), _div(ctime[1],rtime), _div(ctime[2],ctime[1]), warmup), time1, res
end
rtime = time1 - time0
gcdiff = Base.GC_Diff(gcstats1, gcstats0)
Sample(evals, 1e-9rtime/evals, Base.gc_alloc_count(gcdiff)/evals, gcdiff.allocd/evals, _div(gcdiff.total_time,rtime), _div(ctime[1],rtime), _div(ctime[2],ctime[1]), warmup), time1, res
end
@eval _benchmark_3(f::F, evals::Int, warmup::Bool, args...) where F = $(body(:(args...)))
@eval _benchmark_3(f::F, evals::Int, warmup::Bool, arg::Type{T}) where {F, T} = $(body(:arg))
end
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,10 @@ else
x = 1
@test isinteger((@b hash(x) seconds=.001).allocs)
end

@testset "Issue #107, specialization" begin
@test (@b Int rand).allocs == 0
end
end

@testset "Statistics Extension" begin
Expand Down
Loading