Skip to content

Commit b0c4138

Browse files
🚴 perf: Switch on small list length's last two bits.
1 parent 87c2253 commit b0c4138

File tree

7 files changed

+131
-131
lines changed

7 files changed

+131
-131
lines changed

src/0-core/_fast/_append_small_list.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import assert from 'assert';
33
export default function _append_small_list(tree, list) {
44
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
55
// eslint-disable-next-line default-case
6-
switch (list.length) {
6+
switch (list.length & 0b11) {
7+
case 0:
8+
return tree.push(list[0]).push(list[1]).push(list[2]).push(list[3]);
79
case 1:
810
return tree.push(list[0]);
911
case 2:
1012
return tree.push(list[0]).push(list[1]);
1113
case 3:
1214
return tree.push(list[0]).push(list[1]).push(list[2]);
13-
case 4:
14-
return tree.push(list[0]).push(list[1]).push(list[2]).push(list[3]);
1515
}
1616
}

src/0-core/_fast/_from_medium_list.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@ export function _from_medium_list(M, list) {
88
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
99

1010
// eslint-disable-next-line default-case
11-
switch (list.length | 0) {
11+
switch (list.length & 0b11) {
12+
case 0:
13+
return new Deep(
14+
M,
15+
new Two(list[0], list[1]),
16+
empty(cache(M)),
17+
new Two(list[2], list[3]),
18+
);
1219
case 1:
1320
return new Single(M, list[0]);
1421
case 2:
@@ -20,12 +27,5 @@ export function _from_medium_list(M, list) {
2027
empty(cache(M)),
2128
new One(list[2]),
2229
);
23-
case 4:
24-
return new Deep(
25-
M,
26-
new Two(list[0], list[1]),
27-
empty(cache(M)),
28-
new Two(list[2], list[3]),
29-
);
3030
}
3131
}

src/0-core/_fast/_prepend_small_list.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import assert from 'assert';
33
export default function _prepend_small_list(tree, list) {
44
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
55
// eslint-disable-next-line default-case
6-
switch (list.length) {
6+
switch (list.length & 0b11) {
7+
case 0:
8+
return tree.cons(list[3]).cons(list[2]).cons(list[1]).cons(list[0]);
79
case 1:
810
return tree.cons(list[0]);
911
case 2:
1012
return tree.cons(list[1]).cons(list[0]);
1113
case 3:
1214
return tree.cons(list[2]).cons(list[1]).cons(list[0]);
13-
case 4:
14-
return tree.cons(list[3]).cons(list[2]).cons(list[1]).cons(list[0]);
1515
}
1616
}

src/1-digit/1-One.js

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -91,26 +91,32 @@ One.prototype._nodes_with_list_and_one = function (M, list, other) {
9191
assert(other instanceof One);
9292
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
9393
// eslint-disable-next-line default-case
94-
switch (list.length) {
94+
switch (list.length & 0b11) {
95+
case 0:
96+
return [
97+
node3(M, other.a, list[0], list[1]),
98+
node3(M, list[2], list[3], this.a),
99+
];
95100
case 1:
96101
return [node3(M, other.a, list[0], this.a)];
97102
case 2:
98103
return [node2(M, other.a, list[0]), node2(M, list[1], this.a)];
99104
case 3:
100105
return [node3(M, other.a, list[0], list[1]), node2(M, list[2], this.a)];
101-
case 4:
102-
return [
103-
node3(M, other.a, list[0], list[1]),
104-
node3(M, list[2], list[3], this.a),
105-
];
106106
}
107107
};
108108

109109
One.prototype._nodes_with_list_and_two = function (M, list, other) {
110110
assert(other instanceof Two);
111111
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
112112
// eslint-disable-next-line default-case
113-
switch (list.length) {
113+
switch (list.length & 0b11) {
114+
case 0:
115+
return [
116+
other._node(M),
117+
node3(M, list[0], list[1], list[2]),
118+
node2(M, list[3], this.a),
119+
];
114120
case 1:
115121
return [other._node(M), node2(M, list[0], this.a)];
116122
case 2:
@@ -120,20 +126,20 @@ One.prototype._nodes_with_list_and_two = function (M, list, other) {
120126
node3(M, other.a, other.b, list[0]),
121127
node3(M, list[1], list[2], this.a),
122128
];
123-
case 4:
124-
return [
125-
other._node(M),
126-
node3(M, list[0], list[1], list[2]),
127-
node2(M, list[3], this.a),
128-
];
129129
}
130130
};
131131

132132
One.prototype._nodes_with_list_and_three = function (M, list, other) {
133133
assert(other instanceof Three);
134134
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
135135
// eslint-disable-next-line default-case
136-
switch (list.length) {
136+
switch (list.length & 0b11) {
137+
case 0:
138+
return [
139+
other._node(M),
140+
node2(M, list[0], list[1]),
141+
node3(M, list[2], list[3], this.a),
142+
];
137143
case 1:
138144
return [other._node(M), node2(M, list[0], this.a)];
139145
case 2:
@@ -144,20 +150,20 @@ One.prototype._nodes_with_list_and_three = function (M, list, other) {
144150
node2(M, list[0], list[1]),
145151
node2(M, list[2], this.a),
146152
];
147-
case 4:
148-
return [
149-
other._node(M),
150-
node2(M, list[0], list[1]),
151-
node3(M, list[2], list[3], this.a),
152-
];
153153
}
154154
};
155155

156156
One.prototype._nodes_with_list_and_four = function (M, list, other) {
157157
assert(other instanceof Four);
158158
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
159159
// eslint-disable-next-line default-case
160-
switch (list.length) {
160+
switch (list.length & 0b11) {
161+
case 0:
162+
return [
163+
node3(M, other.a, other.b, other.c),
164+
node3(M, other.d, list[0], list[1]),
165+
node3(M, list[2], list[3], this.a),
166+
];
161167
case 1:
162168
return [
163169
node3(M, other.a, other.b, other.c),
@@ -175,12 +181,6 @@ One.prototype._nodes_with_list_and_four = function (M, list, other) {
175181
node2(M, other.d, list[0]),
176182
node3(M, list[1], list[2], this.a),
177183
];
178-
case 4:
179-
return [
180-
node3(M, other.a, other.b, other.c),
181-
node3(M, other.d, list[0], list[1]),
182-
node3(M, list[2], list[3], this.a),
183-
];
184184
}
185185
};
186186

src/1-digit/2-Two.js

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,13 @@ Two.prototype._nodes_with_list_and_one = function (M, list, other) {
9696
assert(other instanceof One);
9797
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
9898
// eslint-disable-next-line default-case
99-
switch (list.length) {
99+
switch (list.length & 0b11) {
100+
case 0:
101+
return [
102+
node2(M, other.a, list[0]),
103+
node3(M, list[1], list[2], list[3]),
104+
this._node(M),
105+
];
100106
case 1:
101107
return [node2(M, other.a, list[0]), this._node(M)];
102108
case 2:
@@ -107,20 +113,20 @@ Two.prototype._nodes_with_list_and_one = function (M, list, other) {
107113
node2(M, list[1], list[2]),
108114
this._node(M),
109115
];
110-
case 4:
111-
return [
112-
node2(M, other.a, list[0]),
113-
node3(M, list[1], list[2], list[3]),
114-
this._node(M),
115-
];
116116
}
117117
};
118118

119119
Two.prototype._nodes_with_list_and_two = function (M, list, other) {
120120
assert(other instanceof Two);
121121
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
122122
// eslint-disable-next-line default-case
123-
switch (list.length) {
123+
switch (list.length & 0b11) {
124+
case 0:
125+
return [
126+
node3(M, other.a, other.b, list[0]),
127+
node2(M, list[1], list[2]),
128+
node3(M, list[3], this.a, this.b),
129+
];
124130
case 1:
125131
return [node3(M, other.a, other.b, list[0]), node2(M, this.a, this.b)];
126132
case 2:
@@ -131,20 +137,20 @@ Two.prototype._nodes_with_list_and_two = function (M, list, other) {
131137
node3(M, list[0], list[1], list[2]),
132138
node2(M, this.a, this.b),
133139
];
134-
case 4:
135-
return [
136-
node3(M, other.a, other.b, list[0]),
137-
node2(M, list[1], list[2]),
138-
node3(M, list[3], this.a, this.b),
139-
];
140140
}
141141
};
142142

143143
Two.prototype._nodes_with_list_and_three = function (M, list, other) {
144144
assert(other instanceof Three);
145145
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
146146
// eslint-disable-next-line default-case
147-
switch (list.length) {
147+
switch (list.length & 0b11) {
148+
case 0:
149+
return [
150+
node3(M, other.a, other.b, other.c),
151+
node3(M, list[0], list[1], list[2]),
152+
node3(M, list[3], this.a, this.b),
153+
];
148154
case 1:
149155
return [
150156
node3(M, other.a, other.b, other.c),
@@ -162,20 +168,21 @@ Two.prototype._nodes_with_list_and_three = function (M, list, other) {
162168
node2(M, list[0], list[1]),
163169
node3(M, list[2], this.a, this.b),
164170
];
165-
case 4:
166-
return [
167-
node3(M, other.a, other.b, other.c),
168-
node3(M, list[0], list[1], list[2]),
169-
node3(M, list[3], this.a, this.b),
170-
];
171171
}
172172
};
173173

174174
Two.prototype._nodes_with_list_and_four = function (M, list, other) {
175175
assert(other instanceof Four);
176176
assert(Number.isInteger(list.length) && list.length >= 1 && list.length <= 4);
177177
// eslint-disable-next-line default-case
178-
switch (list.length) {
178+
switch (list.length & 0b11) {
179+
case 0:
180+
return [
181+
node2(M, other.a, other.b),
182+
node3(M, other.c, other.d, list[0]),
183+
node3(M, list[1], list[2], list[3]),
184+
node2(M, this.a, this.b),
185+
];
179186
case 1:
180187
return [
181188
node2(M, other.a, other.b),
@@ -194,13 +201,6 @@ Two.prototype._nodes_with_list_and_four = function (M, list, other) {
194201
node3(M, other.d, list[0], list[1]),
195202
node3(M, list[2], this.a, this.b),
196203
];
197-
case 4:
198-
return [
199-
node2(M, other.a, other.b),
200-
node3(M, other.c, other.d, list[0]),
201-
node3(M, list[1], list[2], list[3]),
202-
node2(M, this.a, this.b),
203-
];
204204
}
205205
};
206206

0 commit comments

Comments
 (0)