8000 Immutable TreeMap/TreeSet performance (SI-5331) by erikrozendaal · Pull Request #82 · scala/scala · GitHub
[go: up one dir, main page]

Skip to content

Immutable TreeMap/TreeSet performance (SI-5331) #82

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.

8000 Already on GitHub? Sign in to your account

Merged
merged 37 commits into from
Feb 16, 2012
Merged
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
540ad02
Use RedBlack.iterator to create iterators for TreeSet/TreeMap.
erikrozendaal Dec 17, 2011
88ed930
Use custom implementation for iterating over RedBlack trees. Raw
erikrozendaal Dec 17, 2011
edcec03
Optimized implementations of head/headOption/last/lastOption for
erikrozendaal Dec 17, 2011
9cdede8
Optimized implementation of init/tail for TreeSet/TreeMap.
erikrozendaal Dec 17, 2011
b7e6714
RedBlack.scala: Change count from 'def' to 'val' in NonEmpty tree
erikrozendaal Dec 17, 2011
95cb7bc
Implemented drop/take/slice/splitAt/dropRight/takeRight for
erikrozendaal Dec 18, 2011
8d67823
Implemented takeWhile/dropWhile/span to use tree splitting. This
erikrozendaal Dec 18, 2011
3f66061
Switched from isSmaller to ordering.
erikrozendaal Dec 19, 2011
7ec9b0b
Moved from implicit ordering value to implicit parameter.
erikrozendaal Dec 19, 2011
a02a815
Moved from Empty case object to case class in preparation of moving
erikrozendaal Dec 19, 2011
418adc6
Moved type parameter A from RedBlack to Tree.
erikrozendaal Dec 19, 2011
6c0e036
Changed abstract class RedBlack to singleton object.
erikrozendaal Dec 19, 2011
d2706db
Use single shared Empty instance across all RedBlack trees.
erikrozendaal Dec 19, 2011
6b95074
Make sure the redblack test compiles and runs.
erikrozendaal Dec 21, 2011
b9699f9
Made RedBlack private to the scala.collection.immutable package.
erikrozendaal Dec 27, 2011
32171c2
TreeMap/TreeSet no longer keep track of the size (the RedBlack tree
erikrozendaal Dec 27, 2011
b421bba
Performance improvements for iteration (foreach and iterator).
erikrozendaal Dec 27, 2011
4a0c4bb
Improved performance of RedBlack.NonEmpty.nth (helps take/drop/split/…
erikrozendaal Dec 27, 2011
ad0b09c
Added some tests for TreeMap/TreeSet.
erikrozendaal Dec 28, 2011
c51bdea
Minimize number of calls to ordering.
erikrozendaal Dec 28, 2011
6d8dca7
Moved key/value/left/right fields up to NonEmpty class. Don't rely
erikrozendaal Jan 2, 2012
82374ad
Implemented deletes without pattern matching.
erikrozendaal Jan 2, 2012
3dea251
Implemented range without using pattern matching.
erikrozendaal Jan 2, 2012
5c05f66
Use null to represent empty trees. Removed Empty/NonEmpty classes.
erikrozendaal Jan 2, 2012
72ec0ac
Optimize foreach and iterators.
erikrozendaal Jan 4, 2012
d735d0f
Move nth method to RedBlack. Inline factories for tree nodes.
erikrozendaal Jan 5, 2012
388ff47
Add implementation notes. Consistently use eq/ne to compare with null.
erikrozendaal Jan 5, 2012
7e92b3c
Deprecate TreeMap.isSmaller and TreeSet.isSmaller.
erikrozendaal Jan 6, 2012
f656142
Restore old RedBlack class to maintain backwards compatibility.
erikrozendaal Jan 6, 2012
288874d
Renamed object RedBlack to RedBlackTree.
erikrozendaal Jan 7, 2012
e61075c
Tests for takeWhile/dropWhile/span.
erikrozendaal Jan 7, 2012
8b3f984
Fix silly copy-paste error.
erikrozendaal Jan 7, 2012
f26f610
Test for maximum height of red-black tree.
erikrozendaal Jan 8, 2012
00b5cb8
Optimized implementation of TreeMap/TreeSet#to method.
erikrozendaal Jan 15, 2012
7824dbd
Custom coded version of range/from/to/until.
erikrozendaal Jan 21, 2012
78374f3
Custom implementations of drop/take/slice.
erikrozendaal Jan 22, 2012
51667dc
Removed TODOs.
erikrozendaal Jan 24, 2012
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8000
Prev Previous commit
Next Next commit
Add implementation notes. Consistently use eq/ne to compare with null.
  • Loading branch information
erikrozendaal committed Jan 5, 2012
commit 388ff4716f9f4162165221c42fb2f2aa83e1063c
31 changes: 24 additions & 7 deletions src/library/scala/collection/immutable/RedBlack.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ import annotation.tailrec
import annotation.meta.getter

/** An object containing the RedBlack tree implementation used by for `TreeMaps` and `TreeSets`.
*
* Implementation note: since efficiency is important for data structures this implementation
* uses <code>null</code> to represent empty trees. This also means pattern matching cannot
* easily be used. The API represented by the RedBlack object tries to hide these optimizations
* behind a reasonably clean API.
*
* @since 2.3
*/
Expand Down Expand Up @@ -103,7 +108,7 @@ object RedBlack {
else
mkTree(isBlack, x, xv, a, r)
}
private[this] def upd[A, B, B1 >: B](tree: Tree[A, B], k: A, v: B1)(implicit ordering: Ordering[A]): Tree[A, B1] = if (tree == null) {
private[this] def upd[A, B, B1 >: B](tree: Tree[A, B], k: A, v: B1)(implicit ordering: Ordering[A]): Tree[A, B1] = if (tree eq null) {
RedTree(k, v, null, null)
} else {
val cmp = ordering.compare(k, tree.key)
Expand All @@ -114,7 +119,7 @@ object RedBlack {

// Based on Stefan Kahrs' Haskell version of Okasaki's Red&Black Trees
// http://www.cse.unsw.edu.au/~dons/data/RedBlackTree.html
private[this] def del[A, B](tree: Tree[A, B], k: A)(implicit ordering: Ordering[A]): Tree[A, B] = if (tree == null) null else {
private[this] def del[A, B](tree: Tree[A, B], k: A)(implicit ordering: Ordering[A]): Tree[A, B] = if (tree eq null) null else {
def balance(x: A, xv: B, tl: Tree[A, B], tr: Tree[A, B]) = if (isRedTree(tl)) {
if (isRedTree(tr)) {
RedTree(x, xv, tl.black, tr.black)
Expand Down Expand Up @@ -287,6 +292,15 @@ object RedBlack {
}
}

/*
* Forcing direct fields access using the @inline annotation helps speed up
* various operations (especially smallest/greatest and update/delete).
*
* Unfortunately the direct field access is not guaranteed to work (but
* works on the current implementation of the Scala compiler).
*
* An alternative is to implement the these classes using plain old Java code...
*/
sealed abstract class Tree[A, +B](
@(inline @getter) final val key: A,
@(inline @getter) final val value: B,
Expand Down Expand Up @@ -355,11 +369,14 @@ object RedBlack {
index += 1
} catch {
case _: ArrayIndexOutOfBoundsException =>
// Either the tree became unbalanced or we calculated the maximum height incorrectly.
// To avoid crashing the iterator we expand the path array. Obviously this should never
// happen...
//
// An exception handler is used instead of an if-condition to optimize the normal path.
/*
* Either the tree became unbalanced or we calculated the maximum height incorrectly.
* To avoid crashing the iterator we expand the path array. Obviously this should never
* happen...
*
* An exception handler is used instead of an if-condition to optimize the normal path.
* This makes a large difference in iteration speed!
*/
assert(index >= path.length)
path :+= null
pushPath(tree)
Expand Down
0