-
Notifications
You must be signed in to change notification settings - Fork 249
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
Fix splay tree amortized time issues (resolves #923) #924
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
module BenchTrees | ||
|
||
using DataStructures | ||
using BenchmarkTools | ||
using Random | ||
|
||
suite = BenchmarkGroup() | ||
|
||
rand_setup = ( | ||
Random.seed!(1234); | ||
idx1 = rand(1:30000, 1000); | ||
didx1 = rand(1:1000, 500); | ||
) | ||
|
||
# insert a bunch of keys, search for all of them, delete half of them randomly, | ||
# then search for all of them again. | ||
function test_rand(T) | ||
t = T{Int}() | ||
for i in idx1 | ||
push!(t, i) | ||
end | ||
for i in idx1 | ||
haskey(t, i) | ||
end | ||
for i in didx1 | ||
delete!(t, idx1[i]) | ||
end | ||
for i in idx1 | ||
haskey(t, i) | ||
end | ||
end | ||
|
||
# insert 1, ..., N, then push 1 many times. tests a regression from an older | ||
# splay tree implementation where splays didn't happen on redundant pushes. | ||
function test_redundant(T, N=100000) | ||
t = T{Int}() | ||
for i in 1:N | ||
push!(t, i) | ||
end | ||
for i in 1:N | ||
push!(t, 1) | ||
end | ||
end | ||
|
||
# insert 1, ..., N, then access element 1 for N iterations. splay trees should | ||
# perform best here. | ||
function test_biased(T, N=100000) | ||
t = T{Int}() | ||
for i in 1:N | ||
push!(t, i) | ||
end | ||
for _ in 1:N | ||
haskey(t, 1) | ||
end | ||
end | ||
|
||
trees = Dict("splay" => SplayTree, "avl" => AVLTree, "rb" => RBTree) | ||
for (T_name, T) in trees | ||
suite[T_name]["rand"] = | ||
@benchmarkable test_rand($(T)) setup=rand_setup | ||
suite[T_name]["redundant"] = | ||
@benchmarkable test_redundant($(T)) | ||
suite[T_name]["biased"] = | ||
@benchmarkable test_biased($T) | ||
end | ||
|
||
end # module | ||
|
||
BenchTrees.suite |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,7 +126,7 @@ function _join!(tree::SplayTree, s::Union{SplayTreeNode, Nothing}, t::Union{Spla | |
end | ||
end | ||
|
||
function search_node(tree::SplayTree{K}, d::K) where K | ||
function search_node!(tree::SplayTree{K}, d::K) where K | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I gave this a thought. And perhaps now I recall the reason why I didn't splay while searching. Making The context for use of splay trees implies you want recently accessed elements near to the root, so that the subsequent access takes less time. @oxinabox are we okay for introducing a breaking change in the package? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes into the master branch that is fine. We are still prepping 0.19 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Also, might be beating a dead horse here, but it seems to me this |
||
node = tree.root | ||
prev = nothing | ||
while node != nothing && node.data != d | ||
|
@@ -137,33 +137,31 @@ function search_node(tree::SplayTree{K}, d::K) where K | |
node = node.leftChild | ||
end | ||
end | ||
return (node == nothing) ? prev : node | ||
last = (node == nothing) ? prev : node | ||
(last == nothing) || splay!(tree, last) | ||
return last | ||
end | ||
|
||
function Base.haskey(tree::SplayTree{K}, d::K) where K | ||
node = tree.root | ||
if node === nothing | ||
return false | ||
else | ||
node = search_node(tree, d) | ||
node = search_node!(tree, d) | ||
(node === nothing) && return false | ||
is_found = (node.data == d) | ||
is_found && splay!(tree, node) | ||
return is_found | ||
return (node.data == d) | ||
end | ||
end | ||
|
||
Base.in(key, tree::SplayTree) = haskey(tree, key) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Inline with my above comment, every function calling |
||
|
||
function Base.delete!(tree::SplayTree{K}, d::K) where K | ||
node = tree.root | ||
x = search_node(tree, d) | ||
(x == nothing) && return tree | ||
x = search_node!(tree, d) | ||
(x == nothing || x.data != d) && return tree | ||
t = nothing | ||
s = nothing | ||
|
||
splay!(tree, x) | ||
|
||
if x.rightChild !== nothing | ||
t = x.rightChild | ||
t.parent = nothing | ||
|
@@ -183,7 +181,7 @@ end | |
|
||
function Base.push!(tree::SplayTree{K}, d0) where K | ||
d = convert(K, d0) | ||
is_present = search_node(tree, d) | ||
is_present = search_node!(tree, d) | ||
if (is_present !== nothing) && (is_present.data == d) | ||
return tree | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are the results for this benchmark suite? Do post on the main PR.