From 56e6268241914e8dc06a89f97e83b4d04a7dfe2f Mon Sep 17 00:00:00 2001 From: mathieu17g <72861595+mathieu17g@users.noreply.github.com> Date: Sun, 6 Mar 2022 03:51:13 +0100 Subject: [PATCH] fallbacks: inline to `replacex` in `add_or_widen!` (compilation performance) (#275) dicts: use of `OrderedDict` in `dictcolumntable` (column ordering) --- Project.toml | 2 ++ src/Tables.jl | 2 +- src/dicts.jl | 10 +++++----- src/fallbacks.jl | 4 +--- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Project.toml b/Project.toml index 367fe08..1f7f8b2 100644 --- a/Project.toml +++ b/Project.toml @@ -8,6 +8,7 @@ DataAPI = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a" DataValueInterfaces = "e2d170a0-9d28-54be-80f0-106bbe20a464" IteratorInterfaceExtensions = "82899510-4779-5014-852e-03e436cf321d" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +OrderedCollections = "bac558e1-5e72-5ebc-8fee-abe8a469f55d" TableTraits = "3783bdb8-4a98-5b6b-af9a-565f29a5fe9c" Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" @@ -15,6 +16,7 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40" DataAPI = "1" DataValueInterfaces = "1" IteratorInterfaceExtensions = "0.1.1, 1" +OrderedCollections = "1" TableTraits = "0.4.1, 1" julia = "1" diff --git a/src/Tables.jl b/src/Tables.jl index 8d7e7fb..d3e0328 100644 --- a/src/Tables.jl +++ b/src/Tables.jl @@ -1,6 +1,6 @@ module Tables -using LinearAlgebra, DataValueInterfaces, DataAPI, TableTraits, IteratorInterfaceExtensions +using LinearAlgebra, DataValueInterfaces, DataAPI, TableTraits, IteratorInterfaceExtensions, OrderedCollections export rowtable, columntable diff --git a/src/dicts.jl b/src/dicts.jl index 4542654..7f377f5 100644 --- a/src/dicts.jl +++ b/src/dicts.jl @@ -1,6 +1,6 @@ struct DictColumnTable <: AbstractColumns schema::Schema - values::Dict{Symbol, AbstractVector} + values::OrderedDict{Symbol, AbstractVector} end """ @@ -24,7 +24,7 @@ function dictcolumntable(x) cols = columns(x) names = columnnames(cols) sch = schema(cols) - out = Dict(nm => getcolumn(cols, nm) for nm in names) + out = OrderedDict(nm => getcolumn(cols, nm) for nm in names) else r = rows(x) L = Base.IteratorSize(typeof(r)) @@ -32,15 +32,15 @@ function dictcolumntable(x) sch = schema(r) if sch !== nothing names, types = sch.names, sch.types - out = Dict{Int, AbstractVector}(i => allocatecolumn(types[i], len) for i = 1:length(types)) + out = OrderedDict{Int, AbstractVector}(i => allocatecolumn(types[i], len) for i = 1:length(types)) for (i, row) in enumerate(r) eachcolumns(add!, sch, row, out, L, i) end - out = Dict(names[k] => v for (k, v) in out) + out = OrderedDict(names[k] => v for (k, v) in out) else names = Symbol[] seen = Set{Symbol}() - out = Dict{Symbol, AbstractVector}() + out = OrderedDict{Symbol, AbstractVector}() for (i, row) in enumerate(r) for nm in columnnames(row) push!(seen, nm) diff --git a/src/fallbacks.jl b/src/fallbacks.jl index 0c6aea1..485b1ca 100644 --- a/src/fallbacks.jl +++ b/src/fallbacks.jl @@ -140,8 +140,6 @@ end @inline add!(dest::AbstractArray, val, ::Union{Base.HasLength, Base.HasShape}, row) = setindex!(dest, val, row) @inline add!(dest::AbstractArray, val, T, row) = push!(dest, val) -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 || promote_type(typeof(val), T) <: T add!(dest, val, L, row) @@ -150,7 +148,7 @@ replacex(t, col::Int, x) = ntuple(i->i == col ? x : t[i], length(t)) new = allocatecolumn(promote_type(T, typeof(val)), length(dest)) row > 1 && copyto!(new, 1, dest, 1, row - 1) add!(new, val, L, row) - updated[] = replacex(updated[], col, new) + updated[] = ntuple(i->i == col ? new : updated[][i], length(updated[])) return end end