Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / jit-test / tests / v8-v5 / check-splay.js
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
4 // met:
5 //
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.
15 //
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.
27
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
34 // graph.
35
36 //var Splay = new BenchmarkSuite('Splay', 126125, [
37 //  new Benchmark("Splay", SplayRun, SplaySetup, SplayTearDown)
38 //]);
39
40 // This is the best random number generator available to mankind ;)
41 var MyMath = {
42   seed: 49734321,
43   random: function() {
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;
52   },
53 };
54
55 // Configuration.
56 var kSplayTreeSize = 8000;
57 var kSplayTreeModifications = 80;
58 var kSplayTreePayloadDepth = 5;
59
60 var splayTree = null;
61
62
63 function GeneratePayloadTree(depth, key) {
64   if (depth == 0) {
65     return {
66       array  : [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ],
67       string : 'String for key ' + key + ' in leaf node'
68     };
69   } else {
70     return {
71       left:  GeneratePayloadTree(depth - 1, key),
72       right: GeneratePayloadTree(depth - 1, key)
73     };
74   }
75 }
76
77
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();
83 }
84
85
86 function InsertNewNode() {
87   // Insert new node with a unique key.
88   var key;
89   do {
90     key = GenerateKey();
91   } while (splayTree.find(key) != null);
92   splayTree.insert(key, GeneratePayloadTree(kSplayTreePayloadDepth, key));
93   return key;
94 }
95
96
97
98 function SplaySetup() {
99   splayTree = new SplayTree();
100   for (var i = 0; i < kSplayTreeSize; i++) InsertNewNode();
101 }
102
103
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();
109   splayTree = null;
110
111   // Verify that the splay tree has the right size.
112   var length = keys.length;
113   assertEq(length, kSplayTreeSize);
114
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);
118   }
119 }
120
121
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);
129   }
130 }
131
132
133 /**
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.
138  *
139  * @constructor
140  */
141 function SplayTree() {
142 };
143
144
145 /**
146  * Pointer to the root node of the tree.
147  *
148  * @type {SplayTree.Node}
149  * @private
150  */
151 SplayTree.prototype.root_ = null;
152
153
154 /**
155  * @return {boolean} Whether the tree is empty.
156  */
157 SplayTree.prototype.isEmpty = function() {
158   return !this.root_;
159 };
160
161
162 /**
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.
166  *
167  * @param {number} key Key to insert into the tree.
168  * @param {*} value Value to insert into the tree.
169  */
170 SplayTree.prototype.insert = function(key, value) {
171   if (this.isEmpty()) {
172     this.root_ = new SplayTree.Node(key, value);
173     return;
174   }
175   // Splay on the key to move the last node on the search path for
176   // the key to the root of the tree.
177   this.splay_(key);
178   if (this.root_.key == key) {
179     return;
180   }
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;
186   } else {
187     node.right = this.root_;
188     node.left = this.root_.left;
189     this.root_.left = null;
190   }
191   this.root_ = node;
192 };
193
194
195 /**
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.
199  *
200  * @param {number} key Key to find and remove from the tree.
201  * @return {SplayTree.Node} The removed node.
202  */
203 SplayTree.prototype.remove = function(key) {
204   if (this.isEmpty()) {
205     throw Error('Key not found: ' + key);
206   }
207   this.splay_(key);
208   if (this.root_.key != key) {
209     throw Error('Key not found: ' + key);
210   }
211   var removed = this.root_;
212   if (!this.root_.left) {
213     this.root_ = this.root_.right;
214   } else {
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.
218     this.splay_(key);
219     // Insert the original right child as the right child of the new
220     // root.
221     this.root_.right = right;
222   }
223   return removed;
224 };
225
226
227 /**
228  * Returns the node having the specified key or null if the tree doesn't contain
229  * a node with the specified key.
230  *
231  * @param {number} key Key to find in the tree.
232  * @return {SplayTree.Node} Node having the specified key.
233  */
234 SplayTree.prototype.find = function(key) {
235   if (this.isEmpty()) {
236     return null;
237   }
238   this.splay_(key);
239   return this.root_.key == key ? this.root_ : null;
240 };
241
242
243 /**
244  * @return {SplayTree.Node} Node having the maximum key value that
245  *     is less or equal to the specified key value.
246  */
247 SplayTree.prototype.findGreatestLessThan = function(key) {
248   if (this.isEmpty()) {
249     return null;
250   }
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.
253   this.splay_(key);
254   // Now the result is either the root node or the greatest node in
255   // the left subtree.
256   if (this.root_.key <= key) {
257     return this.root_;
258   } else if (this.root_.left) {
259     return this.findMax(this.root_.left);
260   } else {
261     return null;
262   }
263 };
264
265
266 /**
267  * @return {Array<*>} An array containing all the keys of tree's nodes.
268  */
269 SplayTree.prototype.exportKeys = function() {
270   var result = [];
271   if (!this.isEmpty()) {
272     this.root_.traverse_(function(node) { result.push(node.key); });
273   }
274   return result;
275 };
276
277
278 /**
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
284  *
285  * @param {number} key Key to splay the tree on.
286  * @private
287  */
288 SplayTree.prototype.splay_ = function(key) {
289   if (this.isEmpty()) {
290     return;
291   }
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_;
300   while (true) {
301     if (key < current.key) {
302       if (!current.left) {
303         break;
304       }
305       if (key < current.left.key) {
306         // Rotate right.
307         var tmp = current.left;
308         current.left = tmp.right;
309         tmp.right = current;
310         current = tmp;
311         if (!current.left) {
312           break;
313         }
314       }
315       // Link right.
316       right.left = current;
317       right = current;
318       current = current.left;
319     } else if (key > current.key) {
320       if (!current.right) {
321         break;
322       }
323       if (key > current.right.key) {
324         // Rotate left.
325         var tmp = current.right;
326         current.right = tmp.left;
327         tmp.left = current;
328         current = tmp;
329         if (!current.right) {
330           break;
331         }
332       }
333       // Link left.
334       left.right = current;
335       left = current;
336       current = current.right;
337     } else {
338       break;
339     }
340   }
341   // Assemble.
342   left.right = current.left;
343   right.left = current.right;
344   current.left = dummy.right;
345   current.right = dummy.left;
346   this.root_ = current;
347 };
348
349
350 /**
351  * Constructs a Splay tree node.
352  *
353  * @param {number} key Key.
354  * @param {*} value Value.
355  */
356 SplayTree.Node = function(key, value) {
357   this.key = key;
358   this.value = value;
359 };
360
361
362 /**
363  * @type {SplayTree.Node}
364  */
365 SplayTree.Node.prototype.left = null;
366
367
368 /**
369  * @type {SplayTree.Node}
370  */
371 SplayTree.Node.prototype.right = null;
372
373
374 /**
375  * Performs an ordered traversal of the subtree starting at
376  * this SplayTree.Node.
377  *
378  * @param {function(SplayTree.Node)} f Visitor function.
379  * @private
380  */
381 SplayTree.Node.prototype.traverse_ = function(f) {
382   var current = this;
383   while (current) {
384     var left = current.left;
385     if (left) left.traverse_(f);
386     f(current);
387     current = current.right;
388   }
389 };
390
391 SplaySetup();
392 SplayRun();
393 SplayTearDown();