From 80c1a250e64c4dc3b3baf32ce5f901b958d48c7f Mon Sep 17 00:00:00 2001 From: Moritz Schauer Date: Wed, 31 Oct 2018 01:32:24 +0100 Subject: [PATCH] Reduce allocation in flatten (#29786) (cherry picked from commit 07ea3023bc762186a7898b112af9ca0ccf848b91) --- base/iterators.jl | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/base/iterators.jl b/base/iterators.jl index b513d78c89d34..449e8dbb0663d 100644 --- a/base/iterators.jl +++ b/base/iterators.jl @@ -906,7 +906,13 @@ length(f::Flatten{Tuple{}}) = 0 end x = (state === () ? iterate(f.it) : iterate(f.it, state[1])) x === nothing && return nothing - iterate(f, (x[2], x[1])) + y = iterate(x[1]) + while y === nothing + x = iterate(f.it, x[2]) + x === nothing && return nothing + y = iterate(x[1]) + end + return y[1], (x[2], x[1], y[2]) end reverse(f::Flatten) = Flatten(reverse(itr) for itr in reverse(f.it))