bin-serde 1.5.1 → 1.6.1

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.
Files changed (3) hide show
  1. package/index.ts +26 -6
  2. package/lib/index.js +22 -5
  3. package/package.json +1 -1
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;
@@ -164,7 +184,7 @@ export class Writer {
164
184
 
165
185
  private get view(): DataView {
166
186
  if (!this._view) {
167
- this._view = new DataView(this.bytes.buffer);
187
+ this._view = new DataView(this.bytes.buffer, this.bytes.byteOffset);
168
188
  }
169
189
  return this._view;
170
190
  }
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;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bin-serde",
3
- "version": "1.5.1",
3
+ "version": "1.6.1",
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",