diff --git a/ts/README.md b/ts/README.md index 5c8be3395..8b2c0595d 100644 --- a/ts/README.md +++ b/ts/README.md @@ -69,6 +69,10 @@ and async in `rt/task.ts`. | `async fn` | a function returning `rt.Future`, around a generator | | `Poll`, `Waker`, `Context` | `T \| typeof rt.Pending`, refused if `T` can already be pending; `rt.Waker`, which is also the context | +`Vec` storage and `[x; N]` byte arrays up to 1 KiB share 8 KiB `ArrayBuffer`s, as Node's +`Buffer` pool does, so the `buffer` behind a view holds other bytes too: read it through +`byteOffset` and `byteLength`, and copy it before transferring it. + `usize` is 32 bits, as on wasm32, so it is exact in a `number`. Arithmetic is checked as a debug build checks it, so translated code panics where native code would: it never wraps silently. A panic throws `rt.Panic`, with the message native Rust panics with. diff --git a/ts/fixtures/codec/expected/coding/encode.ts b/ts/fixtures/codec/expected/coding/encode.ts index 544f3e84c..f4b768c4f 100644 --- a/ts/fixtures/codec/expected/coding/encode.ts +++ b/ts/fixtures/codec/expected/coding/encode.ts @@ -177,7 +177,7 @@ export class Encoder { this.buf.truncate(end); return rt.Ok(undefined); } - let size2: Uint8Array = new Uint8Array(9); + let size2: Uint8Array = rt.bytes(9); rt.slice.copyFromSlice(rt.slice.range(size2, 0, len), rt.slice.range(this.buf.view(), end)); this.buf.truncate(rt.usize.sub(rt.usize.add(end, len), 1)); rt.slice.copyWithin(this.buf.view(), body, end, rt.usize.sub(rt.usize.add(body, len), 1)); diff --git a/ts/fixtures/codec/expected/harness.ts b/ts/fixtures/codec/expected/harness.ts index 9746efb8e..fb0e69049 100644 --- a/ts/fixtures/codec/expected/harness.ts +++ b/ts/fixtures/codec/expected/harness.ts @@ -82,7 +82,7 @@ export function putUsize(out: rt.ByteVec, value: rt.usize): void { /** Take up to 8 big-endian bytes as a `u64`, zero-padded on the right. */ export function takeU64(data: Uint8Array): [rt.u64, Uint8Array] { - let buf: Uint8Array = new Uint8Array(8); + let buf: Uint8Array = rt.bytes(8); const n = rt.min(data.length, 8); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data, 0, n)); return [rt.u64.fromBeBytes(buf), rt.slice.range(data, n)]; diff --git a/ts/fixtures/lang/expected/fmt.ts b/ts/fixtures/lang/expected/fmt.ts index 6d60ea630..2a0c27c1c 100644 --- a/ts/fixtures/lang/expected/fmt.ts +++ b/ts/fixtures/lang/expected/fmt.ts @@ -13,7 +13,7 @@ export function ints(kind: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const value = rt.u8.rem(kind, 12); if (value === 0) { const data2 = data; - let buf: Uint8Array = new Uint8Array(1); + let buf: Uint8Array = rt.bytes(1); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); const v = rt.u8.fromBeBytes(buf); @@ -21,7 +21,7 @@ export function ints(kind: rt.u8, data: Uint8Array, out: rt.ByteVec): void { put(out, rt.fmt.toString((f: rt.Formatter) => rt.u8.display(v, f))); } else if (value === 1) { const data3 = data; - let buf2: Uint8Array = new Uint8Array(2); + let buf2: Uint8Array = rt.bytes(2); const n2 = rt.min(data3.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data3, 0, n2)); const v2 = rt.u16.fromBeBytes(buf2); @@ -29,7 +29,7 @@ export function ints(kind: rt.u8, data: Uint8Array, out: rt.ByteVec): void { put(out, rt.fmt.toString((f: rt.Formatter) => rt.u16.display(v2, f))); } else if (value === 2) { const data4 = data; - let buf3: Uint8Array = new Uint8Array(4); + let buf3: Uint8Array = rt.bytes(4); const n3 = rt.min(data4.length, buf3.length); rt.slice.copyFromSlice(rt.slice.range(buf3, 0, n3), rt.slice.range(data4, 0, n3)); const v3 = rt.u32.fromBeBytes(buf3); @@ -37,7 +37,7 @@ export function ints(kind: rt.u8, data: Uint8Array, out: rt.ByteVec): void { put(out, rt.fmt.toString((f: rt.Formatter) => rt.u32.display(v3, f))); } else if (value === 3) { const data5 = data; - let buf4: Uint8Array = new Uint8Array(8); + let buf4: Uint8Array = rt.bytes(8); const n4 = rt.min(data5.length, buf4.length); rt.slice.copyFromSlice(rt.slice.range(buf4, 0, n4), rt.slice.range(data5, 0, n4)); const v4 = rt.u64.fromBeBytes(buf4); @@ -45,7 +45,7 @@ export function ints(kind: rt.u8, data: Uint8Array, out: rt.ByteVec): void { put(out, rt.fmt.toString((f: rt.Formatter) => rt.u64.display(v4, f))); } else if (value === 4) { const data6 = data; - let buf5: Uint8Array = new Uint8Array(16); + let buf5: Uint8Array = rt.bytes(16); const n5 = rt.min(data6.length, buf5.length); rt.slice.copyFromSlice(rt.slice.range(buf5, 0, n5), rt.slice.range(data6, 0, n5)); const v5 = rt.u128.fromBeBytes(buf5); @@ -53,7 +53,7 @@ export function ints(kind: rt.u8, data: Uint8Array, out: rt.ByteVec): void { put(out, rt.fmt.toString((f: rt.Formatter) => rt.u128.display(v5, f))); } else if (value === 5) { const data7 = data; - let buf6: Uint8Array = new Uint8Array(4); + let buf6: Uint8Array = rt.bytes(4); const n6 = rt.min(data7.length, buf6.length); rt.slice.copyFromSlice(rt.slice.range(buf6, 0, n6), rt.slice.range(data7, 0, n6)); const v6 = rt.usize.fromBeBytes(buf6); @@ -61,7 +61,7 @@ export function ints(kind: rt.u8, data: Uint8Array, out: rt.ByteVec): void { put(out, rt.fmt.toString((f: rt.Formatter) => rt.usize.display(v6, f))); } else if (value === 6) { const data8 = data; - let buf7: Uint8Array = new Uint8Array(1); + let buf7: Uint8Array = rt.bytes(1); const n7 = rt.min(data8.length, buf7.length); rt.slice.copyFromSlice(rt.slice.range(buf7, 0, n7), rt.slice.range(data8, 0, n7)); const v7 = rt.i8.fromBeBytes(buf7); @@ -69,7 +69,7 @@ export function ints(kind: rt.u8, data: Uint8Array, out: rt.ByteVec): void { put(out, rt.fmt.toString((f: rt.Formatter) => rt.i8.display(v7, f))); } else if (value === 7) { const data9 = data; - let buf8: Uint8Array = new Uint8Array(2); + let buf8: Uint8Array = rt.bytes(2); const n8 = rt.min(data9.length, buf8.length); rt.slice.copyFromSlice(rt.slice.range(buf8, 0, n8), rt.slice.range(data9, 0, n8)); const v8 = rt.i16.fromBeBytes(buf8); @@ -77,7 +77,7 @@ export function ints(kind: rt.u8, data: Uint8Array, out: rt.ByteVec): void { put(out, rt.fmt.toString((f: rt.Formatter) => rt.i16.display(v8, f))); } else if (value === 8) { const data10 = data; - let buf9: Uint8Array = new Uint8Array(4); + let buf9: Uint8Array = rt.bytes(4); const n9 = rt.min(data10.length, buf9.length); rt.slice.copyFromSlice(rt.slice.range(buf9, 0, n9), rt.slice.range(data10, 0, n9)); const v9 = rt.i32.fromBeBytes(buf9); @@ -85,7 +85,7 @@ export function ints(kind: rt.u8, data: Uint8Array, out: rt.ByteVec): void { put(out, rt.fmt.toString((f: rt.Formatter) => rt.i32.display(v9, f))); } else if (value === 9) { const data11 = data; - let buf10: Uint8Array = new Uint8Array(8); + let buf10: Uint8Array = rt.bytes(8); const n10 = rt.min(data11.length, buf10.length); rt.slice.copyFromSlice(rt.slice.range(buf10, 0, n10), rt.slice.range(data11, 0, n10)); const v10 = rt.i64.fromBeBytes(buf10); @@ -93,7 +93,7 @@ export function ints(kind: rt.u8, data: Uint8Array, out: rt.ByteVec): void { put(out, rt.fmt.toString((f: rt.Formatter) => rt.i64.display(v10, f))); } else if (value === 10) { const data12 = data; - let buf11: Uint8Array = new Uint8Array(16); + let buf11: Uint8Array = rt.bytes(16); const n11 = rt.min(data12.length, buf11.length); rt.slice.copyFromSlice(rt.slice.range(buf11, 0, n11), rt.slice.range(data12, 0, n11)); const v11 = rt.i128.fromBeBytes(buf11); @@ -101,7 +101,7 @@ export function ints(kind: rt.u8, data: Uint8Array, out: rt.ByteVec): void { put(out, rt.fmt.toString((f: rt.Formatter) => rt.i128.display(v11, f))); } else { const data13 = data; - let buf12: Uint8Array = new Uint8Array(4); + let buf12: Uint8Array = rt.bytes(4); const n12 = rt.min(data13.length, buf12.length); rt.slice.copyFromSlice(rt.slice.range(buf12, 0, n12), rt.slice.range(data13, 0, n12)); const v12 = rt.isize.fromBeBytes(buf12); @@ -588,7 +588,7 @@ export function panics(kind: rt.u8, rest: Uint8Array, out: rt.ByteVec): void { const arg18 = rt.slice.range(rest, start, end); put(arg19, rt.fmt.format([(f: rt.Formatter) => rt.fmt.list(arg17, f, rt.u8.display), " ", (f: rt.Formatter) => rt.fmt.list(arg18, f, rt.u8.display)])); } else if (value === 15) { - let buf: Uint8Array = new Uint8Array(3); + let buf: Uint8Array = rt.bytes(3); rt.slice.copyFromSlice(buf, rt.slice.range(rest, 0, rt.min(rest.length, 4))); put(out, rt.fmt.format([(f: rt.Formatter) => rt.fmt.list(buf, f, rt.u8.display)])); } else if (value === 16) { diff --git a/ts/fixtures/lang/expected/ints.ts b/ts/fixtures/lang/expected/ints.ts index 23fa18acd..69a6429a4 100644 --- a/ts/fixtures/lang/expected/ints.ts +++ b/ts/fixtures/lang/expected/ints.ts @@ -5,12 +5,12 @@ import * as rt from "./rt.ts"; /** Apply operation `op` to operands taken from `data`. */ export function u8Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; - let buf: Uint8Array = new Uint8Array(1); + let buf: Uint8Array = rt.bytes(1); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); const [a, data3] = [rt.u8.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.u8, Uint8Array]; const data4 = data3; - let buf2: Uint8Array = new Uint8Array(1); + let buf2: Uint8Array = rt.bytes(1); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); const [b, data5] = [rt.u8.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.u8, Uint8Array]; @@ -97,12 +97,12 @@ export function u8Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { /** Apply operation `op` to operands taken from `data`. */ export function u16Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; - let buf: Uint8Array = new Uint8Array(2); + let buf: Uint8Array = rt.bytes(2); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); const [a, data3] = [rt.u16.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.u16, Uint8Array]; const data4 = data3; - let buf2: Uint8Array = new Uint8Array(2); + let buf2: Uint8Array = rt.bytes(2); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); const [b, data5] = [rt.u16.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.u16, Uint8Array]; @@ -189,12 +189,12 @@ export function u16Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { /** Apply operation `op` to operands taken from `data`. */ export function u32Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; - let buf: Uint8Array = new Uint8Array(4); + let buf: Uint8Array = rt.bytes(4); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); const [a, data3] = [rt.u32.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.u32, Uint8Array]; const data4 = data3; - let buf2: Uint8Array = new Uint8Array(4); + let buf2: Uint8Array = rt.bytes(4); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); const [b, data5] = [rt.u32.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.u32, Uint8Array]; @@ -281,12 +281,12 @@ export function u32Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { /** Apply operation `op` to operands taken from `data`. */ export function u64Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; - let buf: Uint8Array = new Uint8Array(8); + let buf: Uint8Array = rt.bytes(8); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); const [a, data3] = [rt.u64.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.u64, Uint8Array]; const data4 = data3; - let buf2: Uint8Array = new Uint8Array(8); + let buf2: Uint8Array = rt.bytes(8); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); const [b, data5] = [rt.u64.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.u64, Uint8Array]; @@ -372,12 +372,12 @@ export function u64Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { /** Apply operation `op` to operands taken from `data`. */ export function u128Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; - let buf: Uint8Array = new Uint8Array(16); + let buf: Uint8Array = rt.bytes(16); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); const [a, data3] = [rt.u128.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.u128, Uint8Array]; const data4 = data3; - let buf2: Uint8Array = new Uint8Array(16); + let buf2: Uint8Array = rt.bytes(16); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); const [b, data5] = [rt.u128.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.u128, Uint8Array]; @@ -464,12 +464,12 @@ export function u128Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { /** Apply operation `op` to operands taken from `data`. */ export function usizeOp(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; - let buf: Uint8Array = new Uint8Array(4); + let buf: Uint8Array = rt.bytes(4); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); const [a, data3] = [rt.usize.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.usize, Uint8Array]; const data4 = data3; - let buf2: Uint8Array = new Uint8Array(4); + let buf2: Uint8Array = rt.bytes(4); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); const [b, data5] = [rt.usize.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.usize, Uint8Array]; @@ -556,12 +556,12 @@ export function usizeOp(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { /** Apply operation `op` to operands taken from `data`. */ export function i8Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; - let buf: Uint8Array = new Uint8Array(1); + let buf: Uint8Array = rt.bytes(1); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); const [a, data3] = [rt.i8.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.i8, Uint8Array]; const data4 = data3; - let buf2: Uint8Array = new Uint8Array(1); + let buf2: Uint8Array = rt.bytes(1); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); const [b, data5] = [rt.i8.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.i8, Uint8Array]; @@ -648,12 +648,12 @@ export function i8Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { /** Apply operation `op` to operands taken from `data`. */ export function i16Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; - let buf: Uint8Array = new Uint8Array(2); + let buf: Uint8Array = rt.bytes(2); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); const [a, data3] = [rt.i16.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.i16, Uint8Array]; const data4 = data3; - let buf2: Uint8Array = new Uint8Array(2); + let buf2: Uint8Array = rt.bytes(2); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); const [b, data5] = [rt.i16.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.i16, Uint8Array]; @@ -740,12 +740,12 @@ export function i16Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { /** Apply operation `op` to operands taken from `data`. */ export function i32Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; - let buf: Uint8Array = new Uint8Array(4); + let buf: Uint8Array = rt.bytes(4); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); const [a, data3] = [rt.i32.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.i32, Uint8Array]; const data4 = data3; - let buf2: Uint8Array = new Uint8Array(4); + let buf2: Uint8Array = rt.bytes(4); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); const [b, data5] = [rt.i32.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.i32, Uint8Array]; @@ -832,12 +832,12 @@ export function i32Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { /** Apply operation `op` to operands taken from `data`. */ export function i64Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; - let buf: Uint8Array = new Uint8Array(8); + let buf: Uint8Array = rt.bytes(8); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); const [a, data3] = [rt.i64.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.i64, Uint8Array]; const data4 = data3; - let buf2: Uint8Array = new Uint8Array(8); + let buf2: Uint8Array = rt.bytes(8); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); const [b, data5] = [rt.i64.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.i64, Uint8Array]; @@ -923,12 +923,12 @@ export function i64Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { /** Apply operation `op` to operands taken from `data`. */ export function i128Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; - let buf: Uint8Array = new Uint8Array(16); + let buf: Uint8Array = rt.bytes(16); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); const [a, data3] = [rt.i128.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.i128, Uint8Array]; const data4 = data3; - let buf2: Uint8Array = new Uint8Array(16); + let buf2: Uint8Array = rt.bytes(16); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); const [b, data5] = [rt.i128.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.i128, Uint8Array]; @@ -1015,12 +1015,12 @@ export function i128Op(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { /** Apply operation `op` to operands taken from `data`. */ export function isizeOp(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { const data2 = data; - let buf: Uint8Array = new Uint8Array(4); + let buf: Uint8Array = rt.bytes(4); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); const [a, data3] = [rt.isize.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.isize, Uint8Array]; const data4 = data3; - let buf2: Uint8Array = new Uint8Array(4); + let buf2: Uint8Array = rt.bytes(4); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); const [b, data5] = [rt.isize.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.isize, Uint8Array]; @@ -1107,7 +1107,7 @@ export function isizeOp(op: rt.u8, data: Uint8Array, out: rt.ByteVec): void { /** Negate, which overflows at the minimum. */ export function i8Neg(data: Uint8Array, out: rt.ByteVec): void { const data2 = data; - let buf: Uint8Array = new Uint8Array(1); + let buf: Uint8Array = rt.bytes(1); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); const [a, ] = [rt.i8.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.i8, Uint8Array]; @@ -1117,7 +1117,7 @@ export function i8Neg(data: Uint8Array, out: rt.ByteVec): void { /** Negate, which overflows at the minimum. */ export function i16Neg(data: Uint8Array, out: rt.ByteVec): void { const data2 = data; - let buf: Uint8Array = new Uint8Array(2); + let buf: Uint8Array = rt.bytes(2); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); const [a, ] = [rt.i16.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.i16, Uint8Array]; @@ -1127,7 +1127,7 @@ export function i16Neg(data: Uint8Array, out: rt.ByteVec): void { /** Negate, which overflows at the minimum. */ export function i32Neg(data: Uint8Array, out: rt.ByteVec): void { const data2 = data; - let buf: Uint8Array = new Uint8Array(4); + let buf: Uint8Array = rt.bytes(4); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); const [a, ] = [rt.i32.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.i32, Uint8Array]; @@ -1137,7 +1137,7 @@ export function i32Neg(data: Uint8Array, out: rt.ByteVec): void { /** Negate, which overflows at the minimum. */ export function i64Neg(data: Uint8Array, out: rt.ByteVec): void { const data2 = data; - let buf: Uint8Array = new Uint8Array(8); + let buf: Uint8Array = rt.bytes(8); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); const [a, ] = [rt.i64.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.i64, Uint8Array]; @@ -1147,7 +1147,7 @@ export function i64Neg(data: Uint8Array, out: rt.ByteVec): void { /** Negate, which overflows at the minimum. */ export function i128Neg(data: Uint8Array, out: rt.ByteVec): void { const data2 = data; - let buf: Uint8Array = new Uint8Array(16); + let buf: Uint8Array = rt.bytes(16); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); const [a, ] = [rt.i128.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.i128, Uint8Array]; @@ -1157,7 +1157,7 @@ export function i128Neg(data: Uint8Array, out: rt.ByteVec): void { /** Negate, which overflows at the minimum. */ export function isizeNeg(data: Uint8Array, out: rt.ByteVec): void { const data2 = data; - let buf: Uint8Array = new Uint8Array(4); + let buf: Uint8Array = rt.bytes(4); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); const [a, ] = [rt.isize.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.isize, Uint8Array]; @@ -1169,7 +1169,7 @@ export function cast(from: rt.u8, to: rt.u8, data: Uint8Array, out: rt.ByteVec): const value = rt.u8.rem(from, 12); if (value === 0) { const data2 = data; - let buf: Uint8Array = new Uint8Array(1); + let buf: Uint8Array = rt.bytes(1); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); const v = ([rt.u8.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.u8, Uint8Array])[0]; @@ -1226,7 +1226,7 @@ export function cast(from: rt.u8, to: rt.u8, data: Uint8Array, out: rt.ByteVec): } } else if (value === 1) { const data3 = data; - let buf2: Uint8Array = new Uint8Array(2); + let buf2: Uint8Array = rt.bytes(2); const n2 = rt.min(data3.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data3, 0, n2)); const v14 = ([rt.u16.fromBeBytes(buf2), rt.slice.range(data3, n2)] as [rt.u16, Uint8Array])[0]; @@ -1283,7 +1283,7 @@ export function cast(from: rt.u8, to: rt.u8, data: Uint8Array, out: rt.ByteVec): } } else if (value === 2) { const data4 = data; - let buf3: Uint8Array = new Uint8Array(4); + let buf3: Uint8Array = rt.bytes(4); const n3 = rt.min(data4.length, buf3.length); rt.slice.copyFromSlice(rt.slice.range(buf3, 0, n3), rt.slice.range(data4, 0, n3)); const v27 = ([rt.u32.fromBeBytes(buf3), rt.slice.range(data4, n3)] as [rt.u32, Uint8Array])[0]; @@ -1340,7 +1340,7 @@ export function cast(from: rt.u8, to: rt.u8, data: Uint8Array, out: rt.ByteVec): } } else if (value === 3) { const data5 = data; - let buf4: Uint8Array = new Uint8Array(8); + let buf4: Uint8Array = rt.bytes(8); const n4 = rt.min(data5.length, buf4.length); rt.slice.copyFromSlice(rt.slice.range(buf4, 0, n4), rt.slice.range(data5, 0, n4)); const v40 = ([rt.u64.fromBeBytes(buf4), rt.slice.range(data5, n4)] as [rt.u64, Uint8Array])[0]; @@ -1397,7 +1397,7 @@ export function cast(from: rt.u8, to: rt.u8, data: Uint8Array, out: rt.ByteVec): } } else if (value === 4) { const data6 = data; - let buf5: Uint8Array = new Uint8Array(16); + let buf5: Uint8Array = rt.bytes(16); const n5 = rt.min(data6.length, buf5.length); rt.slice.copyFromSlice(rt.slice.range(buf5, 0, n5), rt.slice.range(data6, 0, n5)); const v53 = ([rt.u128.fromBeBytes(buf5), rt.slice.range(data6, n5)] as [rt.u128, Uint8Array])[0]; @@ -1454,7 +1454,7 @@ export function cast(from: rt.u8, to: rt.u8, data: Uint8Array, out: rt.ByteVec): } } else if (value === 5) { const data7 = data; - let buf6: Uint8Array = new Uint8Array(4); + let buf6: Uint8Array = rt.bytes(4); const n6 = rt.min(data7.length, buf6.length); rt.slice.copyFromSlice(rt.slice.range(buf6, 0, n6), rt.slice.range(data7, 0, n6)); const v66 = ([rt.usize.fromBeBytes(buf6), rt.slice.range(data7, n6)] as [rt.usize, Uint8Array])[0]; @@ -1511,7 +1511,7 @@ export function cast(from: rt.u8, to: rt.u8, data: Uint8Array, out: rt.ByteVec): } } else if (value === 6) { const data8 = data; - let buf7: Uint8Array = new Uint8Array(1); + let buf7: Uint8Array = rt.bytes(1); const n7 = rt.min(data8.length, buf7.length); rt.slice.copyFromSlice(rt.slice.range(buf7, 0, n7), rt.slice.range(data8, 0, n7)); const v79 = ([rt.i8.fromBeBytes(buf7), rt.slice.range(data8, n7)] as [rt.i8, Uint8Array])[0]; @@ -1568,7 +1568,7 @@ export function cast(from: rt.u8, to: rt.u8, data: Uint8Array, out: rt.ByteVec): } } else if (value === 7) { const data9 = data; - let buf8: Uint8Array = new Uint8Array(2); + let buf8: Uint8Array = rt.bytes(2); const n8 = rt.min(data9.length, buf8.length); rt.slice.copyFromSlice(rt.slice.range(buf8, 0, n8), rt.slice.range(data9, 0, n8)); const v92 = ([rt.i16.fromBeBytes(buf8), rt.slice.range(data9, n8)] as [rt.i16, Uint8Array])[0]; @@ -1625,7 +1625,7 @@ export function cast(from: rt.u8, to: rt.u8, data: Uint8Array, out: rt.ByteVec): } } else if (value === 8) { const data10 = data; - let buf9: Uint8Array = new Uint8Array(4); + let buf9: Uint8Array = rt.bytes(4); const n9 = rt.min(data10.length, buf9.length); rt.slice.copyFromSlice(rt.slice.range(buf9, 0, n9), rt.slice.range(data10, 0, n9)); const v105 = ([rt.i32.fromBeBytes(buf9), rt.slice.range(data10, n9)] as [rt.i32, Uint8Array])[0]; @@ -1682,7 +1682,7 @@ export function cast(from: rt.u8, to: rt.u8, data: Uint8Array, out: rt.ByteVec): } } else if (value === 9) { const data11 = data; - let buf10: Uint8Array = new Uint8Array(8); + let buf10: Uint8Array = rt.bytes(8); const n10 = rt.min(data11.length, buf10.length); rt.slice.copyFromSlice(rt.slice.range(buf10, 0, n10), rt.slice.range(data11, 0, n10)); const v118 = ([rt.i64.fromBeBytes(buf10), rt.slice.range(data11, n10)] as [rt.i64, Uint8Array])[0]; @@ -1739,7 +1739,7 @@ export function cast(from: rt.u8, to: rt.u8, data: Uint8Array, out: rt.ByteVec): } } else if (value === 10) { const data12 = data; - let buf11: Uint8Array = new Uint8Array(16); + let buf11: Uint8Array = rt.bytes(16); const n11 = rt.min(data12.length, buf11.length); rt.slice.copyFromSlice(rt.slice.range(buf11, 0, n11), rt.slice.range(data12, 0, n11)); const v131 = ([rt.i128.fromBeBytes(buf11), rt.slice.range(data12, n11)] as [rt.i128, Uint8Array])[0]; @@ -1796,7 +1796,7 @@ export function cast(from: rt.u8, to: rt.u8, data: Uint8Array, out: rt.ByteVec): } } else { const data13 = data; - let buf12: Uint8Array = new Uint8Array(4); + let buf12: Uint8Array = rt.bytes(4); const n12 = rt.min(data13.length, buf12.length); rt.slice.copyFromSlice(rt.slice.range(buf12, 0, n12), rt.slice.range(data13, 0, n12)); const v144 = ([rt.isize.fromBeBytes(buf12), rt.slice.range(data13, n12)] as [rt.isize, Uint8Array])[0]; @@ -1857,12 +1857,12 @@ export function cast(from: rt.u8, to: rt.u8, data: Uint8Array, out: rt.ByteVec): /** Conversions that check, and ones that cannot fail. */ export function convert(data: Uint8Array, out: rt.ByteVec): void { const data2 = data; - let buf: Uint8Array = new Uint8Array(8); + let buf: Uint8Array = rt.bytes(8); const n = rt.min(data2.length, buf.length); rt.slice.copyFromSlice(rt.slice.range(buf, 0, n), rt.slice.range(data2, 0, n)); const [wide, data3] = [rt.u64.fromBeBytes(buf), rt.slice.range(data2, n)] as [rt.u64, Uint8Array]; const data4 = data3; - let buf2: Uint8Array = new Uint8Array(4); + let buf2: Uint8Array = rt.bytes(4); const n2 = rt.min(data4.length, buf2.length); rt.slice.copyFromSlice(rt.slice.range(buf2, 0, n2), rt.slice.range(data4, 0, n2)); const [signed, ] = [rt.i32.fromBeBytes(buf2), rt.slice.range(data4, n2)] as [rt.i32, Uint8Array]; diff --git a/ts/runtime/rt.ts b/ts/runtime/rt.ts index 8bbd20f7c..868e8c859 100644 --- a/ts/runtime/rt.ts +++ b/ts/runtime/rt.ts @@ -805,6 +805,27 @@ export const slice = { }, }; +const SLAB = 8192; +let slab = new ArrayBuffer(SLAB); +let slabUsed = 0; + +/** + * `n` zeroed bytes. Up to 1 KiB, they share an `ArrayBuffer`, as Node's `Buffer` pool + * does: a lone array's own buffer costs V8 about a microsecond, made when the array is + * past 64 bytes or else the first time something views it. No byte is handed out twice. + */ +export function bytes(n: number): Uint8Array { + if (n > SLAB / 8) return new Uint8Array(n); + if (slabUsed + n > SLAB) { + slab = new ArrayBuffer(SLAB); + slabUsed = 0; + } + const out = new Uint8Array(slab, slabUsed, n); + // Aligned, as Node aligns its pool. + slabUsed += (n + 7) & ~7; + return out; +} + /** `Vec`: a growable byte buffer. */ export class ByteVec { /** The storage; only the first `length` bytes are the vector. */ @@ -812,7 +833,7 @@ export class ByteVec { length = 0; constructor(capacity = 0) { - this.data = new Uint8Array(capacity); + this.data = bytes(capacity); } static from(bytes: Uint8Array): ByteVec { @@ -826,9 +847,8 @@ export class ByteVec { reserve(additional: number): void { const needed = this.length + additional; if (needed <= this.data.length) return; - const grown = new Uint8Array(Math.max(needed, this.data.length * 2, 8)); - // All of `data`, not a view of it: a view of a small array makes V8 move it off its - // heap, which costs more than copying the few bytes past `length`. + const grown = bytes(Math.max(needed, this.data.length * 2, 8)); + // All of `data`, which costs less than making a view of its first `length` bytes. grown.set(this.data); this.data = grown; } diff --git a/ts/src/lower/body.rs b/ts/src/lower/body.rs index 12afe0c62..3bf1a5c68 100644 --- a/ts/src/lower/body.rs +++ b/ts/src/lower/body.rs @@ -2258,7 +2258,7 @@ impl<'b, 'a, 'tcx> Body<'b, 'a, 'tcx> { let v = self.operand(*value, out); match elem_ty.kind() { ty::Uint(ty::UintTy::U8) => { - let array = Expr::New(Box::new(Expr::ident("Uint8Array")), vec![Expr::lit(n.to_string())]); + let array = Expr::rt("bytes", vec![Expr::lit(n.to_string())]); match &v { Expr::Lit(text) if text == "0" => array, _ => array.member("fill").call(vec![v]),