Fast Snapshottable Concurrent Braun Heaps
Thomas D. Dickerson

arXiv:1705.06271v1 [] 17 May 2017

February 21, 2018
Abstract
This paper proposes a new concurrent heap algorithm, based on a stateless shape property, which
efficiently maintains balance during insert and removeMin operations implemented with hand-over-hand
locking. It also provides a O(1) linearizable snapshot operation based on lazy copy-on-write semantics.
Such snapshots can be used to provide consistent views of the heap during iteration, as well as to make
speculative updates (which can later be dropped).
The simplicity of the algorithm allows it to be easily proven correct, and the choice of shape property
provides priority queue performance which is competitive with highly optimized skiplist implementations
(and has stronger bounds on worst-case time complexity).
A Scala reference implementation is provided.

1

Introduction

Concurrent collections data structures are an important tool in modern software design, but they often
provide a limited set of operations compared to their serial counterparts, due to the complexity of concurrent
reasoning and implementation. The priority queue is a widely used data structure, that allows a user to
efficiently query and remove its smallest element. These operations impose a weaker ordering requirement
than other collections (e.g. an ordered map), and it is thus potentially more efficient to implement those
operations. Serial priority queues are typically implemented as either a heap or a skiplist, and heaps have
stronger guarantees of efficiency. Unfortunately, tree data structures have proven more difficult to efficiently
parallelize, and so most concurrent priority queues are based on some form of skiplist. [20]
This paper defines a new concurrent heap algorithm with a shape property that is better tailored to
concurrent updates, and augments it with a fast snapshot operation based on a copy-on-write methodology.
Snapshots can be used to provide read-only iterator semantics, but they can also be used to perform speculative updates, which are a key component in wrapping concurrent data structures with a transactional API
or implementing other kinds of reversible atomic objects. [7, 1]
A Scala reference implementation of the algorithm is provided in the appendix.

1.1

Prior Work

This paper builds on several prior lines of work.
Efficient tree data structures rely on a shape (or balance) property to ensure that the depth of the tree
grows only logarithmically in the number of elements. The Braun property states that for every node, n,
in a tree, the size of its left and right subtrees, l and r, are constrained such that |r| ≤ |l| ≤ (|r| + 1). It
was first proposed for efficient implementations of immutable trees in a pure-functional setting [4, 11], and
while relatively unknown in the broader community1 , Braun trees have long been known to be useful in
implementing heaps. [14, 16] An example of operations on a Braun heap is provided in Figures 1a, 1b, and
1c.
1 Unlike many algorithms and data structures, there is as of yet no Wikipedia article on the subject, which seems a reasonable
measure of obscurity

1

0

0

2
4

1
3

(a) The initial state.

2

1
5

2
4

3

(b) After insert(5).

4
5

1
3

(c) After removeMin.

Figure 1: A sequence of operations on a Braun heap, initialized with (0 to 4).foreach(heap.insert( )).

Collections data structures are of limited utility if their API only allows you to inspect or alter a single
element at a time, yet bulk read operations are challenging in a concurrent context. Correctness of concurrent operations is often defined in terms of linearizability [10], which ensures that all operations appear to
happen instantaneously; however, a bulk read operation necessarily must capture the state of the entire data
structure. Several recent works have leveraged a copy-on-write methodology to provide efficient linearizable
snapshots for a number of data structures, including AVL trees [5] and hash tries [18]. Linearizable iterators
are also an area of inquiry, recent work has implemented them for skip lists [17], which are of interest for
implementing priority queues.
Additionally, there are a number of other concurrent skiplist variants, which might be used as the basis
for a priority queue. [9, 19, 12]

1.2

Overview

