@nxtedition/slice 1.0.10 → 1.0.12
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/lib/index.d.ts +6 -0
- package/lib/index.js +38 -14
- package/package.json +2 -2
package/lib/index.d.ts
CHANGED
|
@@ -26,6 +26,7 @@ export declare class PoolAllocator {
|
|
|
26
26
|
constructor(poolTotal?: number);
|
|
27
27
|
get size(): number;
|
|
28
28
|
isFromPool(slice: Slice | null | undefined): boolean;
|
|
29
|
+
realloc(byteLength: number): Slice;
|
|
29
30
|
realloc(slice: Slice, byteLength: number): Slice;
|
|
30
31
|
get stats(): {
|
|
31
32
|
size: number;
|
|
@@ -35,5 +36,10 @@ export declare class PoolAllocator {
|
|
|
35
36
|
poolUsed: number;
|
|
36
37
|
poolSize: number;
|
|
37
38
|
poolCount: number;
|
|
39
|
+
buckets: {
|
|
40
|
+
free: number;
|
|
41
|
+
used: number;
|
|
42
|
+
size: number;
|
|
43
|
+
}[];
|
|
38
44
|
};
|
|
39
45
|
}
|
package/lib/index.js
CHANGED
|
@@ -228,7 +228,8 @@ export class PoolAllocator {
|
|
|
228
228
|
#size = 0
|
|
229
229
|
#padding = 0
|
|
230
230
|
|
|
231
|
-
#
|
|
231
|
+
#poolsBucket = []
|
|
232
|
+
#poolsBucketUsed = []
|
|
232
233
|
#poolBuffer
|
|
233
234
|
#poolOffset = 0
|
|
234
235
|
#poolSize = 0
|
|
@@ -237,7 +238,8 @@ export class PoolAllocator {
|
|
|
237
238
|
constructor(poolTotal = 128 * 1024 * 1024) {
|
|
238
239
|
this.#poolBuffer = Buffer.allocUnsafe(Number.isFinite(poolTotal) ? poolTotal : 0)
|
|
239
240
|
for (let n = 0; 2 ** n <= 256 * 1024; n++) {
|
|
240
|
-
this.#
|
|
241
|
+
this.#poolsBucket.push([])
|
|
242
|
+
this.#poolsBucketUsed.push(0)
|
|
241
243
|
}
|
|
242
244
|
}
|
|
243
245
|
|
|
@@ -249,19 +251,28 @@ export class PoolAllocator {
|
|
|
249
251
|
return slice != null && slice.buffer === this.#poolBuffer
|
|
250
252
|
}
|
|
251
253
|
|
|
252
|
-
|
|
253
|
-
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
realloc(slice , byteLength ) {
|
|
257
|
+
if (typeof slice === 'number') {
|
|
258
|
+
byteLength = slice
|
|
259
|
+
slice = new Slice()
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if (byteLength == null || !Number.isInteger(byteLength) || byteLength < 0) {
|
|
254
263
|
throw new TypeError(`Invalid byteLength: ${byteLength}`)
|
|
255
264
|
}
|
|
256
265
|
|
|
257
|
-
if (slice
|
|
266
|
+
if (slice == null) {
|
|
267
|
+
slice = new Slice()
|
|
268
|
+
} else if (slice.byteLength === byteLength) {
|
|
258
269
|
return slice
|
|
259
270
|
}
|
|
260
271
|
|
|
261
272
|
// Ceil to nearest power of two.
|
|
262
273
|
const dstIdx = byteLength <= 8 ? 3 : 32 - Math.clz32(byteLength - 1)
|
|
263
274
|
|
|
264
|
-
if (slice
|
|
275
|
+
if (slice.buffer === this.#poolBuffer) {
|
|
265
276
|
const srcIdx = 32 - Math.clz32(slice.maxByteLength - 1)
|
|
266
277
|
|
|
267
278
|
if (slice.maxByteLength !== 1 << srcIdx) {
|
|
@@ -278,7 +289,8 @@ export class PoolAllocator {
|
|
|
278
289
|
return slice
|
|
279
290
|
}
|
|
280
291
|
|
|
281
|
-
this.#
|
|
292
|
+
this.#poolsBucket[srcIdx].push(slice.byteOffset)
|
|
293
|
+
this.#poolsBucketUsed[srcIdx] -= 1
|
|
282
294
|
this.#poolSize -= slice.maxByteLength
|
|
283
295
|
}
|
|
284
296
|
|
|
@@ -289,19 +301,26 @@ export class PoolAllocator {
|
|
|
289
301
|
}
|
|
290
302
|
|
|
291
303
|
const maxByteLength = 1 << dstIdx
|
|
292
|
-
if (
|
|
304
|
+
if (
|
|
305
|
+
this.#poolsBucket.length > 32 ||
|
|
306
|
+
maxByteLength < byteLength ||
|
|
307
|
+
(maxByteLength & 0x7) !== 0
|
|
308
|
+
) {
|
|
293
309
|
throw new Error(`Invalid pool state`)
|
|
294
310
|
}
|
|
295
311
|
|
|
296
|
-
if (dstIdx < this.#
|
|
312
|
+
if (dstIdx < this.#poolsBucket.length && this.#poolsBucket[dstIdx]?.length) {
|
|
297
313
|
slice.buffer = this.#poolBuffer
|
|
298
|
-
slice.byteOffset = this.#
|
|
314
|
+
slice.byteOffset = this.#poolsBucket[dstIdx].pop()
|
|
299
315
|
slice.byteLength = byteLength
|
|
300
316
|
slice.maxByteLength = maxByteLength
|
|
301
317
|
|
|
318
|
+
this.#poolsBucketUsed[dstIdx] += 1
|
|
302
319
|
this.#poolSize += maxByteLength
|
|
320
|
+
this.#size += maxByteLength
|
|
321
|
+
this.#padding += maxByteLength - byteLength
|
|
303
322
|
} else if (
|
|
304
|
-
dstIdx < this.#
|
|
323
|
+
dstIdx < this.#poolsBucket.length &&
|
|
305
324
|
this.#poolOffset + maxByteLength <= this.#poolBuffer.byteLength
|
|
306
325
|
) {
|
|
307
326
|
slice.buffer = this.#poolBuffer
|
|
@@ -311,7 +330,10 @@ export class PoolAllocator {
|
|
|
311
330
|
|
|
312
331
|
this.#poolOffset += maxByteLength
|
|
313
332
|
this.#poolCount += 1
|
|
333
|
+
this.#poolsBucketUsed[dstIdx] += 1
|
|
314
334
|
this.#poolSize += maxByteLength
|
|
335
|
+
this.#size += maxByteLength
|
|
336
|
+
this.#padding += maxByteLength - byteLength
|
|
315
337
|
} else {
|
|
316
338
|
slice.buffer = Buffer.allocUnsafeSlow(byteLength)
|
|
317
339
|
slice.byteOffset = 0
|
|
@@ -319,9 +341,6 @@ export class PoolAllocator {
|
|
|
319
341
|
slice.maxByteLength = byteLength
|
|
320
342
|
}
|
|
321
343
|
|
|
322
|
-
this.#size += slice.maxByteLength
|
|
323
|
-
this.#padding += slice.maxByteLength - slice.byteLength
|
|
324
|
-
|
|
325
344
|
return slice
|
|
326
345
|
}
|
|
327
346
|
|
|
@@ -334,6 +353,11 @@ export class PoolAllocator {
|
|
|
334
353
|
poolUsed: this.#poolOffset,
|
|
335
354
|
poolSize: this.#poolSize,
|
|
336
355
|
poolCount: this.#poolCount,
|
|
356
|
+
buckets: this.#poolsBucket.map((pool, i) => ({
|
|
357
|
+
free: pool.length,
|
|
358
|
+
used: this.#poolsBucketUsed[i],
|
|
359
|
+
size: pool.length + this.#poolsBucketUsed[i],
|
|
360
|
+
})),
|
|
337
361
|
}
|
|
338
362
|
}
|
|
339
363
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nxtedition/slice",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.12",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -28,5 +28,5 @@
|
|
|
28
28
|
"rimraf": "^6.1.3",
|
|
29
29
|
"typescript": "^5.9.3"
|
|
30
30
|
},
|
|
31
|
-
"gitHead": "
|
|
31
|
+
"gitHead": "cfc529e921c93a1fb0292018e2026f2b326ec998"
|
|
32
32
|
}
|