Skip to content

Commit 78c9ef0

Browse files
authored
Alloc cache: preserve the cache across the cached macro. (#582)
1 parent b6bf8da commit 78c9ef0

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

src/host/alloc_cache.jl

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ end
108108
const ALLOC_CACHE = ScopedValue{Union{Nothing, AllocCache}}(nothing)
109109

110110
"""
111-
@cached(cache, expr)
111+
@cached cache expr
112112
113113
Evaluate `expr` using allocations cache `cache`.
114114
@@ -144,14 +144,17 @@ See [`@uncached`](@ref).
144144
"""
145145
macro cached(cache, expr)
146146
return quote
147-
res = @with $(esc(ALLOC_CACHE)) => $(esc(cache)) $(esc(expr))
148-
free_busy!($(esc(cache)))
149-
res
147+
cache = $(esc(cache))
148+
GC.@preserve cache begin
149+
res = @with $(esc(ALLOC_CACHE)) => cache $(esc(expr))
150+
free_busy!(cache)
151+
res
152+
end
150153
end
151154
end
152155

153156
"""
154-
uncached(expr)
157+
@uncached expr
155158
156159
Evaluate expression `expr` without using the allocation. This is useful to call from within
157160
`@cached` to avoid caching some allocations, e.g., because they can be returned out of the

0 commit comments

Comments
 (0)