Skip to content

Commit

Permalink
Specialize adapt_structure for small tuples
Browse files Browse the repository at this point in the history
  • Loading branch information
charleskawczynski committed Mar 14, 2024
1 parent 9c1b146 commit be4a7fd
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/base.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
# 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)
adapt_structure(to, xs::Tuple) = map(adapt(to), xs)
adapt_structure(to, xs::NTuple{N}) where {N} = map(adapt(to), xs)
# Specialize on small Tuples
for N in 1:10
@eval adapt_structure(to, xs::NTuple{$N}) = _adapt_tuple_structure(to, xs)
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

0 comments on commit be4a7fd

Please sign in to comment.