OLD | NEW |
1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 15 matching lines...) Expand all Loading... |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 // This benchmark is based on a JavaScript log processing module used | 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 | 29 // by the V8 profiler to generate execution time profiles for runs of |
30 // JavaScript applications, and it effectively measures how fast the | 30 // JavaScript applications, and it effectively measures how fast the |
31 // JavaScript engine is at allocating nodes and reclaiming the memory | 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 | 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 | 33 // also has to deal with a lot of changes to the large tree object |
34 // graph. | 34 // graph. |
35 | 35 |
36 var Splay = new BenchmarkSuite('Splay', 126125, [ | 36 var Splay = new BenchmarkSuite('Splay', 21915, [ |
37 new Benchmark("Splay", SplayRun, SplaySetup, SplayTearDown) | 37 new Benchmark("Splay", SplayRun, SplaySetup, SplayTearDown) |
38 ]); | 38 ]); |
39 | 39 |
40 | 40 |
41 // Configuration. | 41 // Configuration. |
42 var kSplayTreeSize = 8000; | 42 var kSplayTreeSize = 8000; |
43 var kSplayTreeModifications = 80; | 43 var kSplayTreeModifications = 80; |
44 var kSplayTreePayloadDepth = 5; | 44 var kSplayTreePayloadDepth = 5; |
45 | 45 |
46 var splayTree = null; | 46 var splayTree = null; |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
224 SplayTree.prototype.find = function(key) { | 224 SplayTree.prototype.find = function(key) { |
225 if (this.isEmpty()) { | 225 if (this.isEmpty()) { |
226 return null; | 226 return null; |
227 } | 227 } |
228 this.splay_(key); | 228 this.splay_(key); |
229 return this.root_.key == key ? this.root_ : null; | 229 return this.root_.key == key ? this.root_ : null; |
230 }; | 230 }; |
231 | 231 |
232 | 232 |
233 /** | 233 /** |
| 234 * @return {SplayTree.Node} Node having the maximum key value. |
| 235 */ |
| 236 SplayTree.prototype.findMax = function(opt_startNode) { |
| 237 if (this.isEmpty()) { |
| 238 return null; |
| 239 } |
| 240 var current = opt_startNode || this.root_; |
| 241 while (current.right) { |
| 242 current = current.right; |
| 243 } |
| 244 return current; |
| 245 }; |
| 246 |
| 247 |
| 248 /** |
234 * @return {SplayTree.Node} Node having the maximum key value that | 249 * @return {SplayTree.Node} Node having the maximum key value that |
235 * is less or equal to the specified key value. | 250 * is less than the specified key value. |
236 */ | 251 */ |
237 SplayTree.prototype.findGreatestLessThan = function(key) { | 252 SplayTree.prototype.findGreatestLessThan = function(key) { |
238 if (this.isEmpty()) { | 253 if (this.isEmpty()) { |
239 return null; | 254 return null; |
240 } | 255 } |
241 // Splay on the key to move the node with the given key or the last | 256 // Splay on the key to move the node with the given key or the last |
242 // node on the search path to the top of the tree. | 257 // node on the search path to the top of the tree. |
243 this.splay_(key); | 258 this.splay_(key); |
244 // Now the result is either the root node or the greatest node in | 259 // Now the result is either the root node or the greatest node in |
245 // the left subtree. | 260 // the left subtree. |
246 if (this.root_.key <= key) { | 261 if (this.root_.key < key) { |
247 return this.root_; | 262 return this.root_; |
248 } else if (this.root_.left) { | 263 } else if (this.root_.left) { |
249 return this.findMax(this.root_.left); | 264 return this.findMax(this.root_.left); |
250 } else { | 265 } else { |
251 return null; | 266 return null; |
252 } | 267 } |
253 }; | 268 }; |
254 | 269 |
255 | 270 |
256 /** | 271 /** |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 */ | 385 */ |
371 SplayTree.Node.prototype.traverse_ = function(f) { | 386 SplayTree.Node.prototype.traverse_ = function(f) { |
372 var current = this; | 387 var current = this; |
373 while (current) { | 388 while (current) { |
374 var left = current.left; | 389 var left = current.left; |
375 if (left) left.traverse_(f); | 390 if (left) left.traverse_(f); |
376 f(current); | 391 f(current); |
377 current = current.right; | 392 current = current.right; |
378 } | 393 } |
379 }; | 394 }; |
OLD | NEW |