js/src/v8/splay.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright 2009 the V8 project authors. All rights reserved.
michael@0 2 // Redistribution and use in source and binary forms, with or without
michael@0 3 // modification, are permitted provided that the following conditions are
michael@0 4 // met:
michael@0 5 //
michael@0 6 // * Redistributions of source code must retain the above copyright
michael@0 7 // notice, this list of conditions and the following disclaimer.
michael@0 8 // * Redistributions in binary form must reproduce the above
michael@0 9 // copyright notice, this list of conditions and the following
michael@0 10 // disclaimer in the documentation and/or other materials provided
michael@0 11 // with the distribution.
michael@0 12 // * Neither the name of Google Inc. nor the names of its
michael@0 13 // contributors may be used to endorse or promote products derived
michael@0 14 // from this software without specific prior written permission.
michael@0 15 //
michael@0 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 27
michael@0 28 // This benchmark is based on a JavaScript log processing module used
michael@0 29 // by the V8 profiler to generate execution time profiles for runs of
michael@0 30 // JavaScript applications, and it effectively measures how fast the
michael@0 31 // JavaScript engine is at allocating nodes and reclaiming the memory
michael@0 32 // used for old nodes. Because of the way splay trees work, the engine
michael@0 33 // also has to deal with a lot of changes to the large tree object
michael@0 34 // graph.
michael@0 35
michael@0 36 var Splay = new BenchmarkSuite('Splay', 81491, [
michael@0 37 new Benchmark("Splay", SplayRun, SplaySetup, SplayTearDown)
michael@0 38 ]);
michael@0 39
michael@0 40
michael@0 41 // Configuration.
michael@0 42 var kSplayTreeSize = 8000;
michael@0 43 var kSplayTreeModifications = 80;
michael@0 44 var kSplayTreePayloadDepth = 5;
michael@0 45
michael@0 46 var splayTree = null;
michael@0 47
michael@0 48
michael@0 49 function GeneratePayloadTree(depth, tag) {
michael@0 50 if (depth == 0) {
michael@0 51 return {
michael@0 52 array : [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
michael@0 53 string : 'String for key ' + tag + ' in leaf node'
michael@0 54 };
michael@0 55 } else {
michael@0 56 return {
michael@0 57 left: GeneratePayloadTree(depth - 1, tag),
michael@0 58 right: GeneratePayloadTree(depth - 1, tag)
michael@0 59 };
michael@0 60 }
michael@0 61 }
michael@0 62
michael@0 63
michael@0 64 function GenerateKey() {
michael@0 65 // The benchmark framework guarantees that Math.random is
michael@0 66 // deterministic; see base.js.
michael@0 67 return Math.random();
michael@0 68 }
michael@0 69
michael@0 70
michael@0 71 function InsertNewNode() {
michael@0 72 // Insert new node with a unique key.
michael@0 73 var key;
michael@0 74 do {
michael@0 75 key = GenerateKey();
michael@0 76 } while (splayTree.find(key) != null);
michael@0 77 var payload = GeneratePayloadTree(kSplayTreePayloadDepth, String(key));
michael@0 78 splayTree.insert(key, payload);
michael@0 79 return key;
michael@0 80 }
michael@0 81
michael@0 82
michael@0 83
michael@0 84 function SplaySetup() {
michael@0 85 splayTree = new SplayTree();
michael@0 86 for (var i = 0; i < kSplayTreeSize; i++) InsertNewNode();
michael@0 87 }
michael@0 88
michael@0 89
michael@0 90 function SplayTearDown() {
michael@0 91 // Allow the garbage collector to reclaim the memory
michael@0 92 // used by the splay tree no matter how we exit the
michael@0 93 // tear down function.
michael@0 94 var keys = splayTree.exportKeys();
michael@0 95 splayTree = null;
michael@0 96
michael@0 97 // Verify that the splay tree has the right size.
michael@0 98 var length = keys.length;
michael@0 99 if (length != kSplayTreeSize) {
michael@0 100 throw new Error("Splay tree has wrong size");
michael@0 101 }
michael@0 102
michael@0 103 // Verify that the splay tree has sorted, unique keys.
michael@0 104 for (var i = 0; i < length - 1; i++) {
michael@0 105 if (keys[i] >= keys[i + 1]) {
michael@0 106 throw new Error("Splay tree not sorted");
michael@0 107 }
michael@0 108 }
michael@0 109 }
michael@0 110
michael@0 111
michael@0 112 function SplayRun() {
michael@0 113 // Replace a few nodes in the splay tree.
michael@0 114 for (var i = 0; i < kSplayTreeModifications; i++) {
michael@0 115 var key = InsertNewNode();
michael@0 116 var greatest = splayTree.findGreatestLessThan(key);
michael@0 117 if (greatest == null) splayTree.remove(key);
michael@0 118 else splayTree.remove(greatest.key);
michael@0 119 }
michael@0 120 }
michael@0 121
michael@0 122
michael@0 123 /**
michael@0 124 * Constructs a Splay tree. A splay tree is a self-balancing binary
michael@0 125 * search tree with the additional property that recently accessed
michael@0 126 * elements are quick to access again. It performs basic operations
michael@0 127 * such as insertion, look-up and removal in O(log(n)) amortized time.
michael@0 128 *
michael@0 129 * @constructor
michael@0 130 */
michael@0 131 function SplayTree() {
michael@0 132 };
michael@0 133
michael@0 134
michael@0 135 /**
michael@0 136 * Pointer to the root node of the tree.
michael@0 137 *
michael@0 138 * @type {SplayTree.Node}
michael@0 139 * @private
michael@0 140 */
michael@0 141 SplayTree.prototype.root_ = null;
michael@0 142
michael@0 143
michael@0 144 /**
michael@0 145 * @return {boolean} Whether the tree is empty.
michael@0 146 */
michael@0 147 SplayTree.prototype.isEmpty = function() {
michael@0 148 return !this.root_;
michael@0 149 };
michael@0 150
michael@0 151
michael@0 152 /**
michael@0 153 * Inserts a node into the tree with the specified key and value if
michael@0 154 * the tree does not already contain a node with the specified key. If
michael@0 155 * the value is inserted, it becomes the root of the tree.
michael@0 156 *
michael@0 157 * @param {number} key Key to insert into the tree.
michael@0 158 * @param {*} value Value to insert into the tree.
michael@0 159 */
michael@0 160 SplayTree.prototype.insert = function(key, value) {
michael@0 161 if (this.isEmpty()) {
michael@0 162 this.root_ = new SplayTree.Node(key, value);
michael@0 163 return;
michael@0 164 }
michael@0 165 // Splay on the key to move the last node on the search path for
michael@0 166 // the key to the root of the tree.
michael@0 167 this.splay_(key);
michael@0 168 if (this.root_.key == key) {
michael@0 169 return;
michael@0 170 }
michael@0 171 var node = new SplayTree.Node(key, value);
michael@0 172 if (key > this.root_.key) {
michael@0 173 node.left = this.root_;
michael@0 174 node.right = this.root_.right;
michael@0 175 this.root_.right = null;
michael@0 176 } else {
michael@0 177 node.right = this.root_;
michael@0 178 node.left = this.root_.left;
michael@0 179 this.root_.left = null;
michael@0 180 }
michael@0 181 this.root_ = node;
michael@0 182 };
michael@0 183
michael@0 184
michael@0 185 /**
michael@0 186 * Removes a node with the specified key from the tree if the tree
michael@0 187 * contains a node with this key. The removed node is returned. If the
michael@0 188 * key is not found, an exception is thrown.
michael@0 189 *
michael@0 190 * @param {number} key Key to find and remove from the tree.
michael@0 191 * @return {SplayTree.Node} The removed node.
michael@0 192 */
michael@0 193 SplayTree.prototype.remove = function(key) {
michael@0 194 if (this.isEmpty()) {
michael@0 195 throw Error('Key not found: ' + key);
michael@0 196 }
michael@0 197 this.splay_(key);
michael@0 198 if (this.root_.key != key) {
michael@0 199 throw Error('Key not found: ' + key);
michael@0 200 }
michael@0 201 var removed = this.root_;
michael@0 202 if (!this.root_.left) {
michael@0 203 this.root_ = this.root_.right;
michael@0 204 } else {
michael@0 205 var right = this.root_.right;
michael@0 206 this.root_ = this.root_.left;
michael@0 207 // Splay to make sure that the new root has an empty right child.
michael@0 208 this.splay_(key);
michael@0 209 // Insert the original right child as the right child of the new
michael@0 210 // root.
michael@0 211 this.root_.right = right;
michael@0 212 }
michael@0 213 return removed;
michael@0 214 };
michael@0 215
michael@0 216
michael@0 217 /**
michael@0 218 * Returns the node having the specified key or null if the tree doesn't contain
michael@0 219 * a node with the specified key.
michael@0 220 *
michael@0 221 * @param {number} key Key to find in the tree.
michael@0 222 * @return {SplayTree.Node} Node having the specified key.
michael@0 223 */
michael@0 224 SplayTree.prototype.find = function(key) {
michael@0 225 if (this.isEmpty()) {
michael@0 226 return null;
michael@0 227 }
michael@0 228 this.splay_(key);
michael@0 229 return this.root_.key == key ? this.root_ : null;
michael@0 230 };
michael@0 231
michael@0 232
michael@0 233 /**
michael@0 234 * @return {SplayTree.Node} Node having the maximum key value.
michael@0 235 */
michael@0 236 SplayTree.prototype.findMax = function(opt_startNode) {
michael@0 237 if (this.isEmpty()) {
michael@0 238 return null;
michael@0 239 }
michael@0 240 var current = opt_startNode || this.root_;
michael@0 241 while (current.right) {
michael@0 242 current = current.right;
michael@0 243 }
michael@0 244 return current;
michael@0 245 };
michael@0 246
michael@0 247
michael@0 248 /**
michael@0 249 * @return {SplayTree.Node} Node having the maximum key value that
michael@0 250 * is less than the specified key value.
michael@0 251 */
michael@0 252 SplayTree.prototype.findGreatestLessThan = function(key) {
michael@0 253 if (this.isEmpty()) {
michael@0 254 return null;
michael@0 255 }
michael@0 256 // Splay on the key to move the node with the given key or the last
michael@0 257 // node on the search path to the top of the tree.
michael@0 258 this.splay_(key);
michael@0 259 // Now the result is either the root node or the greatest node in
michael@0 260 // the left subtree.
michael@0 261 if (this.root_.key < key) {
michael@0 262 return this.root_;
michael@0 263 } else if (this.root_.left) {
michael@0 264 return this.findMax(this.root_.left);
michael@0 265 } else {
michael@0 266 return null;
michael@0 267 }
michael@0 268 };
michael@0 269
michael@0 270
michael@0 271 /**
michael@0 272 * @return {Array<*>} An array containing all the keys of tree's nodes.
michael@0 273 */
michael@0 274 SplayTree.prototype.exportKeys = function() {
michael@0 275 var result = [];
michael@0 276 if (!this.isEmpty()) {
michael@0 277 this.root_.traverse_(function(node) { result.push(node.key); });
michael@0 278 }
michael@0 279 return result;
michael@0 280 };
michael@0 281
michael@0 282
michael@0 283 /**
michael@0 284 * Perform the splay operation for the given key. Moves the node with
michael@0 285 * the given key to the top of the tree. If no node has the given
michael@0 286 * key, the last node on the search path is moved to the top of the
michael@0 287 * tree. This is the simplified top-down splaying algorithm from:
michael@0 288 * "Self-adjusting Binary Search Trees" by Sleator and Tarjan
michael@0 289 *
michael@0 290 * @param {number} key Key to splay the tree on.
michael@0 291 * @private
michael@0 292 */
michael@0 293 SplayTree.prototype.splay_ = function(key) {
michael@0 294 if (this.isEmpty()) {
michael@0 295 return;
michael@0 296 }
michael@0 297 // Create a dummy node. The use of the dummy node is a bit
michael@0 298 // counter-intuitive: The right child of the dummy node will hold
michael@0 299 // the L tree of the algorithm. The left child of the dummy node
michael@0 300 // will hold the R tree of the algorithm. Using a dummy node, left
michael@0 301 // and right will always be nodes and we avoid special cases.
michael@0 302 var dummy, left, right;
michael@0 303 dummy = left = right = new SplayTree.Node(null, null);
michael@0 304 var current = this.root_;
michael@0 305 while (true) {
michael@0 306 if (key < current.key) {
michael@0 307 if (!current.left) {
michael@0 308 break;
michael@0 309 }
michael@0 310 if (key < current.left.key) {
michael@0 311 // Rotate right.
michael@0 312 var tmp = current.left;
michael@0 313 current.left = tmp.right;
michael@0 314 tmp.right = current;
michael@0 315 current = tmp;
michael@0 316 if (!current.left) {
michael@0 317 break;
michael@0 318 }
michael@0 319 }
michael@0 320 // Link right.
michael@0 321 right.left = current;
michael@0 322 right = current;
michael@0 323 current = current.left;
michael@0 324 } else if (key > current.key) {
michael@0 325 if (!current.right) {
michael@0 326 break;
michael@0 327 }
michael@0 328 if (key > current.right.key) {
michael@0 329 // Rotate left.
michael@0 330 var tmp = current.right;
michael@0 331 current.right = tmp.left;
michael@0 332 tmp.left = current;
michael@0 333 current = tmp;
michael@0 334 if (!current.right) {
michael@0 335 break;
michael@0 336 }
michael@0 337 }
michael@0 338 // Link left.
michael@0 339 left.right = current;
michael@0 340 left = current;
michael@0 341 current = current.right;
michael@0 342 } else {
michael@0 343 break;
michael@0 344 }
michael@0 345 }
michael@0 346 // Assemble.
michael@0 347 left.right = current.left;
michael@0 348 right.left = current.right;
michael@0 349 current.left = dummy.right;
michael@0 350 current.right = dummy.left;
michael@0 351 this.root_ = current;
michael@0 352 };
michael@0 353
michael@0 354
michael@0 355 /**
michael@0 356 * Constructs a Splay tree node.
michael@0 357 *
michael@0 358 * @param {number} key Key.
michael@0 359 * @param {*} value Value.
michael@0 360 */
michael@0 361 SplayTree.Node = function(key, value) {
michael@0 362 this.key = key;
michael@0 363 this.value = value;
michael@0 364 };
michael@0 365
michael@0 366
michael@0 367 /**
michael@0 368 * @type {SplayTree.Node}
michael@0 369 */
michael@0 370 SplayTree.Node.prototype.left = null;
michael@0 371
michael@0 372
michael@0 373 /**
michael@0 374 * @type {SplayTree.Node}
michael@0 375 */
michael@0 376 SplayTree.Node.prototype.right = null;
michael@0 377
michael@0 378
michael@0 379 /**
michael@0 380 * Performs an ordered traversal of the subtree starting at
michael@0 381 * this SplayTree.Node.
michael@0 382 *
michael@0 383 * @param {function(SplayTree.Node)} f Visitor function.
michael@0 384 * @private
michael@0 385 */
michael@0 386 SplayTree.Node.prototype.traverse_ = function(f) {
michael@0 387 var current = this;
michael@0 388 while (current) {
michael@0 389 var left = current.left;
michael@0 390 if (left) left.traverse_(f);
michael@0 391 f(current);
michael@0 392 current = current.right;
michael@0 393 }
michael@0 394 };

mercurial