Skip to content

Commit

Permalink
Probabilistic splaying yields speed increase in sequential workloads
Browse files Browse the repository at this point in the history
  • Loading branch information
jblachly committed Jan 6, 2020
1 parent 9148a5c commit a112efb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
3 changes: 2 additions & 1 deletion dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"homepage": "http://github.com/blachlylab/intervaltree",
"license": "MIT",
"dependencies": {
"emsi_containers": "~>0.8.0-alpha.12"
"emsi_containers": "~>0.8.0-alpha.12",
"mir-random": "~>2.2.8"
},
"sourceFiles": [ "source/cgranges.o" ],
"excludedSourceFiles": ["source/intervaltree/khash.d", "source/intervaltree/roundup.d"],
Expand Down
13 changes: 11 additions & 2 deletions source/intervaltree/splaytree.d
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import std.experimental.allocator.building_blocks.region;
import std.experimental.allocator.building_blocks.allocator_list : AllocatorList;
import std.experimental.allocator.mallocator : Mallocator;

import mir.random;

version(instrument) __gshared int[] _splaytree_visited;

/// Probably should not be used directly by consumer
Expand Down Expand Up @@ -395,6 +397,12 @@ struct IntervalSplayTree(IntervalType)
@safe @nogc nothrow
private void splay(Node *n)
{
// probablistically splay:
// Albers and Karpinski, Randomized splay trees: theoretical and experimental results
// Information Processing Letters, Volume 81, Issue 4, 28 February 2002
// http://www14.in.tum.de/personen/albers/papers/ipl02.pdf
if (rand!ubyte & 0b11110000) return;

while (n.parent !is null)
{
const Node *p = n.parent;
Expand Down Expand Up @@ -482,7 +490,7 @@ struct IntervalSplayTree(IntervalType)
if (__traits(hasMember, T, "start") &&
__traits(hasMember, T, "end"))
{
Node*[64] stack = void;
Node*[128] stack = void;
int s;
debug int maxs;
version(instrument) int visited;
Expand Down Expand Up @@ -516,7 +524,8 @@ struct IntervalSplayTree(IntervalType)

debug(intervaltree_debug)
{
if (s > 64) {
// guard was (s > 64) but there are three increments above, so decrease for safety
if (s > 60) {
import core.stdc.stdio : stderr, fprintf;
fprintf(stderr, "FAIL maxs: %d", maxs);
assert(0, "stack overflow :-( Please post an issue at https://github.com/blachlylab/intervaltree");
Expand Down

0 comments on commit a112efb

Please sign in to comment.