It is unsurprising that importing insights from the purely-functional data structures community would be of
benefit in designing new concurrent data structures, particularly those requiring a fast-snapshot operation.
Without mutation, every reference to a data structure is intrinsically a snapshot (this property is known as
persistence [15]), because every update operation returns a new data structure (potentially sharing references
to sub-structures in the old one). Since allocation of new objects is a relatively expensive operation, it is
beneficial to design data structures that do not require extensive modifications to maintain bookkeeping
state or enforce algorithmic invariants. Moreover, every point where an algorithm on purely functional data
structures would allocate a new object, a concurrent algorithm implementing the same update as a mutation
will require synchronization, potentially resulting in a bottleneck.
Furthermore, functional programming separates reasoning about the result of a computation from reasoning about its execution, allowing a program to be written correctly even if the result of a computation is
delayed2 .
The Braun property in particular can be maintained by following two simple rules, without any bookkeeping. First, every insertion or deletion must swap the left and right subtrees. Second, insertions traverse
towards the (new) left subtree, and deletions proceed towards the (new) right subtree. As an added benefit
for concurrency, for every consecutive pair of updates, u, v, to a subtree rooted at a node n, the traversal
paths of u and v will intersect only at n. Thus, the number of threads that can be working simultaneously
doubles at each level of the tree. On the other hand, in a traditional binary heap, utilizing the fullness
property, every insertion and deletion is in contention for the boundary position between filled and unfilled
2 In

a single threaded setting, this is referred to as laziness. In a concurrent setting it is often referred to by the names
“future” or “promise” [8, 2].

2

leaves, and, with high probability, every consecutive pair of updates will intersect for the majority of their
path towards the root3 .
A sequential implementation of a Braun heap is provided in Listing 1, consisting of two classes: heap
nodes, containing a value and two child references, and the heap itself, containing a single reference to
the root. Our concurrent version preserves the essence of this implementation (see Listings 2,3,4,5 for
details), while providing thread-safety and linearizable snapshots. With one exception, the effects of mutating
operations only propagate leafward through the tree, so thread safety is achieved simply by following a handover-hand locking discipline. In the case of removeMin, the mutations are separated into two phases, the
first to maintain the Braun property, the second to maintain the heap property. In the first phase, nodes
must be able to reach upwards to modify references to themselves held by their immediate parents; however,
at the beginning of the first phase, the mutex on the root is reentrantly acquired a second time to prevent
observation while the heap property is violated, so thread safety is not violated by this minor breach of
hand-over-hand discipline.
The fast snapshot functionality is implemented as a reference-counting lazy copy-on-write (COW) scheme.
Each node is augmented with a snapCount property, which tracks the number of snapshot references to that
node (it is initialized to 0, since the copy in the “original” heap is not considered a snapshot). When a
snapshot of a heap is requested, a new heap4 is allocated with a reference to the same root node, and the
snapCount of that node is incremented. Mutation of any node with snapCount > 0 is forbidden, as this
would lead to inconsistent state in the other heaps that reference it. Instead, a new node is allocated with
the updated values, and the snapCounts of its left and right children are incremented, and the snapCount of
the original node, for which mutation was requested, is decremented. This has the effect of lazily “peeling”
the snapshot away from the original (i.e. only as-needed).
In a language with deterministic destructors, it would be correct for a heap to decrement the snapCount
of any snapshotted nodes when it exits scope; however, on the JVM finalize is often executed much later,
and any unnecessary allocations resulting from an imprecise snapCount are likely to have already occurred.
Thus, this implementation does not implement a finalize hook for that traversal.

2

Correctness

The correct execution of operations on the concurrent Braun Heap is subject to three invariants. The Braun
property has already been defined, but we restate it here for convenience, in Definition 1.
Definition 1 (Braun Tree). A binary tree, T , is Braun iff for every node, n ∈ T , the size of its left and
right subtrees, n.l and n.r, are constrained such that |n.r| ≤ |n.l| ≤ (|n.r| + 1).
The heap property is well known, but we use a non-standard formulation (Definition 2) conducive to
describing the behavior of threads traversing a tree.
Definition 2 (Heap). A tree, T , is a heap iff the values held by the nodes in every rootward path are
nonincreasing.
Snapshot isolation is well studied in the database literature, but our application is concerned with the
evolution of data structures subject to linearizable operations, and not subjected to transactional semantics. [3, 6] Nevertheless, we appropriate the terminology and provide a closely related5 Definition 3.
Definition 3 (Snapshot Isolation). Two heaps, H1 , H2 , subject to structural sharing, provide snapshot
isolation, iff, without loss of generality, no modification of H1 is reflected in the multiset of values held by
H2 .
they will intersect after 1 step with probability 12 , after 2 steps with probability 43 , etc.
that the heap class serves only to hold a reference to the root.
5 An optimistic STM wrapper built using the techniques of [7] would provide snapshot isolation to any transactions accessing
the heap.
3 Intuitively,

4 Recall

3

