Skip to content

Commit 6b66aab

Browse files
committed
Add cache resizing
1 parent e23786b commit 6b66aab

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

docs/src/literate/howto/caching.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,7 @@ reconstruct(algo, sinograms);
6868

6969
# Caches support serialization like other `RecoPlans`:
7070
clear!(plan)
71-
toTOML(stdout, plan)
71+
toTOML(stdout, plan)
72+
73+
# Caches can also be resized. You can either set the maxsize property of the RecoPlan or use `resize!` on the `ProcessResultCache`. Resizing a cache affects all algorithms build from the same plan.
74+
setAll!(plan, maxsize, 0)

src/RecoPlans/Cache.jl

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,20 @@ The cache is transparent for properties of the underlying parameter. Cache can b
77
"""
88
mutable struct ProcessResultCache{P} <: AbstractUtilityReconstructionParameters{P}
99
param::P
10-
const maxsize::Int64
11-
cache::LRU{UInt64, Any}
10+
maxsize::Int64
11+
const cache::LRU{UInt64, Any}
1212
function ProcessResultCache(; param::Union{P, AbstractUtilityReconstructionParameters{P}}, maxsize::Int64 = 1, cache::LRU{UInt64, Any} = LRU{UInt64, Any}(maxsize = maxsize)) where P
13+
if maxsize != cache.maxsize
14+
@warn "Incosistent cache size detected. Found maxsize $maxsize and cache size $(cache.maxsize). This can happen when a cache is resized. Cache will use $(cache.maxsize)"
15+
end
16+
return ProcessResultCache(cache; param)
17+
end
18+
function ProcessResultCache(maxsize::Int64; param::Union{P, AbstractUtilityReconstructionParameters{P}}) where P
19+
cache::LRU{UInt64, Any} = LRU{UInt64, Any}(maxsize = maxsize)
20+
return new{P}(param, maxsize, cache)
21+
end
22+
function ProcessResultCache(cache::LRU{UInt64, Any}; param::Union{P, AbstractUtilityReconstructionParameters{P}}) where P
23+
maxsize = cache.maxsize
1324
return new{P}(param, maxsize, cache)
1425
end
1526
end
@@ -45,6 +56,10 @@ function Base.setproperty!(plan::RecoPlan{<:ProcessResultCache}, name::Symbol, v
4556
if in(name, [:param, :cache, :maxsize])
4657
t = type(plan, name)
4758
getfield(plan, :values)[name][] = validvalue(plan, t, value) ? value : convert(t, x)
59+
if name == :maxsize && !ismissing(plan.cache)
60+
@warn "Resizing cache will affect all algorithms constructed from this plan" maxlog = 3
61+
resize!(plan.cache; maxsize = plan.maxsize)
62+
end
4863
else
4964
setproperty!(plan.param, name, value)
5065
end
@@ -104,7 +119,17 @@ end
104119
Empty the cache of the `ProcessResultCache`
105120
"""
106121
Base.empty!(cache::ProcessResultCache) = empty!(cache.cache)
122+
"""
123+
resize!(cache::ProcessResultCache)
107124
125+
Resize the cache. This will affect all algorithms sharing the cache, i.e. all algorithms constructed from the same RecoPlan.
126+
"""
127+
function Base.resize!(cache::ProcessResultCache, n)
128+
@warn "Resizing cache will affect all algorithms sharing the cache. Resizing will not update maxsize in RecoPlan" maxlog = 3
129+
cache.maxsize = n
130+
resize!(cache.cache; maxsize = n)
131+
return cache
132+
end
108133
"""
109134
hash(parameter::AbstractImageReconstructionParameters, h)
110135

0 commit comments

Comments
 (0)