From a112efbc17afc7ea043c887c7866392447f2abac Mon Sep 17 00:00:00 2001 From: James Blachly Date: Sun, 5 Jan 2020 20:52:05 -0500 Subject: [PATCH] Probabilistic splaying yields speed increase in sequential workloads --- dub.json | 3 ++- source/intervaltree/splaytree.d | 13 +++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/dub.json b/dub.json index 7741f77..dcbc6f7 100644 --- a/dub.json +++ b/dub.json @@ -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"], diff --git a/source/intervaltree/splaytree.d b/source/intervaltree/splaytree.d index 207fe3e..f406065 100644 --- a/source/intervaltree/splaytree.d +++ b/source/intervaltree/splaytree.d @@ -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 @@ -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; @@ -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; @@ -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");