If every operation provided by the heap individually preserves the Braun, Heap, and Snapshot Isolation
properties, as defined above, and appears to take place at an atomic linearization point, then the algorithm
is correct. We will explicitly consider the insert, removeMin, getMin, and snapshot operations as implemented in Listing 5, as well as the pullUpLeftHelper and pushDownHelper helper methods that removeMin
is built on. For purposes of reasoning about atomicity through a hand-over-hand traversal, we will consider
the heap’s lock mediating access to the root to be the base case, rather than the locks owned by the root
itself.
Theorem 1 (Correctness of getMin). getMin happens atomically, and is Braun, Heap, and Snapshot Isolation preserving.
Proof. getMin is a read-only operation, so Braun, Heap, and Snapshot Isolation are preserved. getMin
acquires a read-lock on the heap, and subsequently on its root node, thus it appears to happen atomically
at the moment when the read lock is acquired on the heap, as no conflicting operations can occur once that
lock is acquired.
Theorem 2 (Correctness of snapshot). snapshot happens atomically, and is Braun, Heap, and Snapshot
Isolation preserving.
Proof. When a thread t executes snapshot on a heap, h, rooted at node n, it increments the snapCount of
n, and allocates a new heap, h0 , with h0 .n = n.
snapshot does not alter either a node’s stored value, or its children, so Braun, Heap, and Snapshot
Isolation are preserved. snapshot acquires a write-lock on the heap, and subsequently on its root node, thus
it appears to happen atomically at the moment when the write lock is acquired on the heap, as no conflicting
operations can occur once that lock is acquired.
Theorem 3 (Correctness of insert). insert happens atomically, and is Braun, Heap, and Snapshot Isolation
preserving.
Proof. insert acquires a write-lock on the heap and observes hand-over-hand locking along its leafward
path. At step i, no previous conflicting operation can intrude, as the lock for node ni on the path is held,
and no conflicting operation can be observed, as the lock for ni+1 will be acquired before proceeding 6 .
Inductively, the entire traversal appears to happen atomically at the moment when the write lock is acquired
on the heap.
When a thread executing insert(v) arrives at a subtree, t = {t.v, t.l, t.r}, an update is performed such
that, t0 = {min(t.v, v), t.r.insert(max(t.v, v)), t.l}, and t0 is returned.
If t is a snapshot, then t0 is the result of allocating a new node (with snapshots of its children), otherwise
t is mutated directly, thus Snapshot Isolation is preserved.
Assume that when the thread arrives at t, then t is Braun. Recall that Braun implies |t.r| ≤ |t.l| ≤ |t.r|+1.
Thus after insert, we have that |t0 .l| = |t.r| + 1 and |t0 .r| = |t.l|. Thus |t0 .r| ≤ |t0 .l| ≤ |t0 .r| + 1 and Braun
holds for t0 .
Assume that when the thread arrives at t, then t is a heap, implying t.v ≤ t.r.v. Since t0 .v = min(v, t.v)
and t0 .l.v = min(t.r.v, max(v, t.v)), then t0 .v ≤ t.v ≤ t0 .l.v. Thus t0 is also a heap.
Theorem 4 (Correctness of pullUpLeftHelper ). If invoked by removeMin, pullUpLeftHelper happens
atomically, and is Braun, Heap, and Snapshot Isolation preserving.
Proof. pullUpLeftHelper is called with a write-lock on the root and observes hand-over-hand locking along
its leafward path. The atomicity argument is largely as for insert; however, upon arriving at the target leaf,
the reference to that leaf in its immediate parent is deleted. Despite this violation of hand-over-hand locking,
pullUpLeftHelper maintains its atomicity if the invoking code retained a lock on the heap preventing any
other threads from traversing down after it.
6 Note that this holds even when n is structurally shared as the result of a snapshot operation. If n ∈ h and n ∈ h0 , then
i
i
i
competing threads performing inserts on h and h0 must still acquire the same lock on ni

4

