Skip to content

Commit

Permalink
perf(qtree): Replace Vector{Index} with custom Indices4 struct for be…
Browse files Browse the repository at this point in the history
…tter performance
  • Loading branch information
guo-yong-zhi committed Nov 5, 2024
1 parent be59a3c commit bef04b5
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions src/qtree_functions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,21 @@ end
hash_spacial_qtree() = HashSpacialQTree()
hash_spacial_qtree(qts) = hash_spacial_qtree()

mutable struct Indices4
i1::Index
i2::Index
i3::Index
i4::Index
len::Int
Indices4() = (ii = new(); ii.len = 0; ii)
end
Base.getindex(ii::Indices4, i) = getfield(ii, i)
Base.setindex!(ii::Indices4, x, i) = setfield!(ii, i, x)
Base.push!(ii::Indices4, x) = ii[ii.len += 1] = x
Base.length(ii::Indices4) = ii.len
Base.iterate(ii::Indices4, i=1) = i <= length(ii) ? (ii[i], i+1) : nothing
Base.eltype(::Type{Indices4}) = Index

function locate!(qt::AbstractStackedQTree, spqtree::Union{HashSpacialQTree, LinkedSpacialQTree}, label::Int)
l = length(qt) #l always >= 2
# @assert kernelsize(qt[l], 1) <= 2 && kernelsize(qt[l], 2) <= 2
Expand All @@ -156,8 +171,7 @@ function locate!(qt::AbstractStackedQTree, spqtree::Union{HashSpacialQTree, Link
# @assert l >= 2
@inbounds mat = qt[l]
rs, cs = getshift(mat)
inds = Vector{Index}(undef, 4)
empty!(inds)
inds = Indices4()
@inbounds mat[rs+1, cs+1] != EMPTY && push!(inds, (l, rs+1, cs+1))
@inbounds mat[rs+1, cs+2] != EMPTY && push!(inds, (l, rs+1, cs+2))
@inbounds mat[rs+2, cs+1] != EMPTY && push!(inds, (l, rs+2, cs+1))
Expand All @@ -166,8 +180,7 @@ function locate!(qt::AbstractStackedQTree, spqtree::Union{HashSpacialQTree, Link
l = l + 1
@inbounds mat = qt[l]
rs, cs = getshift(mat)
inds2 = Vector{Index}(undef, 4)
empty!(inds2)
inds2 = Indices4()
@inbounds mat[rs+1, cs+1] != EMPTY && push!(inds2, (l, rs+1, cs+1))
@inbounds mat[rs+1, cs+2] != EMPTY && push!(inds2, (l, rs+1, cs+2))
@inbounds mat[rs+2, cs+1] != EMPTY && push!(inds2, (l, rs+2, cs+1))
Expand Down

0 comments on commit bef04b5

Please sign in to comment.