From 324e8a4c39ade79ef689e6bf97b1f2ea54682050 Mon Sep 17 00:00:00 2001 From: Johnny Chen Date: Fri, 22 Oct 2021 18:48:42 +0800 Subject: [PATCH] More efficient eager Array conversion for OffsetArray (#46) --- src/showmime.jl | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/showmime.jl b/src/showmime.jl index d9aff22..804b47f 100644 --- a/src/showmime.jl +++ b/src/showmime.jl @@ -54,10 +54,19 @@ csnormalize(c::Colorant) = RGBA(c) # Unless we have PNG IO backend that works on generic array types, we have to eagerly # convert it to dense array types -# On performance: if the array type(e.g., OffsetArray) has efficient convert method to Array -# then this is almost a no-op -enforce_standard_dense_array(A::AbstractArray) = convert(Array, A) +# On performance: if the array type has efficient convert method to Array then this is +# almost a no-op +function enforce_standard_dense_array(A::AbstractArray) + if Base.has_offset_axes(A) + convert(Array, OffsetArrays.no_offset_view(A)) + else + convert(Array, A) + end +end enforce_standard_dense_array(A::DenseArray) = A +enforce_standard_dense_array(A::OffsetArray) = enforce_standard_dense_array(parent(A)) +# TODO(johnnychen94): Uncomment this when we set direct dependency to PNGFiles. +# enforce_standard_dense_array(A::IndirectArray) = A # PNGFiles has built-in support for IndirectArray. const ColorantMatrix{T<:Colorant} = AbstractMatrix{T}