Skip to content
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
52 changes: 38 additions & 14 deletions base/timing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,35 @@ function gc_bytes()
b[]
end

function allocated(f, args::Vararg{Any,N}) where {N}
b0 = Ref{Int64}(0)
b1 = Ref{Int64}(0)
Base.gc_bytes(b0)
f(args...)
Base.gc_bytes(b1)
return b1[] - b0[]
end
only(methods(allocated)).called = 0xff

function allocations(f, args::Vararg{Any,N}) where {N}
stats = Base.gc_num()
f(args...)
diff = Base.GC_Diff(Base.gc_num(), stats)
return Base.gc_alloc_count(diff)
end
only(methods(allocations)).called = 0xff

function is_simply_call(@nospecialize ex)
Meta.isexpr(ex, :call) || return false
for a in ex.args
a isa QuoteNode && continue
a isa Symbol && continue
Base.is_self_quoting(a) && continue
return false
end
return true
end

"""
@allocated

Expand All @@ -487,15 +516,11 @@ julia> @allocated rand(10^6)
```
"""
macro allocated(ex)
quote
Experimental.@force_compile
local b0 = Ref{Int64}(0)
local b1 = Ref{Int64}(0)
gc_bytes(b0)
$(esc(ex))
gc_bytes(b1)
b1[] - b0[]
if !is_simply_call(ex)
ex = :((() -> $ex)())
end
pushfirst!(ex.args, GlobalRef(Base, :allocated))
return esc(ex)
end

"""
Expand All @@ -516,15 +541,14 @@ julia> @allocations rand(10^6)
This macro was added in Julia 1.9.
"""
macro allocations(ex)
quote
Experimental.@force_compile
local stats = Base.gc_num()
$(esc(ex))
local diff = Base.GC_Diff(Base.gc_num(), stats)
Base.gc_alloc_count(diff)
if !is_simply_call(ex)
ex = :((() -> $ex)())
end
pushfirst!(ex.args, GlobalRef(Base, :allocations))
return esc(ex)
end


"""
@lock_conflicts

Expand Down
2 changes: 1 addition & 1 deletion test/boundscheck_exec.jl
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ if bc_opt == bc_default
m1 === m2
end
no_alias_prove(1)
@test_broken (@allocated no_alias_prove(5)) == 0
@test (@allocated no_alias_prove(5)) == 0
end

end
15 changes: 8 additions & 7 deletions test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ has_fma = Dict(
@test clamp(100, Int8) === Int8(100)
@test clamp(200, Int8) === typemax(Int8)

begin
x = [0.0, 1.0, 2.0, 3.0, 4.0]
let x = [0.0, 1.0, 2.0, 3.0, 4.0]
clamp!(x, 1, 3)
@test x == [1.0, 1.0, 2.0, 3.0, 3.0]
end
Expand All @@ -59,12 +58,14 @@ has_fma = Dict(
@test clamp(typemax(UInt16), Int16) === Int16(32767)

# clamp should not allocate a BigInt for typemax(Int16)
x = big(2) ^ 100
@test (@allocated clamp(x, Int16)) == 0
let x = big(2) ^ 100
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these lets now needed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they aren't with the current macro implementation, but just to make this test a bit more clear, and more stable against the next time we change the macro again 😂

@test (@allocated clamp(x, Int16)) == 0
end

x = clamp(2.0, BigInt)
@test x isa BigInt
@test x == big(2)
let x = clamp(2.0, BigInt)
@test x isa BigInt
@test x == big(2)
end
end
end

Expand Down