Skip to content

Commit

Permalink
add frozen function (#8)
Browse files Browse the repository at this point in the history
* add frozen function

* fix method

* add test & pretty print

* fix test

* fix type stablility problem

* oops

* this might be better?

* Update src/replaying.jl

Co-Authored-By: Lyndon White <oxinabox@ucc.asn.au>

* rename -> preallocate

* bump version

* fix test

* export preallocation

Co-authored-by: Lyndon White <oxinabox@ucc.asn.au>
  • Loading branch information
Roger-luo and oxinabox committed Mar 9, 2020
1 parent 7c871e4 commit 836e373
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
@@ -1,7 +1,7 @@
name = "AutoPreallocation"
uuid = "e7028de2-df94-4053-9fdc-99272086b8d4"
authors = ["Lyndon White"]
version = "0.1.1"
version = "0.2.0"

[deps]
Cassette = "7057c7e9-c182-5462-911a-8362d720325c"
Expand Down
2 changes: 1 addition & 1 deletion src/AutoPreallocation.jl
Expand Up @@ -2,7 +2,7 @@ module AutoPreallocation
using Cassette
using LinearAlgebra: LinearAlgebra

export avoid_allocations, record_allocations, freeze, reinitialize!, @no_prealloc
export avoid_allocations, record_allocations, freeze, preallocate, reinitialize!, @no_prealloc

include("record_types.jl")
include("recording.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/record_types.jl
Expand Up @@ -21,7 +21,7 @@ Freezes an allocation `record`, to not allow new allocations to be added.
This converts the memory of the what happenned to be stored as a `Tuple`.
This could give better performance during replay; or it could give worse.
"""
freeze(record) = FrozenAllocationRecord(record)
freeze(record::AbstractAllocationRecord) = FrozenAllocationRecord(record)

"""
reinitialize!(record)
Expand Down
33 changes: 33 additions & 0 deletions src/replaying.jl
Expand Up @@ -44,3 +44,36 @@ function avoid_allocations(record, f, args...; kwargs...)
ctx = new_replay_ctx(record)
return Cassette.overdub(ctx, f, args...; kwargs...)
end

struct PreallocatedFunction{F}
f::F
ctx::Dict{Tuple, ReplayCtx} # maps from argument types to the ReplayCtx
PreallocatedFunction(f) = new{typeof(f)}(f, Dict{Tuple, ReplayCtx}())
end

@generated function (f::PreallocatedFunction)(xs...)
return quote
if haskey(f.ctx, $xs)
ctx = f.ctx[$xs]
# step = ctx.metadata.step::Ref{Int}
ctx.metadata.step[] = 1
return Cassette.overdub(ctx, f.f, xs...)
else
x, record = record_allocations(f.f, xs...)
ctx = AutoPreallocation.new_replay_ctx(record)
f.ctx[$xs] = ctx
return x
end
end
end

"""
preallocate(f)
Preallocate a function. This will preallocate the allocation behaviour of the function by creating
a [`PreallocatedFunction`](@ref). This function will record all the allocations at the first run,
then in the following run, it will not allocate anymore.
"""
preallocate(f) = PreallocatedFunction(f)

Base.show(io::IO, f::PreallocatedFunction) = print(io, "preallocate(", f.f, ")")
3 changes: 3 additions & 0 deletions test/integration_tests.jl
Expand Up @@ -19,6 +19,9 @@ end
val, record = record_allocations(f_matmul)
# NOTE: (@Roger-luo) not sure why this is 256 on my machine
@test (@ballocated avoid_allocations($record, f_matmul)) <= 352

f = preallocate(f_matmul)
@test (@ballocated $f()) <= 352
end

@testset "noprealloc example" begin
Expand Down

2 comments on commit 836e373

@Roger-luo
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/10751

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if Julia TagBot is installed, or can be done manually through the github interface, or via:

git tag -a v0.2.0 -m "<description of version>" 836e373377034c326513faeac04f34788ef63b75
git push origin v0.2.0

Please sign in to comment.