on master help sign in
Add cache for M^i
0841ce2Christian Widera
libraries/Native/Array.js+10−9
@@ -34,20 +34,15 @@ Elm.Native.Array.make = function(elm) {
34 }
34 }
35
35
36 function unsafeGet(i, array) {
36 function unsafeGet(i, array) {
37− var slotSize = Math.pow(M, array.height);
37+ for (var x = array.height; x > 0; x--) {
38−
39− while (true) {
40− var slot = Math.floor(i / slotSize);
38+ var slot = Math.floor(i / cache[x]);
41 while (array.lengths[slot] <= i) {
39 while (array.lengths[slot] <= i) {
42 slot++;
40 slot++;
43 }
41 }
44 i -= slot > 0 ? array.lengths[slot - 1] : 0;
42 i -= slot > 0 ? array.lengths[slot - 1] : 0;
45 array = array.table[slot];
43 array = array.table[slot];
46− if (array.height == 0) {
47− return array.table[i];
48− }
49− slotSize /= M;
50 }
44 }
45+ return array.table[i];
51 }
46 }
52
47
53 // Sets the value at the index i. Only the nodes leading to i will get
48 // Sets the value at the index i. Only the nodes leading to i will get
@@ -537,10 +532,16 @@ Elm.Native.Array.make = function(elm) {
537 }
532 }
538 }
533 }
539
534
535+ // Cache for Math.pow(M, i)
536+ cache = new Array(10);
537+ for (var i = 0; i < cache.length; i++) {
538+ cache[i] = Math.pow(M, i);
539+ }
540+
540 // Calculates in which slot of "table" the item probably is, then
541 // Calculates in which slot of "table" the item probably is, then
541 // find the exact slot via forward searching in "lengths". Returns the index.
542 // find the exact slot via forward searching in "lengths". Returns the index.
542 function getSlot(i, a) {
543 function getSlot(i, a) {
543− var slot = Math.floor(i / (Math.pow(M, a.height)));
544+ var slot = Math.floor(i / cache[a.height]);
544 while (a.lengths[slot] <= i) {
545 while (a.lengths[slot] <= i) {
545 slot++
546 slot++
546 }
547 }