Skip to content

Commit 231fca9

Browse files
authored
use permute!! or copyto! instead of fastpermute! (#236)
1 parent 97adceb commit 231fca9

File tree

6 files changed

+42
-11
lines changed

6 files changed

+42
-11
lines changed

src/IndexedTables.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ using OnlineStatsBase: OnlineStat, fit!
77
using StructArrays: StructVector, StructArray, foreachfield, fieldarrays,
88
collect_structarray, staticschema, ArrayInitializer, refine_perm!, collect_structarray,
99
collect_empty_structarray, grow_to_structarray!, collect_to_structarray!, replace_storage,
10-
GroupPerm, GroupJoinPerm, roweq, rowcmp, fastpermute!
10+
GroupPerm, GroupJoinPerm, roweq, rowcmp
1111

1212
import Tables, TableTraits, IteratorInterfaceExtensions, TableTraitsUtils
1313

src/columns.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,4 +698,5 @@ function init_inputs(f::Tup, input, isvec)
698698
end
699699

700700
# utils
701+
refs(v::Columns) = Columns(map(refs, fieldarrays(v)))
701702
compact_mem(v::Columns) = replace_storage(compact_mem, v)

src/indexedtable.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function table(::Val{:serial}, cols::Tup;
9898
if copy
9999
cs = cs[perm]
100100
else
101-
cs = permute!(cs, perm)
101+
Base.permute!!(refs(cs), perm)
102102
end
103103
elseif copy
104104
cs = copyto!(similar(cs), cs)
@@ -345,7 +345,7 @@ Sort rows of `t` by `by` in place. All of `Base.sort` keyword arguments can be u
345345
"""
346346
function sort!(t::IndexedTable, by...; kwargs...)
347347
isempty(t.pkey) || error("Tables with primary keys can't be sorted in place")
348-
permute!(rows(t), sortperm(rows(t, by...); kwargs...))
348+
Base.permute!!(refs(rows(t)), sortperm(rows(t, by...); kwargs...))
349349
t
350350
end
351351

src/join.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,8 @@ function _broadcast(f::Function, B::NDSparse, C::NDSparse; dimmap=nothing)
574574
idx, iperm, vals = _bcast_loop(f, B, C, B_common_cols, B_perm)
575575
A = NDSparse(idx, vals, copy=false, presorted=true)
576576
if !issorted(A.index)
577-
fastpermute!(A.index, iperm)
578-
fastpermute!(A.data, iperm)
577+
copyto!(A.data, A.data[iperm])
578+
Base.permute!!(refs(A.index), iperm)
579579
end
580580
else
581581
# TODO

src/utils.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,17 @@ getsubfields(t::Tuple, fields) = t[fields]
267267
product(itr) = itr
268268
product(itrs...) = Base.Generator(reverse, Iterators.product(reverse(itrs)...))
269269

270+
# refs
271+
272+
refs(v::PooledArray) = v.refs
273+
refs(v::AbstractArray) = v
274+
function refs(v::StringArray{T}) where {T}
275+
S = Union{WeakRefString{UInt8}, typeintersect(T, Missing)}
276+
convert(StringArray{S}, v)
277+
end
278+
270279
# pool non isbits types
271280

272281
compact_mem(v::PooledArray) = v
273282
compact_mem(v::AbstractArray{T}) where {T} = isbitstype(T) ? v : PooledArray(v)
274-
compact_mem(v::StringArray{String}) =
275-
map(String, PooledArray(convert(StringArray{WeakRefString{UInt8}}, v)))
283+
compact_mem(v::StringArray{T}) where {T} = map(t -> convert(T, t), PooledArray(refs(v)))

test/test_utils.jl

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,40 @@ end
3737
@testset "compact_mem" begin
3838
v = Columns(a=rand(10), b = fill("string", 10))
3939
v_pooled = IndexedTables.replace_storage(v) do c
40-
isbitstype(eltype(c)) ? c : convert(PooledArrays.PooledArray, c)
40+
isbitstype(eltype(c)) ? c : convert(PooledArray, c)
4141
end
4242
@test eltype(v) == eltype(v_pooled)
4343
@test all(v.a .== v_pooled.a)
4444
@test all(v.b .== v_pooled.b)
45-
@test !isa(v_pooled.a, PooledArrays.PooledArray)
46-
@test isa(v_pooled.b, PooledArrays.PooledArray)
45+
@test !isa(v_pooled.a, PooledArray)
46+
@test isa(v_pooled.b, PooledArray)
4747
@test v_pooled == IndexedTables.compact_mem(v)
4848
s = WeakRefStrings.StringArray(["a", "b", "c"])
49-
@test IndexedTables.compact_mem(s) isa PooledArrays.PooledArray{String}
49+
@test IndexedTables.compact_mem(s) isa PooledArray{String}
5050
@test IndexedTables.compact_mem(s)[1] == "a"
5151
@test IndexedTables.compact_mem(s)[2] == "b"
5252
@test IndexedTables.compact_mem(s)[3] == "c"
5353
@test IndexedTables.compact_mem(IndexedTables.compact_mem(s)) == IndexedTables.compact_mem(s)
54+
s = WeakRefStrings.StringArray(["a", missing, "c"])
55+
@test IndexedTables.compact_mem(s) isa PooledArray{Union{String, Missing}}
56+
@test all(isequal.(s, IndexedTables.compact_mem(s)))
57+
end
58+
59+
@testset "refs" begin
60+
a = WeakRefStrings.StringVector(["a", "b", "c"])
61+
b = PooledArrays.PooledArray(["1", "2", "3"])
62+
c = [:a, :b, :c]
63+
s = Columns(a=a, b=b, c=c)
64+
ref = IndexedTables.refs(s)
65+
@test ref[1].a isa WeakRefStrings.WeakRefString{UInt8}
66+
@test all(isequal.(ref.a, a))
67+
@test ref[1].b isa UInt8
68+
@test isequal(ref.b, UInt8.([1, 2, 3]))
69+
Base.permute!!(ref, sortperm(s))
70+
@test issorted(s)
71+
72+
a = WeakRefStrings.StringVector(["a", missing, "c"])
73+
refa = IndexedTables.refs(a)
74+
@test refa isa WeakRefStrings.StringArray{Union{Missing, WeakRefStrings.WeakRefString{UInt8}}}
75+
@test all(isequal.(a, refa))
5476
end

0 commit comments

Comments
 (0)