michael@0: // Copyright 2009 the V8 project authors. All rights reserved. michael@0: // Redistribution and use in source and binary forms, with or without michael@0: // modification, are permitted provided that the following conditions are michael@0: // met: michael@0: // michael@0: // * Redistributions of source code must retain the above copyright michael@0: // notice, this list of conditions and the following disclaimer. michael@0: // * Redistributions in binary form must reproduce the above michael@0: // copyright notice, this list of conditions and the following michael@0: // disclaimer in the documentation and/or other materials provided michael@0: // with the distribution. michael@0: // * Neither the name of Google Inc. nor the names of its michael@0: // contributors may be used to endorse or promote products derived michael@0: // from this software without specific prior written permission. michael@0: // michael@0: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS michael@0: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT michael@0: // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR michael@0: // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT michael@0: // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, michael@0: // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT michael@0: // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE michael@0: // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: michael@0: // This benchmark is based on a JavaScript log processing module used michael@0: // by the V8 profiler to generate execution time profiles for runs of michael@0: // JavaScript applications, and it effectively measures how fast the michael@0: // JavaScript engine is at allocating nodes and reclaiming the memory michael@0: // used for old nodes. Because of the way splay trees work, the engine michael@0: // also has to deal with a lot of changes to the large tree object michael@0: // graph. michael@0: michael@0: var Splay = new BenchmarkSuite('Splay', 81491, [ michael@0: new Benchmark("Splay", SplayRun, SplaySetup, SplayTearDown) michael@0: ]); michael@0: michael@0: michael@0: // Configuration. michael@0: var kSplayTreeSize = 8000; michael@0: var kSplayTreeModifications = 80; michael@0: var kSplayTreePayloadDepth = 5; michael@0: michael@0: var splayTree = null; michael@0: michael@0: michael@0: function GeneratePayloadTree(depth, tag) { michael@0: if (depth == 0) { michael@0: return { michael@0: array : [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], michael@0: string : 'String for key ' + tag + ' in leaf node' michael@0: }; michael@0: } else { michael@0: return { michael@0: left: GeneratePayloadTree(depth - 1, tag), michael@0: right: GeneratePayloadTree(depth - 1, tag) michael@0: }; michael@0: } michael@0: } michael@0: michael@0: michael@0: function GenerateKey() { michael@0: // The benchmark framework guarantees that Math.random is michael@0: // deterministic; see base.js. michael@0: return Math.random(); michael@0: } michael@0: michael@0: michael@0: function InsertNewNode() { michael@0: // Insert new node with a unique key. michael@0: var key; michael@0: do { michael@0: key = GenerateKey(); michael@0: } while (splayTree.find(key) != null); michael@0: var payload = GeneratePayloadTree(kSplayTreePayloadDepth, String(key)); michael@0: splayTree.insert(key, payload); michael@0: return key; michael@0: } michael@0: michael@0: michael@0: michael@0: function SplaySetup() { michael@0: splayTree = new SplayTree(); michael@0: for (var i = 0; i < kSplayTreeSize; i++) InsertNewNode(); michael@0: } michael@0: michael@0: michael@0: function SplayTearDown() { michael@0: // Allow the garbage collector to reclaim the memory michael@0: // used by the splay tree no matter how we exit the michael@0: // tear down function. michael@0: var keys = splayTree.exportKeys(); michael@0: splayTree = null; michael@0: michael@0: // Verify that the splay tree has the right size. michael@0: var length = keys.length; michael@0: if (length != kSplayTreeSize) { michael@0: throw new Error("Splay tree has wrong size"); michael@0: } michael@0: michael@0: // Verify that the splay tree has sorted, unique keys. michael@0: for (var i = 0; i < length - 1; i++) { michael@0: if (keys[i] >= keys[i + 1]) { michael@0: throw new Error("Splay tree not sorted"); michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: function SplayRun() { michael@0: // Replace a few nodes in the splay tree. michael@0: for (var i = 0; i < kSplayTreeModifications; i++) { michael@0: var key = InsertNewNode(); michael@0: var greatest = splayTree.findGreatestLessThan(key); michael@0: if (greatest == null) splayTree.remove(key); michael@0: else splayTree.remove(greatest.key); michael@0: } michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Constructs a Splay tree. A splay tree is a self-balancing binary michael@0: * search tree with the additional property that recently accessed michael@0: * elements are quick to access again. It performs basic operations michael@0: * such as insertion, look-up and removal in O(log(n)) amortized time. michael@0: * michael@0: * @constructor michael@0: */ michael@0: function SplayTree() { michael@0: }; michael@0: michael@0: michael@0: /** michael@0: * Pointer to the root node of the tree. michael@0: * michael@0: * @type {SplayTree.Node} michael@0: * @private michael@0: */ michael@0: SplayTree.prototype.root_ = null; michael@0: michael@0: michael@0: /** michael@0: * @return {boolean} Whether the tree is empty. michael@0: */ michael@0: SplayTree.prototype.isEmpty = function() { michael@0: return !this.root_; michael@0: }; michael@0: michael@0: michael@0: /** michael@0: * Inserts a node into the tree with the specified key and value if michael@0: * the tree does not already contain a node with the specified key. If michael@0: * the value is inserted, it becomes the root of the tree. michael@0: * michael@0: * @param {number} key Key to insert into the tree. michael@0: * @param {*} value Value to insert into the tree. michael@0: */ michael@0: SplayTree.prototype.insert = function(key, value) { michael@0: if (this.isEmpty()) { michael@0: this.root_ = new SplayTree.Node(key, value); michael@0: return; michael@0: } michael@0: // Splay on the key to move the last node on the search path for michael@0: // the key to the root of the tree. michael@0: this.splay_(key); michael@0: if (this.root_.key == key) { michael@0: return; michael@0: } michael@0: var node = new SplayTree.Node(key, value); michael@0: if (key > this.root_.key) { michael@0: node.left = this.root_; michael@0: node.right = this.root_.right; michael@0: this.root_.right = null; michael@0: } else { michael@0: node.right = this.root_; michael@0: node.left = this.root_.left; michael@0: this.root_.left = null; michael@0: } michael@0: this.root_ = node; michael@0: }; michael@0: michael@0: michael@0: /** michael@0: * Removes a node with the specified key from the tree if the tree michael@0: * contains a node with this key. The removed node is returned. If the michael@0: * key is not found, an exception is thrown. michael@0: * michael@0: * @param {number} key Key to find and remove from the tree. michael@0: * @return {SplayTree.Node} The removed node. michael@0: */ michael@0: SplayTree.prototype.remove = function(key) { michael@0: if (this.isEmpty()) { michael@0: throw Error('Key not found: ' + key); michael@0: } michael@0: this.splay_(key); michael@0: if (this.root_.key != key) { michael@0: throw Error('Key not found: ' + key); michael@0: } michael@0: var removed = this.root_; michael@0: if (!this.root_.left) { michael@0: this.root_ = this.root_.right; michael@0: } else { michael@0: var right = this.root_.right; michael@0: this.root_ = this.root_.left; michael@0: // Splay to make sure that the new root has an empty right child. michael@0: this.splay_(key); michael@0: // Insert the original right child as the right child of the new michael@0: // root. michael@0: this.root_.right = right; michael@0: } michael@0: return removed; michael@0: }; michael@0: michael@0: michael@0: /** michael@0: * Returns the node having the specified key or null if the tree doesn't contain michael@0: * a node with the specified key. michael@0: * michael@0: * @param {number} key Key to find in the tree. michael@0: * @return {SplayTree.Node} Node having the specified key. michael@0: */ michael@0: SplayTree.prototype.find = function(key) { michael@0: if (this.isEmpty()) { michael@0: return null; michael@0: } michael@0: this.splay_(key); michael@0: return this.root_.key == key ? this.root_ : null; michael@0: }; michael@0: michael@0: michael@0: /** michael@0: * @return {SplayTree.Node} Node having the maximum key value. michael@0: */ michael@0: SplayTree.prototype.findMax = function(opt_startNode) { michael@0: if (this.isEmpty()) { michael@0: return null; michael@0: } michael@0: var current = opt_startNode || this.root_; michael@0: while (current.right) { michael@0: current = current.right; michael@0: } michael@0: return current; michael@0: }; michael@0: michael@0: michael@0: /** michael@0: * @return {SplayTree.Node} Node having the maximum key value that michael@0: * is less than the specified key value. michael@0: */ michael@0: SplayTree.prototype.findGreatestLessThan = function(key) { michael@0: if (this.isEmpty()) { michael@0: return null; michael@0: } michael@0: // Splay on the key to move the node with the given key or the last michael@0: // node on the search path to the top of the tree. michael@0: this.splay_(key); michael@0: // Now the result is either the root node or the greatest node in michael@0: // the left subtree. michael@0: if (this.root_.key < key) { michael@0: return this.root_; michael@0: } else if (this.root_.left) { michael@0: return this.findMax(this.root_.left); michael@0: } else { michael@0: return null; michael@0: } michael@0: }; michael@0: michael@0: michael@0: /** michael@0: * @return {Array<*>} An array containing all the keys of tree's nodes. michael@0: */ michael@0: SplayTree.prototype.exportKeys = function() { michael@0: var result = []; michael@0: if (!this.isEmpty()) { michael@0: this.root_.traverse_(function(node) { result.push(node.key); }); michael@0: } michael@0: return result; michael@0: }; michael@0: michael@0: michael@0: /** michael@0: * Perform the splay operation for the given key. Moves the node with michael@0: * the given key to the top of the tree. If no node has the given michael@0: * key, the last node on the search path is moved to the top of the michael@0: * tree. This is the simplified top-down splaying algorithm from: michael@0: * "Self-adjusting Binary Search Trees" by Sleator and Tarjan michael@0: * michael@0: * @param {number} key Key to splay the tree on. michael@0: * @private michael@0: */ michael@0: SplayTree.prototype.splay_ = function(key) { michael@0: if (this.isEmpty()) { michael@0: return; michael@0: } michael@0: // Create a dummy node. The use of the dummy node is a bit michael@0: // counter-intuitive: The right child of the dummy node will hold michael@0: // the L tree of the algorithm. The left child of the dummy node michael@0: // will hold the R tree of the algorithm. Using a dummy node, left michael@0: // and right will always be nodes and we avoid special cases. michael@0: var dummy, left, right; michael@0: dummy = left = right = new SplayTree.Node(null, null); michael@0: var current = this.root_; michael@0: while (true) { michael@0: if (key < current.key) { michael@0: if (!current.left) { michael@0: break; michael@0: } michael@0: if (key < current.left.key) { michael@0: // Rotate right. michael@0: var tmp = current.left; michael@0: current.left = tmp.right; michael@0: tmp.right = current; michael@0: current = tmp; michael@0: if (!current.left) { michael@0: break; michael@0: } michael@0: } michael@0: // Link right. michael@0: right.left = current; michael@0: right = current; michael@0: current = current.left; michael@0: } else if (key > current.key) { michael@0: if (!current.right) { michael@0: break; michael@0: } michael@0: if (key > current.right.key) { michael@0: // Rotate left. michael@0: var tmp = current.right; michael@0: current.right = tmp.left; michael@0: tmp.left = current; michael@0: current = tmp; michael@0: if (!current.right) { michael@0: break; michael@0: } michael@0: } michael@0: // Link left. michael@0: left.right = current; michael@0: left = current; michael@0: current = current.right; michael@0: } else { michael@0: break; michael@0: } michael@0: } michael@0: // Assemble. michael@0: left.right = current.left; michael@0: right.left = current.right; michael@0: current.left = dummy.right; michael@0: current.right = dummy.left; michael@0: this.root_ = current; michael@0: }; michael@0: michael@0: michael@0: /** michael@0: * Constructs a Splay tree node. michael@0: * michael@0: * @param {number} key Key. michael@0: * @param {*} value Value. michael@0: */ michael@0: SplayTree.Node = function(key, value) { michael@0: this.key = key; michael@0: this.value = value; michael@0: }; michael@0: michael@0: michael@0: /** michael@0: * @type {SplayTree.Node} michael@0: */ michael@0: SplayTree.Node.prototype.left = null; michael@0: michael@0: michael@0: /** michael@0: * @type {SplayTree.Node} michael@0: */ michael@0: SplayTree.Node.prototype.right = null; michael@0: michael@0: michael@0: /** michael@0: * Performs an ordered traversal of the subtree starting at michael@0: * this SplayTree.Node. michael@0: * michael@0: * @param {function(SplayTree.Node)} f Visitor function. michael@0: * @private michael@0: */ michael@0: SplayTree.Node.prototype.traverse_ = function(f) { michael@0: var current = this; michael@0: while (current) { michael@0: var left = current.left; michael@0: if (left) left.traverse_(f); michael@0: f(current); michael@0: current = current.right; michael@0: } michael@0: };