When a thread executing pullUpLeftHelper arrives at a non-leaf subtree, t = {t.v, t.l, t.r}, an update
is performed such that, t0 = {t.v, t.r, t.l}, and then pullUpLeftHelper is invoked on t0 .r. If t0 .r is a leaf, it
is deleted, and t0 .r.v is returned.
If t is a snapshot, then t0 is the result of allocating a new node (with snapshots of its children), otherwise
t is mutated directly, thus Snapshot Isolation is preserved.
Assume that when the thread arrives at t, then t is Braun. Thus after pullUpLeftHelper, we have that
|t0 .r| = |t.l| − 1 and |t0 .l| = |t.r|. Thus |t0 .r| ≤ |t0 .l| ≤ |t0 .r| + 1 and Braun holds for t0 .
Assume that when the thread arrives at t, then t is a heap. No elements are inserted below t, thus every
rootward path through t remains nonincreasing, and pullUpLeftHelper preserves heap.
Theorem 5 (Correctness of pushDownHelper ). If invoked by removeMin, pushDownHelper happens atomically, and is Braun and Snapshot Isolation preserving, and Heap restoring.
Proof. pushDownHelper is called with a write-lock on the root and observes hand-over-hand locking along
its leafward path. The atomicity argument is as for insert.
When a thread executing pushDownHelper arrives at a non-leaf t = {t.v, t.l, t.r}, updates are performed
such that, t0 .v = min(t.v, t.l.v, t.r.v), c0 .v = max(t.v, c.v), t0 .c = c0 , where c selects whichever of t.l and t.r
contains the smaller value. Then pushDownHelper is invoked on t0 .c.
If t is a snapshot, then t0 is the result of allocating a new node (with snapshots of its children), otherwise
t is mutated directly. If c is a snapshot, then c0 is the result of allocating a new node (with snapshots of its
children), otherwise c is mutated directly. Thus Snapshot Isolation is preserved.
Assume that when the thread arrives at t, then t is Braun. Since no elements are added or removed, t0
is also Braun.
Assume that when the thread arrives at t, the subtrees rooted at its children are heaps. For children c, d ∈
t, we say without loss of generality that c.v ≤ d.v. After pushDownHelper, we have that t0 .v = min(c.v, t.v),
c0 .v = max(c.v, t.v), d0 .v = d.v. Thus t0 .v ≤ c0 .v and also t0 .v ≤ d0 .v, and inductively, pushDownHelper
restores heap.
Theorem 6 (Correctness of removeMin). removeMin happens atomically, and is Braun, Heap, and Snapshot
Isolation preserving.
Proof. After acquiring the heap, removeMin reentrantly acquires a double write-lock on the root, r.v, and
updates the root, s.t. r0 .v = r.pullUpLeftHelper. After this, removeMin executes r0 .pushDownHelper.
Since pullUpLeftHelper ensures that r0 is not a snapshot, the direct assignment to r0 .v does not violate
Snapshot Isolation.
Since the lock on the root is held continuously between starting pullUpLeftHelper and starting pushDownHelper,
removeMin is atomic.
Both subroutines are Braun preserving, so too is their composition.
Since pullUpLeftHelper is heap preserving, r0 .l and r0 .r are heaps, pushDownHelper is heap restoring,
and removeMin is heap preserving.

3

Performance

We benchmarked the concurrent Braun heap implementation of this paper against two other concurrent
priority queue implementations. PriorityBlockingQueue is part of the Java standard library, and its
methods use a simple mutex around a high-performance single-threaded binary heap implemented with
an array. [13] It provides no native snapshot operation; however, while the documentation asserts that its
iterator is “weakly consistent”, the source of the implementation reveals that it uses a mutex for a copy of the
backing array, which provides strong consistency. The provided constructor for efficient construction from
sorted bulk data is incompatible with the Iterator interface, so we implemented snapshot with successive
inserts from the iterator.
SkipListPriorityQueue is built on a variant of the Java standard library’s ConcurrentSkipListMap
that has been augmented with the lock-free linearizable iterator of Petrank and Timnat. [12, 17] Simple
5

CAS loops built on putIfAbsent, replace, and the key/value variant of remove were used to transform
the ordered map into an ordered multiset supporting insert and removeMin. Their iterator was augmented
with a wrapper to properly repeat each entry in the multiset. As with PriorityBlockingQueue, no native
snapshot was supported, and no compatible bulk data constructor was available, so we implemented it with
successive inserts from the iterator.

3.1

Methodology

