Skip to content

Commit

Permalink
Alloc cache: preserve the cache across the cached macro. (#582)
Browse files Browse the repository at this point in the history
  • Loading branch information
maleadt authored Jan 9, 2025
1 parent b6bf8da commit 78c9ef0
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/host/alloc_cache.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ end
const ALLOC_CACHE = ScopedValue{Union{Nothing, AllocCache}}(nothing)

"""
@cached(cache, expr)
@cached cache expr
Evaluate `expr` using allocations cache `cache`.
Expand Down Expand Up @@ -144,14 +144,17 @@ See [`@uncached`](@ref).
"""
macro cached(cache, expr)
return quote
res = @with $(esc(ALLOC_CACHE)) => $(esc(cache)) $(esc(expr))
free_busy!($(esc(cache)))
res
cache = $(esc(cache))
GC.@preserve cache begin
res = @with $(esc(ALLOC_CACHE)) => cache $(esc(expr))
free_busy!(cache)
res
end
end
end

"""
uncached(expr)
@uncached expr
Evaluate expression `expr` without using the allocation. This is useful to call from within
`@cached` to avoid caching some allocations, e.g., because they can be returned out of the
Expand Down

0 comments on commit 78c9ef0

Please sign in to comment.