Skip to content

Commit

Permalink
Re-land: Specialize adapt_structure for small tuples (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
charleskawczynski authored Mar 20, 2024
1 parent 9c1b146 commit 925df4a
Showing 1 changed file with 15 additions and 2 deletions.
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

0 comments on commit 925df4a

Please sign in to comment.