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', 81491, [ |
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; |
47 | 47 |
48 | 48 |
49 function GeneratePayloadTree(depth, key) { | 49 function GeneratePayloadTree(depth, tag) { |
50 if (depth == 0) { | 50 if (depth == 0) { |
51 return { | 51 return { |
52 array : [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], | 52 array : [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ], |
53 string : 'String for key ' + key + ' in leaf node' | 53 string : 'String for key ' + tag + ' in leaf node' |
54 }; | 54 }; |
55 } else { | 55 } else { |
56 return { | 56 return { |
57 left: GeneratePayloadTree(depth - 1, key), | 57 left: GeneratePayloadTree(depth - 1, tag), |
58 right: GeneratePayloadTree(depth - 1, key) | 58 right: GeneratePayloadTree(depth - 1, tag) |
59 }; | 59 }; |
60 } | 60 } |
61 } | 61 } |
62 | 62 |
63 | 63 |
64 function GenerateKey() { | 64 function GenerateKey() { |
65 // The benchmark framework guarantees that Math.random is | 65 // The benchmark framework guarantees that Math.random is |
66 // deterministic; see base.js. | 66 // deterministic; see base.js. |
67 return Math.random(); | 67 return Math.random(); |
68 } | 68 } |
69 | 69 |
70 | 70 |
71 function InsertNewNode() { | 71 function InsertNewNode() { |
72 // Insert new node with a unique key. | 72 // Insert new node with a unique key. |
73 var key; | 73 var key; |
74 do { | 74 do { |
75 key = GenerateKey(); | 75 key = GenerateKey(); |
76 } while (splayTree.find(key) != null); | 76 } while (splayTree.find(key) != null); |
77 splayTree.insert(key, GeneratePayloadTree(kSplayTreePayloadDepth, key)); | 77 var payload = GeneratePayloadTree(kSplayTreePayloadDepth, String(key)); |
| 78 splayTree.insert(key, payload); |
78 return key; | 79 return key; |
79 } | 80 } |
80 | 81 |
81 | 82 |
82 | 83 |
83 function SplaySetup() { | 84 function SplaySetup() { |
84 splayTree = new SplayTree(); | 85 splayTree = new SplayTree(); |
85 for (var i = 0; i < kSplayTreeSize; i++) InsertNewNode(); | 86 for (var i = 0; i < kSplayTreeSize; i++) InsertNewNode(); |
86 } | 87 } |
87 | 88 |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 SplayTree.prototype.find = function(key) { | 224 SplayTree.prototype.find = function(key) { |
224 if (this.isEmpty()) { | 225 if (this.isEmpty()) { |
225 return null; | 226 return null; |
226 } | 227 } |
227 this.splay_(key); | 228 this.splay_(key); |
228 return this.root_.key == key ? this.root_ : null; | 229 return this.root_.key == key ? this.root_ : null; |
229 }; | 230 }; |
230 | 231 |
231 | 232 |
232 /** | 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 /** |
233 * @return {SplayTree.Node} Node having the maximum key value that | 249 * @return {SplayTree.Node} Node having the maximum key value that |
234 * is less or equal to the specified key value. | 250 * is less than the specified key value. |
235 */ | 251 */ |
236 SplayTree.prototype.findGreatestLessThan = function(key) { | 252 SplayTree.prototype.findGreatestLessThan = function(key) { |
237 if (this.isEmpty()) { | 253 if (this.isEmpty()) { |
238 return null; | 254 return null; |
239 } | 255 } |
240 // 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 |
241 // node on the search path to the top of the tree. | 257 // node on the search path to the top of the tree. |
242 this.splay_(key); | 258 this.splay_(key); |
243 // 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 |
244 // the left subtree. | 260 // the left subtree. |
245 if (this.root_.key <= key) { | 261 if (this.root_.key < key) { |
246 return this.root_; | 262 return this.root_; |
247 } else if (this.root_.left) { | 263 } else if (this.root_.left) { |
248 return this.findMax(this.root_.left); | 264 return this.findMax(this.root_.left); |
249 } else { | 265 } else { |
250 return null; | 266 return null; |
251 } | 267 } |
252 }; | 268 }; |
253 | 269 |
254 | 270 |
255 /** | 271 /** |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 */ | 385 */ |
370 SplayTree.Node.prototype.traverse_ = function(f) { | 386 SplayTree.Node.prototype.traverse_ = function(f) { |
371 var current = this; | 387 var current = this; |
372 while (current) { | 388 while (current) { |
373 var left = current.left; | 389 var left = current.left; |
374 if (left) left.traverse_(f); | 390 if (left) left.traverse_(f); |
375 f(current); | 391 f(current); |
376 current = current.right; | 392 current = current.right; |
377 } | 393 } |
378 }; | 394 }; |
OLD | NEW |