The experiments were run on an Amazon EC2 “c4.8xlarge” instance, providing 36 virtual CPUs and 60GB of
RAM, using an Oracle Java 8 runtime. For each sequence of tests, the priority queue implementation being
benchmarked was initialized with a large quantity of random entries, then a series of warmup executions for
the VM’s JIT compiler were performed and discarded. Finally, each sequence of tests were performed and
the times recorded and averaged over a number of iterations. For experiments where the priority queues were
initialized with 220 entries, 20 warmup runs and 20 experimental runs were used. For experiments where
the priority queues were initialized with 225 entries, 1 warmup run and 5 experimental runs were used due
to the substantially longer execution times involved in the iterator tests.
In all, five tests were conducted, Insert, RemoveMin, Sum, Snap+Insert, and Snap-Only. For the Insert
tests, 1344 randomly selected values were inserted into the priority queue, with each of t threads responsible
for 1344/t insertions7 . Similarly, for the RemoveMin tests, each of t threads was tasked with executing
RemoveMin 1344/t times.
For the Sum tasks, all threads were tasked with individually calculating the sum of every entry in the
priority queue.
For the Snap+Insert tasks, each of t threads created its own snapshot and inserted 1344/t items, whereas
for the Snap-Only tasks, each of t threads merely had to instantiate a snapshot.

3.2

Results

In Figure 2, the results of the first 4 tests, on priority queues of size ≈ 220 , can be seen, plotted as
speedups over their single threaded executions. Generally, the concurrent Braun heaps scaled better than
PriorityBlockingQueue, except on the RemoveMin test, where both algorithms suffered from an O(log
n) traversal with a lock on the root, but PriorityBlockingQueue was able to fall back on a highly-tuned
single threaded algorithm backed by an array. On the tests involving iteration and snapshots, the concurrent Braun heap implementation scaled substantially better than either the SkipListPriorityQueue or the
PriorityBlockingQueue. Also of note is that Petrank and Timnat’s lock-free iterators appear to pay a
heavy performance penalty for linearizability in a lock-free setting without the benefit of fast snapshots.
For the four tests shown in Figure 2, the raw performance numbers were within small to moderate constant
factors of one another; however, with the Snap-Only test, plotted in Figure 3, the performance gap between
O(1) vs Ω(n) is fully evident in the execution times, in favor of the concurrent Braun heaps.
The Insert, RemoveMin, and Sum tests were repeated on priority queues of size ≈ 225 . The snapshotbased tests were excluded due to the prohibitive memory costs for the SkipList and Blocking implementations8 . For these tests, the performance scaling gaps already on display in earlier tests for Insert and Sum
were amplified; however, the results for RemoveMin now favored PriorityBlockingQueue. More comprehensive benchmarking is in order to better characterize this regime change.

4

Conclusion

The algorithm for concurrent Braun heaps presented here is both easily implemented9 and easily verified,
while providing competitive performance scaling on traditional priority queue operations, and supporting
7

This number was selected because it is a common multiple of the thread counts used, to avoid rounding error
SkipList implementation also had to be excluded from the final iterator test due to prohibitive execution times.
9 The ConcurrentSkipListMap source code, on which the competing SkipListPriorityQueue was based, has > 2000 lines of
code, whereas the BraunHeap in the appendix has < 200.
8 The

6

Multithreaded Speedup of RemoveMin Test (2^20)

Multithreaded Speedup of Insert Test (2^20)
1.8

7

1.6

6

Performance Scaling

Performance Scaling

1.4
5
4
3
2

1.2
1
0.8
0.6
0.4

1

0.2
0

0
0

4

8

12

16

20

24

28

0

32

4

8

12

16

20

24

28

32

28

32

Thread Count

Thread Count

Multithreaded Speedup of Snap+Insert Test (2^20)

Multithreaded Speedup of Sum Test (2^20)
4.5

1.2

4
3.5

Performance Scaling

Performance Scaling

1
0.8
0.6
0.4

3
2.5
2
1.5
1

0.2

0.5
0

0
0

4

8

12

16

20

24

28

0

32

4

8

12

16

20

24

Thread Count

Thread Count

Figure 2: Left to right, top to bottom, results from the Insert, RemoveMin, Sum, and Snap+Insert tests on priority
queues initialized with 220 elements.

Execution Time of Snapshot Test (2^20)
10000

Raw Execution Time

1000

100

10

1
0

4

8

12

16

20

24

28

32

0.1

0.01

Thread Count

Figure 3: Raw execution times of the Snap-Only test, plotted on a log scale. The legend coloring matches that of
Figure 2.

7

Multithreaded Speedup of Insert Test (2^25)

