Skip to content

Commit

Permalink
add a Base.tail method for nonempty tuples
Browse files Browse the repository at this point in the history
I have some code that sorts tuples using recursion. When the tuple
length gets big (over around 65), suddenly I get lots of allocations in
my code, when there should be none. Using `@profview_allocs` shows that
the allocations happen in `Base.tail`. This simple implementation fixes
that.
  • Loading branch information
nsajko committed Nov 9, 2023
1 parent 529e4e7 commit c2b01fc
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions base/ntuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,10 @@ end
(t..., fill(val, N-M)...)
end
end

function tail(x::Tuple{Any,Vararg{Any,n}}) where {n}
f = let x = x
i -> x[i + 1]
end
ntuple(f, Val(n))::NTuple{n,Any}
end

0 comments on commit c2b01fc

Please sign in to comment.