Skip to content

Commit f91cd9e

Browse files
Merge pull request #21 from danielwe/lazyfix
Fix issues with LazyBufferCache
2 parents fe0f06d + c08856d commit f91cd9e

File tree

2 files changed

+13
-15
lines changed

2 files changed

+13
-15
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,8 @@ LazyBufferCache(f::F=identity)
185185
```
186186

187187
A `LazyBufferCache` is a `Dict`-like type for the caches which automatically defines
188-
new cache vectors on demand when they are required. The function `f` is a length
189-
map which maps `length_of_cache = f(length(u))`, which by default creates cache
190-
vectors of the same length.
188+
new cache arrays on demand when they are required. The function `f` maps
189+
`size_of_cache = f(size(u))`, which by default creates cache arrays of the same size.
191190

192191
Note that `LazyBufferCache` does cause a dynamic dispatch, though it is type-stable.
193192
This gives it a ~100ns overhead, and thus on very small problems it can reduce

src/PreallocationTools.jl

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,25 +75,24 @@ end
7575
"""
7676
b = LazyBufferCache(f=identity)
7777
78-
A lazily allocated buffer object. Given a vector `u`, `b[u]` returns a `Vector` of the
79-
same element type and length `f(length(u))` (defaulting to the same length), which is
80-
allocated as needed and then cached within `b` for subsequent usage.
78+
A lazily allocated buffer object. Given an array `u`, `b[u]` returns an array of the
79+
same type and size `f(size(u))` (defaulting to the same size), which is allocated as
80+
needed and then cached within `b` for subsequent usage.
8181
8282
"""
8383
struct LazyBufferCache{F<:Function}
8484
bufs::Dict # a dictionary mapping types to buffers
85-
lengthmap::F
86-
LazyBufferCache(f::F=identity) where {F<:Function} = new{F}(Dict()) # start with empty dict
85+
sizemap::F
86+
LazyBufferCache(f::F=identity) where {F<:Function} = new{F}(Dict(), f) # start with empty dict
8787
end
8888

8989
# override the [] method
90-
function Base.getindex(b::LazyBufferCache, u::AbstractArray{T}) where {T}
91-
n = b.lengthmap(size(u)) # required buffer length
92-
buf = get!(b.bufs, T) do
93-
similar(u, T, n) # buffer to allocate if it was not found in b.bufs
94-
end::typeof(u) # declare type since b.bufs dictionary is untyped
95-
# Doesn't work well with matrices, needs more thought!
96-
#return resize!(buf, n) # resize the buffer if needed, e.g. if problem was resized
90+
function Base.getindex(b::LazyBufferCache, u::T) where {T<:AbstractArray}
91+
s = b.sizemap(size(u)) # required buffer size
92+
buf = get!(b.bufs, (T, s)) do
93+
similar(u, s) # buffer to allocate if it was not found in b.bufs
94+
end::T # declare type since b.bufs dictionary is untyped
95+
return buf
9796
end
9897

9998
export dualcache, get_tmp, LazyBufferCache

0 commit comments

Comments
 (0)