Skip to content

Commit 5f9bb23

Browse files
committed
Share one TextDecoder for pack indexes
1 parent a257821 commit 5f9bb23

1 file changed

Lines changed: 5 additions & 4 deletions

File tree

‎src/browser/http.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export interface Pack {
1414
offsets: Map<string, { image: string; at: number }>;
1515
}
1616

17+
const utf8 = new TextDecoder();
18+
1719
// Pack layout: repeated [u16 path length][path][u32 size][bytes]. In the boot
1820
// pack, "/index.prefix" holds prefix-coded "d path" and "f path size
1921
// [image:offset] [pack]" lines. A file with an offset is read in ranges from
@@ -24,11 +26,10 @@ export interface Pack {
2426
export function parsePack(buf: ArrayBuffer): Pack {
2527
const dv = new DataView(buf);
2628
const u8 = new Uint8Array(buf);
27-
const dec = new TextDecoder();
2829
const files = new Map<string, Uint8Array>();
2930
for (let p = 0; p < buf.byteLength;) {
3031
const n = dv.getUint16(p);
31-
const path = dec.decode(u8.subarray(p + 2, p + 2 + n));
32+
const path = utf8.decode(u8.subarray(p + 2, p + 2 + n));
3233
p += 2 + n;
3334
const size = dv.getUint32(p);
3435
files.set(path, u8.subarray(p + 4, p + 4 + size));
@@ -38,7 +39,7 @@ export function parsePack(buf: ArrayBuffer): Pack {
3839
const raw = files.get("/index.prefix") ?? files.get("/index.txt");
3940
if (raw) {
4041
pack.index = new PackIndex(pack);
41-
addIndex(pack, dec.decode(raw), files.has("/index.prefix"));
42+
addIndex(pack, utf8.decode(raw), files.has("/index.prefix"));
4243
files.delete("/index.txt");
4344
files.delete("/index.prefix");
4445
for (const [p, data] of files) {
@@ -103,7 +104,7 @@ export class PackIndex extends Map<string, number> {
103104
const raw = this.modules.get(module);
104105
if (!raw) return;
105106
this.modules.delete(module);
106-
addIndex(this.pack, new TextDecoder().decode(raw), true);
107+
addIndex(this.pack, utf8.decode(raw), true);
107108
}
108109
}
109110

0 commit comments

Comments
 (0)