js/src/jit-test/tests/v8-v5/check-splay.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

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', 126125, [
michael@0 37 // new Benchmark("Splay", SplayRun, SplaySetup, SplayTearDown)
michael@0 38 //]);
michael@0 39
michael@0 40 // This is the best random number generator available to mankind ;)
michael@0 41 var MyMath = {
michael@0 42 seed: 49734321,
michael@0 43 random: function() {
michael@0 44 // Robert Jenkins' 32 bit integer hash function.
michael@0 45 this.seed = ((this.seed + 0x7ed55d16) + (this.seed << 12)) & 0xffffffff;
michael@0 46 this.seed = ((this.seed ^ 0xc761c23c) ^ (this.seed >>> 19)) & 0xffffffff;
michael@0 47 this.seed = ((this.seed + 0x165667b1) + (this.seed << 5)) & 0xffffffff;
michael@0 48 this.seed = ((this.seed + 0xd3a2646c) ^ (this.seed << 9)) & 0xffffffff;
michael@0 49 this.seed = ((this.seed + 0xfd7046c5) + (this.seed << 3)) & 0xffffffff;
michael@0 50 this.seed = ((this.seed ^ 0xb55a4f09) ^ (this.seed >>> 16)) & 0xffffffff;
michael@0 51 return (this.seed & 0xfffffff) / 0x10000000;
michael@0 52 },
michael@0 53 };
michael@0 54
michael@0 55 // Configuration.
michael@0 56 var kSplayTreeSize = 8000;
michael@0 57 var kSplayTreeModifications = 80;
michael@0 58 var kSplayTreePayloadDepth = 5;
michael@0 59
michael@0 60 var splayTree = null;
michael@0 61
michael@0 62
michael@0 63 function GeneratePayloadTree(depth, key) {
michael@0 64 if (depth == 0) {
michael@0 65 return {
michael@0 66 array : [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
michael@0 67 string : 'String for key ' + key + ' in leaf node'
michael@0 68 };
michael@0 69 } else {
michael@0 70 return {
michael@0 71 left: GeneratePayloadTree(depth - 1, key),
michael@0 72 right: GeneratePayloadTree(depth - 1, key)
michael@0 73 };
michael@0 74 }
michael@0 75 }
michael@0 76
michael@0 77
michael@0 78 function GenerateKey() {
michael@0 79 // The benchmark framework guarantees that Math.random is
michael@0 80 // deterministic; see base.js.
michael@0 81 // base.js isn't pulled in for jit-tests
michael@0 82 return MyMath.random();
michael@0 83 }
michael@0 84
michael@0 85
michael@0 86 function InsertNewNode() {
michael@0 87 // Insert new node with a unique key.
michael@0 88 var key;
michael@0 89 do {
michael@0 90 key = GenerateKey();
michael@0 91 } while (splayTree.find(key) != null);
michael@0 92 splayTree.insert(key, GeneratePayloadTree(kSplayTreePayloadDepth, key));
michael@0 93 return key;
michael@0 94 }
michael@0 95
michael@0 96
michael@0 97 function SplaySetup() {
michael@0 98 splayTree = new SplayTree();
michael@0 99 for (var i = 0; i < kSplayTreeSize; i++) InsertNewNode();
michael@0 100 }
michael@0 101
michael@0 102
michael@0 103 function SplayTearDown() {
michael@0 104 // Allow the garbage collector to reclaim the memory
michael@0 105 // used by the splay tree no matter how we exit the
michael@0 106 // tear down function.
michael@0 107 var keys = splayTree.exportKeys();
michael@0 108 splayTree = null;
michael@0 109
michael@0 110 // Verify that the splay tree has the right size.
michael@0 111 var length = keys.length;
michael@0 112 assertEq(length, kSplayTreeSize);
michael@0 113
michael@0 114 // Verify that the splay tree has sorted, unique keys.
michael@0 115 for (var i = 0; i < length - 1; i++) {
michael@0 116 assertEq(keys[i] < keys[i + 1], true);
michael@0 117 }
michael@0 118 }
michael@0 119
michael@0 120
michael@0 121 function SplayRun() {
michael@0 122 // Replace a few nodes in the splay tree.
michael@0 123 for (var i = 0; i < kSplayTreeModifications; i++) {
michael@0 124 var key = InsertNewNode();
michael@0 125 var greatest = splayTree.findGreatestLessThan(key);
michael@0 126 if (greatest == null) splayTree.remove(key);
michael@0 127 else splayTree.remove(greatest.key);
michael@0 128 }
michael@0 129 }
michael@0 130
michael@0 131
michael@0 132 /**
michael@0 133 * Constructs a Splay tree. A splay tree is a self-balancing binary
michael@0 134 * search tree with the additional property that recently accessed
michael@0 135 * elements are quick to access again. It performs basic operations
michael@0 136 * such as insertion, look-up and removal in O(log(n)) amortized time.
michael@0 137 *
michael@0 138 * @constructor
michael@0 139 */
michael@0 140 function SplayTree() {
michael@0 141 };
michael@0 142
michael@0 143
michael@0 144 /**
michael@0 145 * Pointer to the root node of the tree.
michael@0 146 *
michael@0 147 * @type {SplayTree.Node}
michael@0 148 * @private
michael@0 149 */
michael@0 150 SplayTree.prototype.root_ = null;
michael@0 151
michael@0 152
michael@0 153 /**
michael@0 154 * @return {boolean} Whether the tree is empty.
michael@0 155 */
michael@0 156 SplayTree.prototype.isEmpty = function() {
michael@0 157 return !this.root_;
michael@0 158 };
michael@0 159
michael@0 160
michael@0 161 /**
michael@0 162 * Inserts a node into the tree with the specified key and value if
michael@0 163 * the tree does not already contain a node with the specified key. If
michael@0 164 * the value is inserted, it becomes the root of the tree.
michael@0 165 *
michael@0 166 * @param {number} key Key to insert into the tree.
michael@0 167 * @param {*} value Value to insert into the tree.
michael@0 168 */
michael@0 169 SplayTree.prototype.insert = function(key, value) {
michael@0 170 if (this.isEmpty()) {
michael@0 171 this.root_ = new SplayTree.Node(key, value);
michael@0 172 return;
michael@0 173 }
michael@0 174 // Splay on the key to move the last node on the search path for
michael@0 175 // the key to the root of the tree.
michael@0 176 this.splay_(key);
michael@0 177 if (this.root_.key == key) {
michael@0 178 return;
michael@0 179 }
michael@0 180 var node = new SplayTree.Node(key, value);
michael@0 181 if (key > this.root_.key) {
michael@0 182 node.left = this.root_;
michael@0 183 node.right = this.root_.right;
michael@0 184 this.root_.right = null;
michael@0 185 } else {
michael@0 186 node.right = this.root_;
michael@0 187 node.left = this.root_.left;
michael@0 188 this.root_.left = null;
michael@0 189 }
michael@0 190 this.root_ = node;
michael@0 191 };
michael@0 192
michael@0 193
michael@0 194 /**
michael@0 195 * Removes a node with the specified key from the tree if the tree
michael@0 196 * contains a node with this key. The removed node is returned. If the
michael@0 197 * key is not found, an exception is thrown.
michael@0 198 *
michael@0 199 * @param {number} key Key to find and remove from the tree.
michael@0 200 * @return {SplayTree.Node} The removed node.
michael@0 201 */
michael@0 202 SplayTree.prototype.remove = function(key) {
michael@0 203 if (this.isEmpty()) {
michael@0 204 throw Error('Key not found: ' + key);
michael@0 205 }
michael@0 206 this.splay_(key);
michael@0 207 if (this.root_.key != key) {
michael@0 208 throw Error('Key not found: ' + key);
michael@0 209 }
michael@0 210 var removed = this.root_;
michael@0 211 if (!this.root_.left) {
michael@0 212 this.root_ = this.root_.right;
michael@0 213 } else {
michael@0 214 var right = this.root_.right;
michael@0 215 this.root_ = this.root_.left;
michael@0 216 // Splay to make sure that the new root has an empty right child.
michael@0 217 this.splay_(key);
michael@0 218 // Insert the original right child as the right child of the new
michael@0 219 // root.
michael@0 220 this.root_.right = right;
michael@0 221 }
michael@0 222 return removed;
michael@0 223 };
michael@0 224
michael@0 225
michael@0 226 /**
michael@0 227 * Returns the node having the specified key or null if the tree doesn't contain
michael@0 228 * a node with the specified key.
michael@0 229 *
michael@0 230 * @param {number} key Key to find in the tree.
michael@0 231 * @return {SplayTree.Node} Node having the specified key.
michael@0 232 */
michael@0 233 SplayTree.prototype.find = function(key) {
michael@0 234 if (this.isEmpty()) {
michael@0 235 return null;
michael@0 236 }
michael@0 237 this.splay_(key);
michael@0 238 return this.root_.key == key ? this.root_ : null;
michael@0 239 };
michael@0 240
michael@0 241
michael@0 242 /**
michael@0 243 * @return {SplayTree.Node} Node having the maximum key value that
michael@0 244 * is less or equal to the specified key value.
michael@0 245 */
michael@0 246 SplayTree.prototype.findGreatestLessThan = function(key) {
michael@0 247 if (this.isEmpty()) {
michael@0 248 return null;
michael@0 249 }
michael@0 250 // Splay on the key to move the node with the given key or the last
michael@0 251 // node on the search path to the top of the tree.
michael@0 252 this.splay_(key);
michael@0 253 // Now the result is either the root node or the greatest node in
michael@0 254 // the left subtree.
michael@0 255 if (this.root_.key <= key) {
michael@0 256 return this.root_;
michael@0 257 } else if (this.root_.left) {
michael@0 258 return this.findMax(this.root_.left);
michael@0 259 } else {
michael@0 260 return null;
michael@0 261 }
michael@0 262 };
michael@0 263
michael@0 264
michael@0 265 /**
michael@0 266 * @return {Array<*>} An array containing all the keys of tree's nodes.
michael@0 267 */
michael@0 268 SplayTree.prototype.exportKeys = function() {
michael@0 269 var result = [];
michael@0 270 if (!this.isEmpty()) {
michael@0 271 this.root_.traverse_(function(node) { result.push(node.key); });
michael@0 272 }
michael@0 273 return result;
michael@0 274 };
michael@0 275
michael@0 276
michael@0 277 /**
michael@0 278 * Perform the splay operation for the given key. Moves the node with
michael@0 279 * the given key to the top of the tree. If no node has the given
michael@0 280 * key, the last node on the search path is moved to the top of the
michael@0 281 * tree. This is the simplified top-down splaying algorithm from:
michael@0 282 * "Self-adjusting Binary Search Trees" by Sleator and Tarjan
michael@0 283 *
michael@0 284 * @param {number} key Key to splay the tree on.
michael@0 285 * @private
michael@0 286 */
michael@0 287 SplayTree.prototype.splay_ = function(key) {
michael@0 288 if (this.isEmpty()) {
michael@0 289 return;
michael@0 290 }
michael@0 291 // Create a dummy node. The use of the dummy node is a bit
michael@0 292 // counter-intuitive: The right child of the dummy node will hold
michael@0 293 // the L tree of the algorithm. The left child of the dummy node
michael@0 294 // will hold the R tree of the algorithm. Using a dummy node, left
michael@0 295 // and right will always be nodes and we avoid special cases.
michael@0 296 var dummy, left, right;
michael@0 297 dummy = left = right = new SplayTree.Node(null, null);
michael@0 298 var current = this.root_;
michael@0 299 while (true) {
michael@0 300 if (key < current.key) {
michael@0 301 if (!current.left) {
michael@0 302 break;
michael@0 303 }
michael@0 304 if (key < current.left.key) {
michael@0 305 // Rotate right.
michael@0 306 var tmp = current.left;
michael@0 307 current.left = tmp.right;
michael@0 308 tmp.right = current;
michael@0 309 current = tmp;
michael@0 310 if (!current.left) {
michael@0 311 break;
michael@0 312 }
michael@0 313 }
michael@0 314 // Link right.
michael@0 315 right.left = current;
michael@0 316 right = current;
michael@0 317 current = current.left;
michael@0 318 } else if (key > current.key) {
michael@0 319 if (!current.right) {
michael@0 320 break;
michael@0 321 }
michael@0 322 if (key > current.right.key) {
michael@0 323 // Rotate left.
michael@0 324 var tmp = current.right;
michael@0 325 current.right = tmp.left;
michael@0 326 tmp.left = current;
michael@0 327 current = tmp;
michael@0 328 if (!current.right) {
michael@0 329 break;
michael@0 330 }
michael@0 331 }
michael@0 332 // Link left.
michael@0 333 left.right = current;
michael@0 334 left = current;
michael@0 335 current = current.right;
michael@0 336 } else {
michael@0 337 break;
michael@0 338 }
michael@0 339 }
michael@0 340 // Assemble.
michael@0 341 left.right = current.left;
michael@0 342 right.left = current.right;
michael@0 343 current.left = dummy.right;
michael@0 344 current.right = dummy.left;
michael@0 345 this.root_ = current;
michael@0 346 };
michael@0 347
michael@0 348
michael@0 349 /**
michael@0 350 * Constructs a Splay tree node.
michael@0 351 *
michael@0 352 * @param {number} key Key.
michael@0 353 * @param {*} value Value.
michael@0 354 */
michael@0 355 SplayTree.Node = function(key, value) {
michael@0 356 this.key = key;
michael@0 357 this.value = value;
michael@0 358 };
michael@0 359
michael@0 360
michael@0 361 /**
michael@0 362 * @type {SplayTree.Node}
michael@0 363 */
michael@0 364 SplayTree.Node.prototype.left = null;
michael@0 365
michael@0 366
michael@0 367 /**
michael@0 368 * @type {SplayTree.Node}
michael@0 369 */
michael@0 370 SplayTree.Node.prototype.right = null;
michael@0 371
michael@0 372
michael@0 373 /**
michael@0 374 * Performs an ordered traversal of the subtree starting at
michael@0 375 * this SplayTree.Node.
michael@0 376 *
michael@0 377 * @param {function(SplayTree.Node)} f Visitor function.
michael@0 378 * @private
michael@0 379 */
michael@0 380 SplayTree.Node.prototype.traverse_ = function(f) {
michael@0 381 var current = this;
michael@0 382 while (current) {
michael@0 383 var left = current.left;
michael@0 384 if (left) left.traverse_(f);
michael@0 385 f(current);
michael@0 386 current = current.right;
michael@0 387 }
michael@0 388 };
michael@0 389
michael@0 390 SplaySetup();
michael@0 391 SplayRun();
michael@0 392 SplayTearDown();

mercurial