Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Make hash(::CategoricalValue) faster by pre-computing hashes #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions benchmark/benchmarks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,38 @@ end
@bench "CategoricalArray" sumequals(ca, ca[1])
@bench "NullableCategoricalArray" sumequals(nca, nca[1])
end

@benchgroup "hash" begin
function f(X)
h = zero(UInt)
index = X.pool.index
@inbounds for i in X.refs
h += hash(index[i])
end
h
end

function g(X)
h = zero(UInt)
@inbounds for x in X
h += hash(x)
end
h
end

function h(X)
h = zero(UInt)
pool = X.pool
@inbounds for i in X.refs
h += CategoricalArrays.hash_level(pool, i)
end
h
end

X = CategoricalArray(repeat(["ABCDEF", "GHIJKL", "MNOPQR", "STUVWX"], inner=100, outer=100))

using BenchmarkTools
@bench "hashing strings" f(X)
@bench "hashing CategoricalValues" g(X)
@bench "using precomputed hashes" h(X)
end
5 changes: 3 additions & 2 deletions src/buildfields.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ function buildinvindex{T, R}(index::Vector{T}, ::Type{R}=DefaultRefType)
return invindex
end

function buildvalues!{T, R, V}(pool::CategoricalPool{T, R, V})
function buildcaches!{T, R, V}(pool::CategoricalPool{T, R, V})
n = length(levels(pool))
resize!(pool.valindex, n)
resize!(pool.hashindex, n)
for i in 1:n
pool.valindex[i] = V(i, pool)
pool.hashindex[i] = hash(pool.index[i])
end
return pool.valindex
end

function buildorder!{S, R <: Integer}(order::Array{R},
Expand Down
2 changes: 2 additions & 0 deletions src/pool.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ function Base.delete!{S, R, V}(pool::CategoricalPool{S, R, V}, levels...)
return pool
end

hash_level(pool::CategoricalPool, i::Integer) = pool.hashindex[i]

function levels!{S, R, V}(pool::CategoricalPool{S, R, V}, newlevels::Vector)
if !allunique(newlevels)
throw(ArgumentError(string("duplicated levels found in newlevels: ",
Expand Down
10 changes: 6 additions & 4 deletions src/typedefs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ if VERSION >= v"0.6.0-dev.2643"
order::Vector{R}
levels::Vector{T}
valindex::Vector{V}
hashindex::Vector{UInt}
ordered::Bool

function CategoricalPool{T, R, V}(index::Vector{T},
invindex::Dict{T, R},
order::Vector{R},
ordered::Bool) where {T, R, V}
pool = new(index, invindex, order, index[order], V[], ordered)
buildvalues!(pool)
pool = new(index, invindex, order, index[order], V[], UInt[], ordered)
buildcaches!(pool)
return pool
end
end
Expand All @@ -33,14 +34,15 @@ else
order::Vector{R}
levels::Vector{T}
valindex::Vector{V}
hashindex::Vector{UInt}
ordered::Bool

function CategoricalPool{T, R}(index::Vector{T},
invindex::Dict{T, R},
order::Vector{R},
ordered::Bool)
pool = new(index, invindex, order, index[order], V[], ordered)
buildvalues!(pool)
pool = new(index, invindex, order, index[order], V[], UInt[], ordered)
buildcaches!(pool)
return pool
end
end
Expand Down
2 changes: 2 additions & 0 deletions src/value.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ Base.isequal(x::CategoricalValue, y::Any) = isequal(index(x.pool)[x.level], y)
Base.isequal(x::Any, y::CategoricalValue) = isequal(y, x)

Base.hash(x::CategoricalValue, h::UInt) = hash(index(x.pool)[x.level], h)
# When h=zero(UInt) we do not need to recompute the hash
Base.hash(x::CategoricalValue) = x.pool.hashindex[x.level]

function Base.isless{S, T}(x::CategoricalValue{S}, y::CategoricalValue{T})
throw(ArgumentError("CategoricalValue objects with different pools cannot be tested for order"))
Expand Down