Skip to content

Commit

Permalink
fix: returnable pool
Browse files Browse the repository at this point in the history
  • Loading branch information
Sway007 committed Jul 11, 2024
1 parent 94d91f1 commit 4a8dcc7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
5 changes: 2 additions & 3 deletions packages/core/src/utils/ClearableObjectPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import { IPoolElement, ObjectPool } from "./ObjectPool";
export class ClearableObjectPool<T extends IPoolElement> extends ObjectPool<T> {
private _usedElementCount: number = 0;

constructor(type: new () => T) {
super(type);
this._elements = [];
constructor(type: new () => T, initializeCount: number = 0) {
super(type, initializeCount);
}

/**
Expand Down
6 changes: 5 additions & 1 deletion packages/core/src/utils/ObjectPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ export abstract class ObjectPool<T extends IPoolElement> {
protected _type: new () => T;
protected _elements: T[];

constructor(type: new () => T) {
constructor(type: new () => T, initializeCount: number = 0) {
this._type = type;
const elements = (this._elements = new Array<T>(initializeCount));
for (let i = 0; i < initializeCount; i++) {
elements[i] = new type();
}
}

garbageCollection(): void {
Expand Down
10 changes: 4 additions & 6 deletions packages/core/src/utils/ReturnableObjectPool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ export class ReturnableObjectPool<T extends IPoolElement> extends ObjectPool<T>
private _lastElementIndex: number;

constructor(type: new () => T, initializeCount: number = 1) {
super(type);
super(type, initializeCount);
this._lastElementIndex = initializeCount - 1;
const elements = (this._elements = new Array<T>(initializeCount));
for (let i = 0; i < initializeCount; ++i) {
elements[i] = new type();
}
}

/**
Expand All @@ -22,7 +18,9 @@ export class ReturnableObjectPool<T extends IPoolElement> extends ObjectPool<T>
if (this._lastElementIndex < 0) {
return new this._type();
}
return this._elements[this._lastElementIndex--];
const ret = this._elements[this._lastElementIndex--];
this._elements.length -= 1;
return ret;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/shader-lab/src/ShaderLab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export class ShaderLab implements IShaderLab {
/**
* @internal
*/
private static _shaderPositionPool = new ClearableObjectPool(ShaderPosition);
private static _shaderPositionPool = new ClearableObjectPool(ShaderPosition, 1000);
/**
* @internal
*/
private static _shaderRangePool = new ClearableObjectPool(ShaderRange);
private static _shaderRangePool = new ClearableObjectPool(ShaderRange, 500);

static createPosition(
index: number,
Expand Down

0 comments on commit 4a8dcc7

Please sign in to comment.