From 931dc51b9ef0efa2df4f25c582b082210b0f3777 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Thu, 11 Jan 2018 11:37:32 -0600 Subject: [PATCH] Improve performance of eachindex(a, b). Fixes #25497 (#25503) --- base/abstractarray.jl | 7 ++++++- base/multidimensional.jl | 2 +- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/base/abstractarray.jl b/base/abstractarray.jl index c7e5ff9d41fb0..5a9220c2d91a4 100644 --- a/base/abstractarray.jl +++ b/base/abstractarray.jl @@ -849,9 +849,14 @@ eachindex(::IndexLinear, A::AbstractArray) = linearindices(A) function eachindex(::IndexLinear, A::AbstractArray, B::AbstractArray...) @_inline_meta indsA = linearindices(A) - all(x->linearindices(x) == indsA, B) || throw_eachindex_mismatch(IndexLinear(), A, B...) + _all_match_first(linearindices, indsA, B...) || throw_eachindex_mismatch(IndexLinear(), A, B...) indsA end +function _all_match_first(f::F, inds, A, B...) where F<:Function + @_inline_meta + (inds == f(A)) & _all_match_first(f, inds, B...) +end +_all_match_first(f::F, inds) where F<:Function = true isempty(a::AbstractArray) = (_length(a) == 0) diff --git a/base/multidimensional.jl b/base/multidimensional.jl index 0294042fcefec..e3277ebd1917c 100644 --- a/base/multidimensional.jl +++ b/base/multidimensional.jl @@ -264,7 +264,7 @@ module IteratorsMD @inline function eachindex(::IndexCartesian, A::AbstractArray, B::AbstractArray...) axsA = axes(A) - all(x->axes(x) == axsA, B) || Base.throw_eachindex_mismatch(IndexCartesian(), A, B...) + Base._all_match_first(axes, axsA, B...) || Base.throw_eachindex_mismatch(IndexCartesian(), A, B...) CartesianIndices(axsA) end