From 609560a8118cb2a9f1816b819e2227f6e92a9f8a Mon Sep 17 00:00:00 2001 From: Jacob Quinn Date: Sun, 7 Feb 2021 04:49:42 -0700 Subject: [PATCH] Improve widening check for non-schema column building (#232) The issue here is seen in a simple example where a row-oriented input may have a Float64 element early on, but Int64 elements afterwards. For those Int64 elements, the `val isa T` check fails, which leads to the code branch where the array is widened using `promote_type(typeof(val), T)` and reallocated. For the case of Int64 and Float64, `promote_type` produces Float64, so the array is reallocated unnecessarily. By instead checking if our `val` to set is _either_ `val isa T || promote_type(typeof(val), T) <: T` then we take the assumption that if `val` promotes to `T` then it can also be converted to `T` on `setindex!`. This is tricky to test for because there's actually no change in behavior; we're just avoiding a lot of unnecessary array allocating/inefficiencies. --- src/fallbacks.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fallbacks.jl b/src/fallbacks.jl index 48c2ab1..10303e2 100644 --- a/src/fallbacks.jl +++ b/src/fallbacks.jl @@ -143,7 +143,7 @@ end replacex(t, col::Int, x) = ntuple(i->i == col ? x : t[i], length(t)) @inline function add_or_widen!(val, col::Int, nm, dest::AbstractArray{T}, row, updated, L) where {T} - if val isa T + if val isa T || promote_type(typeof(val), T) <: T add!(dest, val, L, row) return else