Multithreaded Speedup of RemoveMin Test (2^25)

10

Multithreaded Speedup of Sum Test (2^25)

2.5

1.2

9
1

6
5
4
3
2

Performance Scaling

2

7

Performance Scaling

Performance Scaling

8

1.5

1

0.5

0.8
0.6
0.4
0.2

1
0

0
1

6

11

16

21

26

31

0
1

Thread Count

6

11

16

Thread Count

21

26

31

1

6

11

16

21

26

31

Thread Count

Figure 4: Left to right, Insert, RemoveMin, and Sum tests on priority queues initialized with 225 elements. The
legend coloring matches that of Figure 2.

fast and consistent snapshots and iteration.

4.1

Future Work

This work leads naturally to several future lines of inquiry. Much of the algorithm lends itself to a lock-free
approach based on CAS instructions; however, the two phases of the removeMin operation leave the heap in
an inconsistent state if not executed atomically. An efficient approach to speculative executions of removeMin
would be a useful next step in developing a lock-free concurrent Braun heap (there is also a fairness concern
here, as long-running remove operations may consistently get delayed by fast inserts).
The experimental results of this paper also suggest that without fast snapshots, iteration over concurrent
data structures is inordinately expensive due to synchronization costs. Another direction would be to address
this by leveraging copy-on-write semantics in the pursuit of efficient snapshot operations for other data
structures, potentially including skip lists.

References
[1] Timos Antonopoulos, Paul Gazzillo, Eric Koskinen, and Zhong Shao. A theory of vertically composable
transactional objects.
[2] Henry C Baker Jr and Carl Hewitt. The incremental garbage collection of processes. In ACM Sigplan
Notices, volume 12, pages 55–59. ACM, 1977.
[3] Hal Berenson, Phil Bernstein, Jim Gray, Jim Melton, Elizabeth O’Neil, and Patrick O’Neil. A critique
of ansi sql isolation levels. In ACM SIGMOD Record, volume 24, pages 1–10. ACM, 1995.
[4] W Braun and M Rem. A logarithmic implementation of flexible arrays. Memorandum MR83/4, Eindhoven University of Technology, 1983.
[5] Nathan G Bronson, Jared Casper, Hassan Chafi, and Kunle Olukotun. A practical concurrent binary
search tree. In ACM Sigplan Notices, volume 45, pages 257–268. ACM, 2010.
[6] Khuzaima Daudjee and Kenneth Salem. Lazy database replication with snapshot isolation. In Proceedings of the 32nd international conference on Very large data bases, pages 715–726. VLDB Endowment,
2006.
[7] Thomas D Dickerson, Paul Gazzillo, Eric Koskinen, and Maurice Herlihy. Proust: A design space for
highly-concurrent transactional data structures. arXiv preprint arXiv:1702.04866, 2017.
[8] Daniel P Friedman and David Stephen Wise. The impact of applicative programming on multiprocessing.
Indiana University, Computer Science Department, 1976.

8

[9] Maurice Herlihy, Yossi Lev, Victor Luchangco, and Nir Shavit. A simple optimistic skiplist algorithm.
In International Colloquium on Structural Information and Communication Complexity, pages 124–138.
Springer, 2007.
[10] Maurice P Herlihy and Jeannette M Wing. Linearizability: A correctness condition for concurrent
objects. ACM Transactions on Programming Languages and Systems (TOPLAS), 12(3):463–492, 1990.
[11] Rob Hoogerwoord. A logarithmic implementation of flexible arrays. In Mathematics of Program Construction, pages 191–207. Springer, 1993.
[12] Doug Lea. Concurrent skip list map. java.util.concurrent.
[13] Doug Lea. Priority blocking queue. java.util.concurrent.
[14] Tobias Nipkow. Priority queues based on braun trees. 2016.
[15] Chris Okasaki. Purely functional data structures. PhD thesis, Princeton University, 1996.
[16] Chris Okasaki. Three algorithms on braun trees. Journal of Functional Programming, 7(06):661–666,
1997.
[17] Erez Petrank and Shahar Timnat. Lock-free data-structure iterators. In International Symposium on
Distributed Computing, pages 224–238. Springer, 2013.
[18] Aleksandar Prokopec, Nathan Grasso Bronson, Phil Bagwell, and Martin Odersky. Concurrent tries
with efficient non-blocking snapshots. In Acm Sigplan Notices, volume 47, pages 151–160. ACM, 2012.
[19] William Pugh. Concurrent maintenance of skip lists. 1990.
[20] Nir Shavit and Itay Lotan. Skiplist-based concurrent priority queues. In Parallel and Distributed
Processing Symposium, 2000. IPDPS 2000. Proceedings. 14th International, pages 263–268. IEEE, 2000.

