Skip to content

Commit 9e13319

Browse files
committed
Fix type instabilities in RB tree
Improves benchmark results for #762 Before: 100000 elements DataStructures.AVLTree 201.994 ms (200002 allocations: 9.16 MiB) DataStructures.RBTree 514.536 ms (200003 allocations: 12.21 MiB) 1000 elements DataStructures.AVLTree 1.303 ms (2002 allocations: 93.80 KiB) DataStructures.RBTree 4.395 ms (2003 allocations: 125.11 KiB) 10 elements DataStructures.AVLTree 6.998 μs (22 allocations: 1008 bytes) DataStructures.RBTree 37.789 μs (23 allocations: 1.36 KiB) Now: 100000 elements DataStructures.AVLTree 261.295 ms (204574 allocations: 9.23 MiB) DataStructures.RBTree 394.023 ms (200003 allocations: 12.21 MiB) 1000 elements DataStructures.AVLTree 1.771 ms (2014 allocations: 93.98 KiB) DataStructures.RBTree 3.190 ms (2003 allocations: 125.11 KiB) 10 elements DataStructures.AVLTree 9.797 μs (22 allocations: 1008 bytes) DataStructures.RBTree 17.728 μs (23 allocations: 1.36 KiB)
1 parent fde262b commit 9e13319

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

src/DataStructures.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ module DataStructures
5757

5858
export findkey
5959

60+
# _getproperty(x::Nothing, f::Symbol) = @assert false
61+
_setproperty!(x::Nothing, f::Symbol, v) = @assert false
62+
6063
include("delegate.jl")
6164

6265
include("deque.jl")

src/avl_tree.jl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ AVLTreeNode(d) = AVLTreeNode{Any}(d)
1616

1717
AVLTreeNode_or_null{T} = Union{AVLTreeNode{T}, Nothing}
1818

19-
_getproperty(x::Nothing, f) = @assert false
20-
_getproperty(x::AVLTreeNode{T}, f) where {T} = getfield(x, f)
21-
Base.getproperty(x::AVLTreeNode_or_null{T}, f::Symbol) where {T} =
22-
_getproperty(x, f)
19+
# _getproperty(x::AVLTreeNode{T}, f::Symbol) where {T} = getfield(x, f)
20+
# Base.getproperty(x::AVLTreeNode_or_null{T}, f::Symbol) where {T} =
21+
# _getproperty(x, f)
2322

24-
_setproperty!(x::Nothing, f, v) = @assert false
2523
_setproperty!(x::AVLTreeNode{T}, f, v) where {T} =
2624
# setfield!(x, f, convert(fieldtype(typeof(x), f), v))
2725
setfield!(x, f, v)
@@ -31,8 +29,6 @@ _setproperty!(x::AVLTreeNode{T}, f, v::AVLTreeNode{T}) where {T} =
3129
setfield!(x, f, v)
3230
Base.setproperty!(x::AVLTreeNode_or_null{T}, f::Symbol, v) where {T} =
3331
_setproperty!(x, f, v)
34-
Base.setproperty!(x::AVLTreeNode_or_null{T}, f::Symbol, v::AVLTreeNode_or_null{T}) where {T} =
35-
_setproperty!(x, f, v)
3632

3733
mutable struct AVLTree{T}
3834
root::AVLTreeNode_or_null{T}

src/red_black_tree.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ end
1717
RBTreeNode() = RBTreeNode{Any}()
1818
RBTreeNode(d) = RBTreeNode{Any}(d)
1919

20+
# _getproperty(x::RBTreeNode{K}, f) where {K} = getfield(x, f)
21+
# Base.getproperty(x::Union{Nothing, RBTreeNode{K}}, f::Symbol) where {K} =
22+
# _getproperty(x, f)
23+
24+
_setproperty!(x::RBTreeNode{K}, f, v) where {K} =
25+
# setfield!(x, f, convert(fieldtype(typeof(x), f), v))
26+
setfield!(x, f, v)
27+
_setproperty!(x::RBTreeNode{K}, f, ::Nothing) where {K} =
28+
setfield!(x, f, nothing)
29+
_setproperty!(x::RBTreeNode{K}, f, v::RBTreeNode{K}) where {K} =
30+
setfield!(x, f, v)
31+
Base.setproperty!(x::Union{Nothing, RBTreeNode{K}}, f::Symbol, v) where {K} =
32+
_setproperty!(x, f, v)
33+
2034
function create_null_node(K::Type)
2135
node = RBTreeNode{K}()
2236
node.color = false

0 commit comments

Comments
 (0)