Skip to content

Commit 3f073cf

Browse files
committed
Rename ArrayBufferPool to ResourcePool for other uses.
also make sure that you can't freely call ResourcePool methods, that would break the assumption made by DirectContextModel.
1 parent 02d09a3 commit 3f073cf

File tree

5 files changed

+38
-27
lines changed

5 files changed

+38
-27
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ The actual memory usage can be as low as a half of the specified due to the inte
123123

124124
* Level 2 runs a thorough search with about 300 sets of parameters and takes about a minute or two. This is best useful for the release build and you would like to save best parameters for later uses.
125125

126-
While not strictly required, `Packer.optimize` in the API strongly recommends the use of `arrayBufferPool` in the options object. Otherwise the optimization can run slower especially with larger memory. The pool can be created via `new ArrayBufferPool()`.
126+
While not strictly required, `Packer.optimize` in the API strongly recommends the use of `resourcePool` in the options object. Otherwise the optimization can run slower especially with larger memory. The pool can be created via `new ResourcePool()`.
127127

128128
### Advanced Configuration
129129

cli.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import * as fs from 'fs';
44
import * as process from 'process';
5-
import { ArrayBufferPool, Packer, defaultSparseSelectors } from './index.mjs';
5+
import { ResourcePool, Packer, defaultSparseSelectors } from './index.mjs';
66

