From 925df4aa39f6715b088c4fa7712fd58cc4a28da5 Mon Sep 17 00:00:00 2001 From: Charles Kawczynski Date: Wed, 20 Mar 2024 10:30:16 -0400 Subject: [PATCH] Re-land: Specialize adapt_structure for small tuples (#81) --- src/base.jl | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/base.jl b/src/base.jl index 6a55250..541c834 100644 --- a/src/base.jl +++ b/src/base.jl @@ -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