A

Extended source code listings

9

class HeapNode[E:Ordering](var v: E, var left:HeapNode[E] = null,
var right:HeapNode[E] = null){
def update(nV:E, nL:HeapNode[E], nR:HeapNode[E]):HeapNode[E] = {
v = nV; left = nL; right = nr; this
}
def insertHelper(nV: E):HeapNode[E] = {
val (smaller, larger) = if(nV < v) { (nV, v) } else { (v, nV) }
update(smaller, if(right == null) { new HeapNode(larger) }
else { right.insertHelper(larger) }, left)
}
def pullUpLeftHelper(selfP:HeapNode[E] => Unit, isRoot:Boolean = false):Option[E] = {
if(left == null) {
selfP(null)
if(root == this) { None }
else { Some(value) }
} else {
update(v, right, left)
right.pullUpLeftHelper(right_= _)
}
}
def pushDownHelper:Unit = {
val pushNext = if(right == null && left != null) {
val tmpLV = left.v
if(tmpLV < v){ left.update(v); v = tmpLV }
null
} else if(right != null) {
val (tmpV, tmpLV, tmpRV) = (v, left.v, right.v)
(if(tmpLV < tmpV || tmpRV < tmpV) { v = tmpLV; left }
else { v = tmpRV; right }).update(tmpV)
} else { null }
if(pushNext != null) pushNext.pushDownHelper
}
}
class BraunHeap[E:Ordering] {
var root:HeapNode[E] = null
def getMin:Option[E] = Option(root).map(_.v)
def insert(v:E):Unit = if(root == null){ root = new HeapNode(v) }
else { root.insertHelper(v) }
def removeMin:Option[E] = getMin.map{ oldV =>
root.pullUpLeftHelper(root_= _, true).foreach{ newV =>
root.v = newV; root.pushDownHelper
}
oldV
}
}
Listing 1: Sequential Logic for a Braun Heap

10

def throughLock[N,Z](fL:N => Lock)(fZ:N => Z)(n:N):Z = {
val l = fL(n); l.lock;
val r = fZ(n); l.unlock; r
}
def withNullPath[E:Ordering,Z](v:HeapNode[E], nP: => Z, nnP: HeapNode[E] => Z):Z = {
if(v == null) nP else nnP(v)
}
def dfsIfNotNull[E:Ordering](n:HeapNode[E]):Stream[E] = {
withNullPath(n,Stream.Empty,{node => node.busyRead.lock; node.dfs})
}
def snapshotIfNotNull[E:Ordering](n:HeapNode[E]):HeapNode[E] = {
withNullPath(n, null, _.snapshot)
}
def lockPair:(WriteLock, ReadLock) = {
val r = new ReentrantReadWriteLock; (r.writeLock, r.readLock)
}
def ensureWriteable[E:Ordering](n:HeapNode[E], mut:HeapNode[E]=>Unit, v:E):HeapNode[E] = {
val ret = withNullPath(n,new HeapNode(v),{
concrete:HeapNode[E] =>
concrete.busyWrite.lock
concrete.unsnap
}); mut(ret); ret
}
Listing 2: Shared helper methods for the concurrent BraunHeap and HeapNode classes.

