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

Specialize adapt_structure for small tuples #81

Merged
merged 2 commits into from
Mar 20, 2024
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
17 changes: 15 additions & 2 deletions src/base.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
# predefined adaptors for working with types from the Julia standard library

adapt_structure(to, xs::Union{Tuple,NamedTuple}) = map(adapt(to), xs)

# Use recursion to avoid inference bail-out in `map`
#adapt_structure(to, xs::Union{Tuple,NamedTuple}) = map(adapt(to), xs)
adapt_structure(to, xs::NamedTuple) = map(adapt(to), xs)
# Specialize on small Tuples
function adapt_structure(to, xs::Tuple)
if length(xs) ≤ 20
_adapt_tuple_structure(to, xs)
else
map(adapt(to), xs)
end
end
_adapt_tuple_structure(to, xs::Tuple) =
(adapt(to, first(xs)), _adapt_tuple_structure(to, Base.tail(xs))...)
_adapt_tuple_structure(to, xs::Tuple{}) = ()
_adapt_tuple_structure(to, xs::Tuple{<:Any}) = (adapt(to, first(xs)), )

## Closures

Expand Down
Loading