Skip to content

Commit

Permalink
@inline fixes single-alloc
Browse files Browse the repository at this point in the history
  • Loading branch information
ACEsuit committed May 31, 2024
1 parent ef0a0c5 commit 760ef86
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/WithAlloc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ export whatalloc, @withalloc1, @withalloc

function whatalloc end

function _bumper_alloc(allocinfo::Tuple{Type, Vararg{Int, N}}) where {N}
@inline function _bumper_alloc(allocinfo::Tuple)
(Bumper.alloc!(Bumper.default_buffer(), allocinfo... ), )
end

function _bumper_alloc(allocinfo::NTuple{N, <: Tuple}) where {N}
@inline function _bumper_alloc(allocinfo::NTuple{N, <: Tuple}) where {N}
ntuple(i -> Bumper.alloc!(Bumper.default_buffer(), allocinfo[i]...), N)
end

Expand Down
13 changes: 13 additions & 0 deletions test/_readme.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ function alloctest1(B, C)
end
end

function alloctest2(B, C)
@no_escape begin
# allocinfo = WithAlloc.whatalloc(mymul!, B, C)
# A = ( (@alloc(allocinfo...)), )
# A = WithAlloc._bumper_alloc(allocinfo)
# A = (Bumper.alloc!(Bumper.default_buffer(), allocinfo... ), )
# s3 = sum( mymul!(A..., B, C) )
s3 = sum( @withalloc mymul!(B, C) )
end
end


function alloctest(B, C)
@no_escape begin
s3 = sum( @withalloc mymul!(B, C) )
Expand All @@ -81,3 +93,4 @@ end
B = randn(5,10); C = randn(10, 3)
@btime alloctest($B, $C) # 243.056 ns (2 allocations: 64 bytes)
@btime alloctest1($B, $C) # 106.551 ns (0 allocations: 0 bytes)
@btime alloctest2($B, $C)
26 changes: 26 additions & 0 deletions test/test1.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ end
s3 = sum(A3)
end
@test s3 s1

## allocation test

alloctest(B, C) = (@no_escape begin sum( @withalloc mymul!(B, C) ); end)

nalloc = let
B = randn(5,10)
C = randn(10, 3)
@allocated alloctest(B, C)
end

@test nalloc == 0


##

Expand Down Expand Up @@ -66,3 +79,16 @@ end

@test sb s

## allocation test

alloctest2(B, C, D) =
(@no_escape begin sum(sum.( @withalloc mymul2!(B, C, D) )); end)

nalloc2 = let
B = randn(5,10)
C = randn(10, 3)
D = randn(10, 5)
@allocated alloctest2(B, C, D)
end

@btime alloctest2($B, $C, $D)

0 comments on commit 760ef86

Please sign in to comment.