@volatile var snapCount = 0
val (busyWrite, busyRead) = lockPair
def snapshot:HeapNode[E] = {
busyWrite.lock; snapCount += 1; busyWrite.unlock; this
}
def unsnapHelper:HeapNode[E] = {
if(snapCount > 0){
val nV = value
val nL = snapshotIfNotNull(left)
val nR = snapshotIfNotNull(right)
snapCount -= 1; val ret = new HeapNode(nV, nL, nR)
ret.busyWrite.lock; busyWrite.unlock; ret
} else { this }
}
def unsnap:HeapNode[E] = {
throughLock[HeapNode[E],HeapNode[E]](_.busyRead)(_.unsnapHelper)(this)
}
private[BraunHeap] def dfs:Stream[E] = {
busyRead.unlock; ((value #:: dfsIfNotNull(left)) #::: dfsIfNotNull(right))
}
Listing 3: The concurrent HeapNode is augmented with a COW-based snapshot functionality.

11

class HeapNode[E:Ordering](@volatile var v:E, @volatile var left:HeapNode[E],
@volatile var right:HeapNode[E]){
def updateHelper(nV:E, nL:HeapNode[E], nR:HeapNode[E]):HeapNode[E] = {
v = nV; left = nL; right = nR; this
}
def update(nV:E, nL:HeapNode[E], nR:HeapNode[E]):HeapNode[E] = {
unsnap.updateHelper(nV, nL, nR)
}
def insertHelper(nV:E):Unit = {
val (smaller, larger) = if(nV < v){ (nV, v) } else { (v, nV) }
val insertNeeded = (right != null)
update(smaller, ensureWriteable(right, right_=, larger),
left).insertHelperPost(insertNeeded, larger)
}
def insertHelperPost(insertNeeded:Boolean, larger:E):Unit = {
if(insertNeeded){
val leftNow = left; busyWrite.unlock; leftNow.insertHelper(larger)
} else { busyWrite.unlock }
}
def pullupLeftHelper(selfP:HeapNode[E]=>Unit, amRoot:Boolean = false):Option[E] = {
withNullPath(left,{
selfP(null); if(snapCount > 0) { snapCount -= 1 } busyWrite.unlock
if(amRoot){ busyWrite.unlock; None } else { Some(v) }
},update(v, right, _).pullupLeftHelperPost(selfRef))
}
def pullupLeftHelperPost(selfP:HeapNode[E]=>Unit):Option[E] = {
selfP(this); right.busyWrite.lock; busyWrite.unlock
right.pullupLeftHelper(right_=)
}
def pushdownHelper:Unit = {
val pushNext = if(right == null && left != null){
val tmpLV = left.v
if(tmpLV < v){
left.busyWrite.lock; left = left.update(v)
left.busyWrite.unlock; value = tmpLV
} null
} else if(right != null) {
val (tmpV, tmpLV, tmpRV) = (v, left.value, right.value)
if((tmpLV < tmpV) || (tmpRV < tmpV)){
val (nV, nU, nP) = if(tmpLV < tmpRV) {
(tmpLV, left_= _, left)
} else { (tmpRV, right_= _, right) }
v = nV; nP.busyWrite.lock; val ret = nP.update(tmpV); nU(ret); ret
} else { null }
} else { null }
busyWrite.unlock
if(pushNext != null) pushNext.pushdownHelper
}
}
Listing 4: The core BraunHeap algorithm is unchanged by hand-over-hand locking. Some methods have been
refactored to respect COW boundaries.

12

class BraunHeap[E:Ordering] {
@volatile var root:HeapNode[E] = initRoot
val (busyWrite, busyRead) = lockPair
def syncReadRoot:HeapNode[E] = throughLock(_.busyRead)(_.root)(this)
def snapshot:BraunHeap[E] = {
new BraunHeap[E](snapshotIfNotNull(syncReadRoot))
}
def iterator:Iterator[E] = dfsIfNotNull(snapshot.root).iterator
def getMin:Option[E] = {
Option(syncReadRoot).map{throughLock(_.busyRead)(_.value) _}
}
def insertPre(value:E):Boolean = {
val insertNeeded = (root != null)
ensureWriteable(root, root_=, value)
insertNeeded
}
def insert(value:E):Unit = {
if(throughLock[BraunHeap[E],Boolean](_.busyWrite){
_.insertPre(value)
}(this)){ root.insertHelper(value) }
}
def removeMinPre:(Option[E], Option[E]) = {
withNullPath(root,(None, None),{
rootInit:HeapNode[E] =>
rootInit.busyWrite.lock
root = rootInit.unsnap
root.busyWrite.lock
val oldRoot = Some(root.value);
val newRoot = root.pullupLeftHelper(root_=, true)
(oldRoot, newRoot)
})
}
def removeMin:Option[E] = {
val (ret, newRoot) = throughLock(_.busyWrite){_.removeMinPre}(this)
if(ret.isDefined && newRoot.isDefined){
root.value = newRoot.get
root.pushdownHelper
} ret
}
}
Listing 5: The core BraunHeap algorithm is unchanged by hand-over-hand locking.

13