77
let VERSION = 'unknown';
88
try {
@@ -116,7 +116,7 @@ async function parseArgs(args) {
116116
const inputs = [];
117117
let currentInput = {};
118118
const options = {
119-
arrayBufferPool: new ArrayBufferPool(),
119+
resourcePool: new ResourcePool(),
120120
};
121121
let command;
122122
let optimize;

index.d.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
export class ArrayBufferPool {
1+
export class ResourcePool {
22
constructor();
3+
/** @deprecated Should not be used externally! */
34
allocate(parent: object, size: number): ArrayBuffer;
5+
/** @deprecated Should not be used externally! */
46
release(buf: ArrayBuffer): void;
57
}
68

9+
/** @deprecated Use {@link ResourcePool} instead */
10+
export const ArrayBufferPool = ResourcePool;
11+
712
export type ScaledFreq = number;
813

914
export type Bit = 0 | 1;
@@ -42,7 +47,9 @@ export interface DirectContextModelOptions {
4247
precision: number;
4348
modelMaxCount: number;
4449
modelRecipBaseCount: number;
45-
arrayBufferPool?: ArrayBufferPool;
50+
resourcePool?: ResourcePool;
51+
/** @deprecated Use {@link DirectContextModelOptions.resourcePool} instead */
52+
arrayBufferPool?: ResourcePool;
4653
}
4754

4855
export class DirectContextModel implements Model {
@@ -90,7 +97,7 @@ export class DefaultModel implements LogisticMixModel {
9097
flushByte(currentByte: number, inBits: number): void;
9198
release(): void;
9299

93-
readonly quitesSeen: Set<number>;
100+
readonly quotesSeen: Set<number>;
94101
}
95102

96103
export interface CompressOptions extends AnsOptions {
@@ -164,7 +171,9 @@ export interface PackerOptions {
164171
precision?: number;
165172
modelMaxCount?: number;
166173
modelRecipBaseCount?: number;
167-
arrayBufferPool?: ArrayBufferPool;
174+
resourcePool?: ResourcePool;
175+
/** @deprecated Use {@link PackerOptions.resourcePool} instead */
176+
arrayBufferPool?: ResourcePool;
168177
recipLearningRate?: number;
169178
numAbbreviations?: number;
170179
allowFreeVars?: boolean;

index.mjs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ const getPerformanceObject = async () => {
7070

7171
//------------------------------------------------------------------------------
7272

73-
export class ArrayBufferPool {
73+
export class ResourcePool {
7474
constructor() {
75-
// pool.get(size) is an array of ArrayBuffer of given size
76-
this.pool = new Map();
75+
// arrayBuffers.get(size) is an array of ArrayBuffer of given size
76+
this.arrayBuffers = new Map();
7777
}
7878

7979
allocate(parent, size) {
80-
const available = this.pool.get(size);
80+
const available = this.arrayBuffers.get(size);
8181
let buf;
8282
if (available) buf = available.pop();
8383
if (!buf) buf = new ArrayBuffer(size);
@@ -86,15 +86,17 @@ export class ArrayBufferPool {
8686

8787
// FinalizationRegistry is also possible, but GC couldn't keep up with the memory usage
8888
release(buf) {
89-
let available = this.pool.get(buf.byteLength);
89+
let available = this.arrayBuffers.get(buf.byteLength);
9090
if (!available) {
9191
available = [];
92-
this.pool.set(buf.byteLength, available);
92+
this.arrayBuffers.set(buf.byteLength, available);
9393
}
9494
available.push(buf);
9595
}
9696
}
9797

98+
export const ArrayBufferPool = ResourcePool;
99+
98100
const newUintArray = (pool, parent, nbits, length) => {
99101
if (nbits <= 8) return new Uint8Array(pool ? pool.allocate(parent, length) : length);
100102
if (nbits <= 16) return new Uint16Array(pool ? pool.allocate(parent, length * 2) : length);
@@ -230,18 +232,18 @@ export class AnsDecoder {
230232
//------------------------------------------------------------------------------
231233

232234
export class DirectContextModel {
233-
constructor({ inBits, contextBits, precision, modelMaxCount, modelRecipBaseCount, arrayBufferPool }) {
235+
constructor({ inBits, contextBits, precision, modelMaxCount, modelRecipBaseCount, arrayBufferPool, resourcePool }) {
234236
this.inBits = inBits;
235237
this.contextBits = contextBits;
236238
this.precision = precision;
237239
this.modelMaxCount = modelMaxCount;
238240
this.modelBaseCount = 1 / modelRecipBaseCount;
239241

240-
this.arrayBufferPool = arrayBufferPool;
241-
this.predictions = newUintArray(arrayBufferPool, this, precision, 1 << contextBits);
242-
this.counts = newUintArray(arrayBufferPool, this, ceilLog2(modelMaxCount + 1), 1 << contextBits);
242+
this.resourcePool = resourcePool || arrayBufferPool;
243+
this.predictions = newUintArray(this.resourcePool, this, precision, 1 << contextBits);
244+
this.counts = newUintArray(this.resourcePool, this, ceilLog2(modelMaxCount + 1), 1 << contextBits);
243245

244-
if (arrayBufferPool) {
246+
if (this.resourcePool) {
245247
// we need to initialize the array since it may have been already used,
246248
// but UintXXArray.fill is comparatively slow, less than 5 GB/s even in fastest browsers.
247249
// we instead use more memory to confirm that each bit of context has been initialized.
@@ -251,7 +253,7 @@ export class DirectContextModel {
251253
// we choose a new mark to mark initialized elements *in this instance*.
252254
// if the mark reaches 255 we reset the entire array and start over.
253255
// this scheme effectively reduces the number of fill calls by a factor of 510.
254-
this.confirmations = newUintArray(arrayBufferPool, this, 8, (1 << contextBits) + 1);
256+
this.confirmations = newUintArray(this.resourcePool, this, 8, (1 << contextBits) + 1);
255257
this.mark = this.confirmations[1 << contextBits] + 1;
256258
if (this.mark === 256) {
257259
this.mark = 1;
@@ -326,10 +328,10 @@ export class DirectContextModel {
326328
}
327329

328330
release() {
329-
if (this.arrayBufferPool) {
330-
if (this.predictions) this.arrayBufferPool.release(this.predictions.buffer);
331-
if (this.counts) this.arrayBufferPool.release(this.counts.buffer);
332-
if (this.confirmations) this.arrayBufferPool.release(this.confirmations.buffer);
331+
if (this.resourcePool) {
332+
if (this.predictions) this.resourcePool.release(this.predictions.buffer);
333+
if (this.counts) this.resourcePool.release(this.counts.buffer);
334+
if (this.confirmations) this.resourcePool.release(this.confirmations.buffer);
333335
this.predictions = null;
334336
this.counts = null;
335337
this.confirmations = null;
@@ -630,7 +632,7 @@ export class Packer {
630632
modelRecipBaseCount: options.modelRecipBaseCount || 20,
631633
recipLearningRate: options.recipLearningRate || Math.max(1, 500),
632634
contextBits: options.contextBits,
633-
arrayBufferPool: options.arrayBufferPool,
635+
resourcePool: options.resourcePool || options.arrayBufferPool,
634636
numAbbreviations: typeof options.numAbbreviations === 'number' ? options.numAbbreviations : 64,
635637
allowFreeVars: options.allowFreeVars,
636638
};

test.mjs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import test from 'ava';
22
import * as crypto from 'crypto';
33
import {
4-
ArrayBufferPool, AnsEncoder, AnsDecoder, DirectContextModel, DefaultModel, Packer,
54
compressWithModel, decompressWithModel
5+
ResourcePool, AnsEncoder, AnsDecoder, DirectContextModel, DefaultModel, Packer,
66
} from './index.mjs';
77

88
//------------------------------------------------------------------------------
@@ -263,14 +263,14 @@ test('inputEndsWithByte', t => {
263263
//------------------------------------------------------------------------------
264264

265265
test('DirectContextModel.confirmations', t => {
266-
const arrayBufferPool = new ArrayBufferPool();
266+
const resourcePool = new ResourcePool();
267267
const options = {
268268
inBits: 8,
269269
outBits: 8,
270270
precision: 16,
271271
contextBits: 5, // 32 elements
272272
modelMaxCount: 63,
273-
arrayBufferPool,
273+
resourcePool,
274274
};
275275

276276
// the size of 1 will set ~8 elements and test for partial fills.

0 commit comments

Comments
 (0)