bin-serde 1.5.0 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.ts CHANGED
@@ -1,13 +1,34 @@
1
1
  import { pack, unpack } from "./utf8-buffer.js";
2
2
  import utf8Size from "utf8-buffer-size";
3
3
 
4
+ const SLAB_SIZE = 8192;
5
+ const MAX_POOLED = 4096;
6
+
7
+ let slab: Uint8Array = new Uint8Array(SLAB_SIZE);
8
+ let slabOffset = 0;
9
+
10
+ function allocFromSlab(size: number): Uint8Array {
11
+ if (size > MAX_POOLED) {
12
+ // Too large for pool, allocate directly
13
+ return new Uint8Array(size);
14
+ }
15
+ if (slabOffset + size > SLAB_SIZE) {
16
+ // Slab full, allocate new one
17
+ slab = new Uint8Array(SLAB_SIZE);
18
+ slabOffset = 0;
19
+ }
20
+ const buf = slab.subarray(slabOffset, slabOffset + size);
21
+ slabOffset += size;
22
+ return buf;
23
+ }
24
+
4
25
  export class Writer {
5
26
  private pos = 0;
6
27
  private bytes: Uint8Array;
7
- private _view: DataView | null = null;
28
+ private _view: DataView | null = null; // lazily allocated
8
29
 
9
30
  constructor(initialSize = 256) {
10
- this.bytes = new Uint8Array(Math.max(initialSize, 16));
31
+ this.bytes = allocFromSlab(Math.max(initialSize, 16));
11
32
  }
12
33
 
13
34
  writeUInt8(val: number) {
@@ -60,8 +81,7 @@ export class Writer {
60
81
  }
61
82
 
62
83
  writeVarint(val: number) {
63
- this.writeUVarint((val << 1) ^ (val >> 31));
64
- return this;
84
+ return this.writeUVarint((val << 1) ^ (val >> 31));
65
85
  }
66
86
 
67
87
  writeFloat(val: number) {
@@ -156,7 +176,7 @@ export class Writer {
156
176
  while (newSize < this.pos + size) {
157
177
  newSize *= 2;
158
178
  }
159
- const newBytes = new Uint8Array(newSize);
179
+ const newBytes = allocFromSlab(newSize);
160
180
  newBytes.set(this.bytes);
161
181
  this.bytes = newBytes;
162
182
  this._view = null;
@@ -184,6 +204,12 @@ export class Reader {
184
204
  return this.bytes[this.pos++];
185
205
  }
186
206
 
207
+ readUInt16() {
208
+ const val = this.view.getUint16(this.pos);
209
+ this.pos += 2;
210
+ return val;
211
+ }
212
+
187
213
  readUInt32() {
188
214
  const val = this.view.getUint32(this.pos);
189
215
  this.pos += 4;
package/lib/index.d.ts CHANGED
@@ -25,6 +25,7 @@ export declare class Reader {
25
25
  private view;
26
26
  constructor(buf: Uint8Array);
27
27
  readUInt8(): number;
28
+ readUInt16(): number;
28
29
  readUInt32(): number;
29
30
  readUVarint(): number;
30
31
  readVarint(): number;
package/lib/index.js CHANGED
@@ -1,11 +1,29 @@
1
1
  import { pack, unpack } from "./utf8-buffer.js";
2
2
  import utf8Size from "utf8-buffer-size";
3
+ const SLAB_SIZE = 8192;
4
+ const MAX_POOLED = 4096;
5
+ let slab = new Uint8Array(SLAB_SIZE);
6
+ let slabOffset = 0;
7
+ function allocFromSlab(size) {
8
+ if (size > MAX_POOLED) {
9
+ // Too large for pool, allocate directly
10
+ return new Uint8Array(size);
11
+ }
12
+ if (slabOffset + size > SLAB_SIZE) {
13
+ // Slab full, allocate new one
14
+ slab = new Uint8Array(SLAB_SIZE);
15
+ slabOffset = 0;
16
+ }
17
+ const buf = slab.subarray(slabOffset, slabOffset + size);
18
+ slabOffset += size;
19
+ return buf;
20
+ }
3
21
  export class Writer {
4
22
  pos = 0;
5
23
  bytes;
6
- _view = null;
24
+ _view = null; // lazily allocated
7
25
  constructor(initialSize = 256) {
8
- this.bytes = new Uint8Array(Math.max(initialSize, 16));
26
+ this.bytes = allocFromSlab(Math.max(initialSize, 16));
9
27
  }
10
28
  writeUInt8(val) {
11
29
  this.ensureSize(1);
@@ -53,8 +71,7 @@ export class Writer {
53
71
  return this;
54
72
  }
55
73
  writeVarint(val) {
56
- this.writeUVarint((val << 1) ^ (val >> 31));
57
- return this;
74
+ return this.writeUVarint((val << 1) ^ (val >> 31));
58
75
  }
59
76
  writeFloat(val) {
60
77
  this.ensureSize(4);
@@ -141,7 +158,7 @@ export class Writer {
141
158
  while (newSize < this.pos + size) {
142
159
  newSize *= 2;
143
160
  }
144
- const newBytes = new Uint8Array(newSize);
161
+ const newBytes = allocFromSlab(newSize);
145
162
  newBytes.set(this.bytes);
146
163
  this.bytes = newBytes;
147
164
  this._view = null;
@@ -164,6 +181,11 @@ export class Reader {
164
181
  readUInt8() {
165
182
  return this.bytes[this.pos++];
166
183
  }
184
+ readUInt16() {
185
+ const val = this.view.getUint16(this.pos);
186
+ this.pos += 2;
187
+ return val;
188
+ }
167
189
  readUInt32() {
168
190
  const val = this.view.getUint32(this.pos);
169
191
  this.pos += 4;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bin-serde",
3
- "version": "1.5.0",
3
+ "version": "1.6.0",
4
4
  "description": "A low level library for efficiently writing and reading binary data in javascript",
5
5
  "type": "module",
6
6
  "main": "lib/index.js",