From 5dd180bb66b2c5bb040ac4b59892064e59456262 Mon Sep 17 00:00:00 2001 From: guoyongzhi Date: Mon, 11 Nov 2024 19:26:11 +0800 Subject: [PATCH] optimize SVector4 and improve type stability --- src/common_datatypes.jl | 22 +++++++++++++--------- src/qtree_functions.jl | 4 ++-- src/qtrees.jl | 4 ++-- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/common_datatypes.jl b/src/common_datatypes.jl index 875a72f..b7dc855 100644 --- a/src/common_datatypes.jl +++ b/src/common_datatypes.jl @@ -187,27 +187,31 @@ Base.getindex(im::IntMap, ind...) = getindex(im.map, ind...) Base.setindex!(im::IntMap, v, ind...) = setindex!(im.map, v, ind...) ##### SVector4 -mutable struct SVector4{T} +mutable struct SVector4Core{T} e1::T e2::T e3::T e4::T + SVector4Core{T}() where T = new() +end +mutable struct SVector4{T} + vec::SVector4Core{T} len::Int - SVector4{T}() where T = (v = new(); v.len = 0; v) end +SVector4{T}() where T = SVector4{T}(SVector4Core{T}(), 0) function SVector4{T}(e1, e2, e3, e4) where T v = SVector4{T}() - v.e1 = e1 - v.e2 = e2 - v.e3 = e3 - v.e4 = e4 + v.vec.e1 = e1 + v.vec.e2 = e2 + v.vec.e3 = e3 + v.vec.e4 = e4 v.len = 4 v end SVector4(e1::T, e2::T, e3::T, e4::T) where T = SVector4{T}(e1, e2, e3, e4) -Base.getindex(v::SVector4, i) = getfield(v, i) -Base.setindex!(v::SVector4, x, i) = setfield!(v, i, x) -Base.push!(v::SVector4, x) = v[v.len += 1] = x +Base.getindex(v::SVector4{T}, i) where T = getfield(v.vec, i)::T +Base.setindex!(v::SVector4{T}, x::T, i) where T = setfield!(v.vec, i, x) +Base.push!(v::SVector4{T}, x::T) where T = v[v.len += 1] = x Base.length(v::SVector4) = v.len Base.iterate(v::SVector4, i=1) = i <= length(v) ? (v[i], i+1) : nothing Base.eltype(::Type{SVector4{T}}) where T = T diff --git a/src/qtree_functions.jl b/src/qtree_functions.jl index d67a875..f173c3d 100644 --- a/src/qtree_functions.jl +++ b/src/qtree_functions.jl @@ -1,5 +1,5 @@ ########## totalcollisions -function collision_dfs(Q1::AbstractStackedQTree, Q2::AbstractStackedQTree, i=(length(Q1), 1, 1)) #faster than _collision_randbfs (6:7) +function collision_dfs(Q1::AbstractStackedQTree, Q2::AbstractStackedQTree, i::Index=(length(Q1), 1, 1)) #faster than _collision_randbfs (6:7) # @assert size(Q1) == size(Q2) code = Q1[i] & Q2[i] if code == FULL # Q1[i] == FULL || Q2[i] == FULL && (Q1[i] != EMPTY && Q2[i] != EMPTY) @@ -138,7 +138,7 @@ function linked_spacial_qtree(qts) end hash_spacial_qtree() = HashSpacialQTree() hash_spacial_qtree(qts) = hash_spacial_qtree() -function positionlower(qt, ind) +function positionlower(qt, ind::Index) while ind[1] > 2 cind = ind flag = false diff --git a/src/qtrees.jl b/src/qtrees.jl index 4b2d07c..4ad8687 100644 --- a/src/qtrees.jl +++ b/src/qtrees.jl @@ -119,8 +119,8 @@ function PaddedMat(l::T, sz::Tuple{Int,Int}=size(l), rshift=0, cshift=0; default m.kernel[2:end - 2, 2:end - 2] .= l m end -function PaddedMat{T}(kernelsz::Tuple{Int,Int}, sz::Tuple{Int,Int}=size(l), - rshift=0, cshift=0; default=0x00) where {T <: AbstractMatrix{UInt8}} +function PaddedMat{T}(kernelsz::Tuple{Int,Int}, sz::Tuple{Int,Int}, + rshift=0, cshift=0; default=zero(T)) where {T <: AbstractMatrix{UInt8}} k = similar(T, kernelsz .+ 3) # +3 to keep top-down getindex in _collision_randbfs within the kernel bounds k[[1, end - 1, end], :] .= default k[:, [1, end - 1, end]] .= default