Skip to content

Commit 49c62f8

Browse files
committed
Use a hashtable for the literals table, instead of linear search.
1 parent 1d8ecbc commit 49c62f8

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

Diff for: bcompile.js

+22-24
Original file line numberDiff line numberDiff line change
@@ -61,35 +61,33 @@ define(["text!bcompile.js", "bytecode-table"], function make_bcompile(bcompile_s
6161
scope: 0,
6262
desugar_frame_get: !dont_desugar_frame_get
6363
};
64-
// literal symbol table. Does string intern'ing too. Very simple.
64+
// literal symbol table. Does string intern'ing too.
65+
var ObjectIs = Object.is || function(a, b) {
66+
if (typeof(a)==='number' && typeof(b)==='number') {
67+
if (a!==a) { return (b!==b); } // NaN
68+
if (a===0) { return (b===0) && (1/a === 1/b); } // +/- 0
69+
}
70+
return (a===b);
71+
};
72+
var literalMap = Object.create(null);
6573
state.literal = function(val) {
66-
var i = 0, l;
67-
if (Object.is) {
68-
while (i < this.literals.length) {
69-
l = this.literals[i];
70-
if (Object.is(l, val)) { return i; }
71-
i += 1;
72-
}
73-
} else if (!(val===val)) { // Look for NaN
74-
while (i < this.literals.length) {
75-
l = this.literals[i];
76-
if (!(l===l)) { return i; }
77-
i += 1;
78-
}
79-
} else if (val===0) { // Handle +/-0
80-
while (i < this.literals.length) {
81-
l = this.literals[i];
82-
if (l===0 && (1/l)===(1/val)) { return i; }
83-
i += 1;
84-
}
85-
} else { // Everything else
86-
while (i < this.literals.length) {
87-
l = this.literals[i];
88-
if (l===val) { return i; }
74+
var i, pair, key, entries;
75+
key = typeof(val) + ':' + val; // very basic hash key
76+
entries = literalMap[key];
77+
if (entries!==undefined) {
78+
i = 0;
79+
while (i < entries.length) {
80+
pair = entries[i];
81+
if (ObjectIs(pair[0], val)) { return pair[1]; }
8982
i += 1;
9083
}
84+
} else {
85+
entries = [];
86+
literalMap[key] = entries;
9187
}
88+
i = this.literals.length;
9289
this.literals[i] = val;
90+
entries.push([val, i]);
9391
return i;
9492
};
9593
// create a function representation

0 commit comments

Comments
 (0)