Skip to content

Commit

Permalink
fallbacks: inline to replacex in add_or_widen! (compilation perfo…
Browse files Browse the repository at this point in the history
…rmance) (#275)

dicts: use of `OrderedDict` in `dictcolumntable` (column ordering)
  • Loading branch information
mathieu17g authored Mar 6, 2022
1 parent 5e62ab2 commit 56e6268
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ 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"

[compat]
DataAPI = "1"
DataValueInterfaces = "1"
IteratorInterfaceExtensions = "0.1.1, 1"
OrderedCollections = "1"
TableTraits = "0.4.1, 1"
julia = "1"

Expand Down
2 changes: 1 addition & 1 deletion src/Tables.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Tables

using LinearAlgebra, DataValueInterfaces, DataAPI, TableTraits, IteratorInterfaceExtensions
using LinearAlgebra, DataValueInterfaces, DataAPI, TableTraits, IteratorInterfaceExtensions, OrderedCollections

export rowtable, columntable

Expand Down
10 changes: 5 additions & 5 deletions src/dicts.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
struct DictColumnTable <: AbstractColumns
schema::Schema
values::Dict{Symbol, AbstractVector}
values::OrderedDict{Symbol, AbstractVector}
end

"""
Expand All @@ -24,23 +24,23 @@ 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))
len = Base.haslength(r) ? length(r) : 0
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)
Expand Down
4 changes: 1 addition & 3 deletions src/fallbacks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit 56e6268

Please sign in to comment.