Skip to content

Commit 20791bc

Browse files
Merge pull request #111 from SouthEndMusic/LBC_initializer
Add optional `initializer!` function to `LazyBufferCache`
2 parents 36263af + f89d012 commit 20791bc

File tree

5 files changed

+27
-4
lines changed

5 files changed

+27
-4
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ A `LazyBufferCache` is a `Dict`-like type for the caches which automatically def
218218
new cache arrays on demand when they are required. The function `f` maps
219219
`size_of_cache = f(size(u))`, which by default creates cache arrays of the same size.
220220

221+
By default the created buffers are not initialized, but a function `initializer!`
222+
can be supplied which is applied to the buffer when it is created, for instance `buf -> fill!(buf, 0.0)`.
223+
221224
Note that `LazyBufferCache` is type-stable and contains no dynamic dispatch. This gives
222225
it a ~15ns overhead. The upside of `LazyBufferCache` is that the user does not have to
223226
worry about potential issues with chunk sizes and such: `LazyBufferCache` is much easier!

docs/src/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,9 @@ A `LazyBufferCache` is a `Dict`-like type for the caches, which automatically de
208208
new cache arrays on demand when they are required. The function `f` maps
209209
`size_of_cache = f(size(u))`, which by default creates cache arrays of the same size.
210210

211+
By default the created buffers are not initialized, but a function `initializer!`
212+
can be supplied which is applied to the buffer when it is created, for instance `buf -> fill!(buf, 0.0)`.
213+
211214
Note that `LazyBufferCache` is type-stable and contains no dynamic dispatch. This gives
212215
it a ~15ns overhead. The upside of `LazyBufferCache` is that the user does not have to
213216
worry about potential issues with chunk sizes and such: `LazyBufferCache` is much easier!

src/PreallocationTools.jl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -197,19 +197,27 @@ end
197197
# LazyBufferCache
198198

199199
"""
200-
b = LazyBufferCache(f=identity)
200+
b = LazyBufferCache(f = identity; initializer! = identity)
201201
202202
A lazily allocated buffer object. Given an array `u`, `b[u]` returns an array of the
203203
same type and size `f(size(u))` (defaulting to the same size), which is allocated as
204204
needed and then cached within `b` for subsequent usage.
205205
206+
By default the created buffers are not initialized, but a function `initializer!`
207+
can be supplied which is applied to the buffer when it is created, for instance `buf -> fill!(buf, 0.0)`.
208+
206209
Optionally, the size can be explicitly given at calltime using `b[u,s]`, which will
207210
return a cache of size `s`.
208211
"""
209-
struct LazyBufferCache{F <: Function}
212+
struct LazyBufferCache{F <: Function, I <: Function}
210213
bufs::Dict{Any, Any} # a dictionary mapping (type, size) pairs to buffers
211214
sizemap::F
212-
LazyBufferCache(f::F = identity) where {F <: Function} = new{F}(Dict(), f) # start with empty dict
215+
initializer!::I
216+
function LazyBufferCache(
217+
f::F = identity; initializer!::I = identity) where {
218+
F <: Function, I <: Function}
219+
new{F, I}(Dict(), f, initializer!)
220+
end # start with empty dict
213221
end
214222

215223
similar_type(x::AbstractArray, s::Integer) = similar_type(x, (s,))
@@ -222,7 +230,9 @@ end
222230
function get_tmp(
223231
b::LazyBufferCache, u::T, s = b.sizemap(size(u))) where {T <: AbstractArray}
224232
get!(b.bufs, (T, s)) do
225-
similar(u, s) # buffer to allocate if it was not found in b.bufs
233+
buffer = similar(u, s) # buffer to allocate if it was not found in b.bufs
234+
b.initializer!(buffer)
235+
buffer
226236
end::similar_type(u, s) # declare type since b.bufs dictionary is untyped
227237
end
228238

test/lbc.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
using PreallocationTools: LazyBufferCache
2+
using Test
3+
4+
b = LazyBufferCache(Returns(10); initializer! = buf -> fill!(buf, 0))
5+
6+
@test b[Float64[]] == zeros(10)

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ if GROUP == "All" || GROUP == "Core"
1616
@safetestset "DiffCache Resizing" include("core_resizing.jl")
1717
@safetestset "DiffCache Nested Duals" include("core_nesteddual.jl")
1818
@safetestset "DiffCache Sparsity Support" include("sparsity_support.jl")
19+
@safetestset "LazyBufferCache" include("lbc.jl")
1920
@safetestset "GeneralLazyBufferCache" include("general_lbc.jl")
2021
end
2122

0 commit comments

Comments
 (0)