1 // Copyright 2009 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 // This benchmark is based on a JavaScript log processing module used
29 // by the V8 profiler to generate execution time profiles for runs of
30 // JavaScript applications, and it effectively measures how fast the
31 // JavaScript engine is at allocating nodes and reclaiming the memory
32 // used for old nodes. Because of the way splay trees work, the engine
33 // also has to deal with a lot of changes to the large tree object
36 //var Splay = new BenchmarkSuite('Splay', 126125, [
37 // new Benchmark("Splay", SplayRun, SplaySetup, SplayTearDown)
40 // This is the best random number generator available to mankind ;)
44 // Robert Jenkins' 32 bit integer hash function.
45 this.seed = ((this.seed + 0x7ed55d16) + (this.seed << 12)) & 0xffffffff;
46 this.seed = ((this.seed ^ 0xc761c23c) ^ (this.seed >>> 19)) & 0xffffffff;
47 this.seed = ((this.seed + 0x165667b1) + (this.seed << 5)) & 0xffffffff;
48 this.seed = ((this.seed + 0xd3a2646c) ^ (this.seed << 9)) & 0xffffffff;
49 this.seed = ((this.seed + 0xfd7046c5) + (this.seed << 3)) & 0xffffffff;
50 this.seed = ((this.seed ^ 0xb55a4f09) ^ (this.seed >>> 16)) & 0xffffffff;
51 return (this.seed & 0xfffffff) / 0x10000000;
56 var kSplayTreeSize = 8000;
57 var kSplayTreeModifications = 80;
58 var kSplayTreePayloadDepth = 5;
63 function GeneratePayloadTree(depth, key) {
66 array : [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
67 string : 'String for key ' + key + ' in leaf node'
71 left: GeneratePayloadTree(depth - 1, key),
72 right: GeneratePayloadTree(depth - 1, key)
78 function GenerateKey() {
79 // The benchmark framework guarantees that Math.random is
80 // deterministic; see base.js.
81 // base.js isn't pulled in for jit-tests
82 return MyMath.random();
86 function InsertNewNode() {
87 // Insert new node with a unique key.
91 } while (splayTree.find(key) != null);
92 splayTree.insert(key, GeneratePayloadTree(kSplayTreePayloadDepth, key));
98 function SplaySetup() {
99 splayTree = new SplayTree();
100 for (var i = 0; i < kSplayTreeSize; i++) InsertNewNode();
104 function SplayTearDown() {
105 // Allow the garbage collector to reclaim the memory
106 // used by the splay tree no matter how we exit the
107 // tear down function.
108 var keys = splayTree.exportKeys();
111 // Verify that the splay tree has the right size.
112 var length = keys.length;
113 assertEq(length, kSplayTreeSize);
115 // Verify that the splay tree has sorted, unique keys.
116 for (var i = 0; i < length - 1; i++) {
117 assertEq(keys[i] < keys[i + 1], true);
122 function SplayRun() {
123 // Replace a few nodes in the splay tree.
124 for (var i = 0; i < kSplayTreeModifications; i++) {
125 var key = InsertNewNode();
126 var greatest = splayTree.findGreatestLessThan(key);
127 if (greatest == null) splayTree.remove(key);
128 else splayTree.remove(greatest.key);
134 * Constructs a Splay tree. A splay tree is a self-balancing binary
135 * search tree with the additional property that recently accessed
136 * elements are quick to access again. It performs basic operations
137 * such as insertion, look-up and removal in O(log(n)) amortized time.
141 function SplayTree() {
146 * Pointer to the root node of the tree.
148 * @type {SplayTree.Node}
151 SplayTree.prototype.root_ = null;
155 * @return {boolean} Whether the tree is empty.
157 SplayTree.prototype.isEmpty = function() {
163 * Inserts a node into the tree with the specified key and value if
164 * the tree does not already contain a node with the specified key. If
165 * the value is inserted, it becomes the root of the tree.
167 * @param {number} key Key to insert into the tree.
168 * @param {*} value Value to insert into the tree.
170 SplayTree.prototype.insert = function(key, value) {
171 if (this.isEmpty()) {
172 this.root_ = new SplayTree.Node(key, value);
175 // Splay on the key to move the last node on the search path for
176 // the key to the root of the tree.
178 if (this.root_.key == key) {
181 var node = new SplayTree.Node(key, value);
182 if (key > this.root_.key) {
183 node.left = this.root_;
184 node.right = this.root_.right;
185 this.root_.right = null;
187 node.right = this.root_;
188 node.left = this.root_.left;
189 this.root_.left = null;
196 * Removes a node with the specified key from the tree if the tree
197 * contains a node with this key. The removed node is returned. If the
198 * key is not found, an exception is thrown.
200 * @param {number} key Key to find and remove from the tree.
201 * @return {SplayTree.Node} The removed node.
203 SplayTree.prototype.remove = function(key) {
204 if (this.isEmpty()) {
205 throw Error('Key not found: ' + key);
208 if (this.root_.key != key) {
209 throw Error('Key not found: ' + key);
211 var removed = this.root_;
212 if (!this.root_.left) {
213 this.root_ = this.root_.right;
215 var right = this.root_.right;
216 this.root_ = this.root_.left;
217 // Splay to make sure that the new root has an empty right child.
219 // Insert the original right child as the right child of the new
221 this.root_.right = right;
228 * Returns the node having the specified key or null if the tree doesn't contain
229 * a node with the specified key.
231 * @param {number} key Key to find in the tree.
232 * @return {SplayTree.Node} Node having the specified key.
234 SplayTree.prototype.find = function(key) {
235 if (this.isEmpty()) {
239 return this.root_.key == key ? this.root_ : null;
244 * @return {SplayTree.Node} Node having the maximum key value that
245 * is less or equal to the specified key value.
247 SplayTree.prototype.findGreatestLessThan = function(key) {
248 if (this.isEmpty()) {
251 // Splay on the key to move the node with the given key or the last
252 // node on the search path to the top of the tree.
254 // Now the result is either the root node or the greatest node in
256 if (this.root_.key <= key) {
258 } else if (this.root_.left) {
259 return this.findMax(this.root_.left);
267 * @return {Array<*>} An array containing all the keys of tree's nodes.
269 SplayTree.prototype.exportKeys = function() {
271 if (!this.isEmpty()) {
272 this.root_.traverse_(function(node) { result.push(node.key); });
279 * Perform the splay operation for the given key. Moves the node with
280 * the given key to the top of the tree. If no node has the given
281 * key, the last node on the search path is moved to the top of the
282 * tree. This is the simplified top-down splaying algorithm from:
283 * "Self-adjusting Binary Search Trees" by Sleator and Tarjan
285 * @param {number} key Key to splay the tree on.
288 SplayTree.prototype.splay_ = function(key) {
289 if (this.isEmpty()) {
292 // Create a dummy node. The use of the dummy node is a bit
293 // counter-intuitive: The right child of the dummy node will hold
294 // the L tree of the algorithm. The left child of the dummy node
295 // will hold the R tree of the algorithm. Using a dummy node, left
296 // and right will always be nodes and we avoid special cases.
297 var dummy, left, right;
298 dummy = left = right = new SplayTree.Node(null, null);
299 var current = this.root_;
301 if (key < current.key) {
305 if (key < current.left.key) {
307 var tmp = current.left;
308 current.left = tmp.right;
316 right.left = current;
318 current = current.left;
319 } else if (key > current.key) {
320 if (!current.right) {
323 if (key > current.right.key) {
325 var tmp = current.right;
326 current.right = tmp.left;
329 if (!current.right) {
334 left.right = current;
336 current = current.right;
342 left.right = current.left;
343 right.left = current.right;
344 current.left = dummy.right;
345 current.right = dummy.left;
346 this.root_ = current;
351 * Constructs a Splay tree node.
353 * @param {number} key Key.
354 * @param {*} value Value.
356 SplayTree.Node = function(key, value) {
363 * @type {SplayTree.Node}
365 SplayTree.Node.prototype.left = null;
369 * @type {SplayTree.Node}
371 SplayTree.Node.prototype.right = null;
375 * Performs an ordered traversal of the subtree starting at
376 * this SplayTree.Node.
378 * @param {function(SplayTree.Node)} f Visitor function.
381 SplayTree.Node.prototype.traverse_ = function(f) {
384 var left = current.left;
385 if (left) left.traverse_(f);
387 current = current.right;