Skip to content

Commit 7bddb51

Browse files
committed
perf: speedup windows1252toString with unrolled loops
1 parent d465b97 commit 7bddb51

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

fallback/windows1252.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@ const map = [
1212

1313
function mappedCopy(arr, start = 0) {
1414
const out = Uint16Array.from(start === 0 ? arr : arr.subarray(start)) // copy to modify in-place, also those are 16-bit now
15-
const end = out.length
16-
for (let i = 0; i < end; i++) {
15+
let i = 0
16+
for (const end3 = out.length - 3; i < end3; i += 4) {
17+
const c0 = out[i], c1 = out[i + 1], c2 = out[i + 2], c3 = out[i + 3] // prettier-ignore
18+
if ((c0 & 0xe0) === 0x80) out[i] = map[c0 & 0x7f]
19+
if ((c1 & 0xe0) === 0x80) out[i + 1] = map[c1 & 0x7f]
20+
if ((c2 & 0xe0) === 0x80) out[i + 2] = map[c2 & 0x7f]
21+
if ((c3 & 0xe0) === 0x80) out[i + 3] = map[c3 & 0x7f]
22+
}
23+
24+
for (const end = out.length; i < end; i++) {
1725
const c = out[i]
18-
if ((c & 0b1110_0000) === 0b1000_0000) out[i] = map[c & 0x7f] // 3 high bytes must match for 0x80-0x9f range
26+
if ((c & 0xe0) === 0x80) out[i] = map[c & 0x7f] // 3 high bytes must match for 0x80-0x9f range
1927
}
2028

2129
return out
@@ -24,10 +32,19 @@ function mappedCopy(arr, start = 0) {
2432
// Faster in Hermes
2533
function mappedZero(arr, start = 0) {
2634
const out = new Uint16Array(arr.length - start)
27-
const length = out.length
28-
for (let i = 0, j = start; i < length; i++, j++) {
35+
let i = 0
36+
let j = start
37+
for (const end3 = out.length - 3; i < end3; i += 4, j += 4) {
38+
const c0 = arr[j], c1 = arr[j + 1], c2 = arr[j + 2], c3 = arr[j + 3] // prettier-ignore
39+
out[i] = (c0 & 0xe0) === 0x80 ? map[c0 & 0x7f] : c0
40+
out[i + 1] = (c1 & 0xe0) === 0x80 ? map[c1 & 0x7f] : c1
41+
out[i + 2] = (c2 & 0xe0) === 0x80 ? map[c2 & 0x7f] : c2
42+
out[i + 3] = (c3 & 0xe0) === 0x80 ? map[c3 & 0x7f] : c3
43+
}
44+
45+
for (const end = out.length; i < end; i++, j++) {
2946
const c = arr[j]
30-
out[i] = (c & 0b1110_0000) === 0b1000_0000 ? map[c & 0x7f] : c // 3 high bytes must match for 0x80-0x9f range
47+
out[i] = (c & 0xe0) === 0x80 ? map[c & 0x7f] : c // 3 high bytes must match for 0x80-0x9f range
3148
}
3249

3350
return out

0 commit comments

Comments
 (0)