Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alloc cache: preserve the cache across the cached macro. #582

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading