bin-serde 1.6.3 → 1.6.4
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 +13 -5
- package/lib/index.js +12 -5
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -7,6 +7,9 @@ const MAX_POOLED = 4096;
|
|
|
7
7
|
let slab: Uint8Array = new Uint8Array(SLAB_SIZE);
|
|
8
8
|
let slabOffset = 0;
|
|
9
9
|
|
|
10
|
+
const f32 = new Float32Array(1);
|
|
11
|
+
const f32u8 = new Uint8Array(f32.buffer);
|
|
12
|
+
|
|
10
13
|
function allocFromSlab(size: number): Uint8Array {
|
|
11
14
|
if (size > MAX_POOLED) {
|
|
12
15
|
// Too large for pool, allocate directly
|
|
@@ -111,8 +114,11 @@ export class Writer {
|
|
|
111
114
|
|
|
112
115
|
writeFloat(val: number) {
|
|
113
116
|
this.ensureSize(4);
|
|
114
|
-
|
|
115
|
-
this.pos
|
|
117
|
+
f32[0] = val;
|
|
118
|
+
this.bytes[this.pos++] = f32u8[0]!;
|
|
119
|
+
this.bytes[this.pos++] = f32u8[1]!;
|
|
120
|
+
this.bytes[this.pos++] = f32u8[2]!;
|
|
121
|
+
this.bytes[this.pos++] = f32u8[3]!;
|
|
116
122
|
return this;
|
|
117
123
|
}
|
|
118
124
|
|
|
@@ -258,9 +264,11 @@ export class Reader {
|
|
|
258
264
|
}
|
|
259
265
|
|
|
260
266
|
readFloat() {
|
|
261
|
-
|
|
262
|
-
this.pos
|
|
263
|
-
|
|
267
|
+
f32u8[0] = this.bytes[this.pos++];
|
|
268
|
+
f32u8[1] = this.bytes[this.pos++];
|
|
269
|
+
f32u8[2] = this.bytes[this.pos++];
|
|
270
|
+
f32u8[3] = this.bytes[this.pos++];
|
|
271
|
+
return f32[0];
|
|
264
272
|
}
|
|
265
273
|
|
|
266
274
|
readBits(numBits: number) {
|
package/lib/index.js
CHANGED
|
@@ -4,6 +4,8 @@ const SLAB_SIZE = 8192;
|
|
|
4
4
|
const MAX_POOLED = 4096;
|
|
5
5
|
let slab = new Uint8Array(SLAB_SIZE);
|
|
6
6
|
let slabOffset = 0;
|
|
7
|
+
const f32 = new Float32Array(1);
|
|
8
|
+
const f32u8 = new Uint8Array(f32.buffer);
|
|
7
9
|
function allocFromSlab(size) {
|
|
8
10
|
if (size > MAX_POOLED) {
|
|
9
11
|
// Too large for pool, allocate directly
|
|
@@ -96,8 +98,11 @@ export class Writer {
|
|
|
96
98
|
}
|
|
97
99
|
writeFloat(val) {
|
|
98
100
|
this.ensureSize(4);
|
|
99
|
-
|
|
100
|
-
this.pos
|
|
101
|
+
f32[0] = val;
|
|
102
|
+
this.bytes[this.pos++] = f32u8[0];
|
|
103
|
+
this.bytes[this.pos++] = f32u8[1];
|
|
104
|
+
this.bytes[this.pos++] = f32u8[2];
|
|
105
|
+
this.bytes[this.pos++] = f32u8[3];
|
|
101
106
|
return this;
|
|
102
107
|
}
|
|
103
108
|
writeBits(bits) {
|
|
@@ -227,9 +232,11 @@ export class Reader {
|
|
|
227
232
|
return val % 2 === 0 ? val / 2 : -(val + 1) / 2;
|
|
228
233
|
}
|
|
229
234
|
readFloat() {
|
|
230
|
-
|
|
231
|
-
this.pos
|
|
232
|
-
|
|
235
|
+
f32u8[0] = this.bytes[this.pos++];
|
|
236
|
+
f32u8[1] = this.bytes[this.pos++];
|
|
237
|
+
f32u8[2] = this.bytes[this.pos++];
|
|
238
|
+
f32u8[3] = this.bytes[this.pos++];
|
|
239
|
+
return f32[0];
|
|
233
240
|
}
|
|
234
241
|
readBits(numBits) {
|
|
235
242
|
const numBytes = Math.ceil(numBits / 8);
|