-
-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: Reduce allocations on the hot path (#3111)
This PR takes a stab at a lot of the hot path allocations in excalibur improving performance significantly (numbers will be forthcoming with the new broadphase updates) - New `RentalPool` type for sparse object pooling - Perf improvements: Hot path allocations * Reduce State/Transform stack hot path allocations in graphics context * Reduce Transform allocations * Reduce AffineMatrix allocations
- Loading branch information
Showing
11 changed files
with
182 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,45 @@ | ||
import { Color } from '../../Color'; | ||
import { RentalPool } from '../../Util/RentalPool'; | ||
import { ExcaliburGraphicsContextState } from './ExcaliburGraphicsContext'; | ||
import { Material } from './material'; | ||
|
||
export class ContextState implements ExcaliburGraphicsContextState { | ||
opacity: number = 1; | ||
z: number = 0; | ||
tint: Color = Color.White; | ||
material: Material = null; | ||
} | ||
|
||
export class StateStack { | ||
public current: ExcaliburGraphicsContextState = this._getDefaultState(); | ||
private _pool = new RentalPool<ContextState>( | ||
() => new ContextState(), | ||
(s) => { | ||
s.opacity = 1; | ||
s.z = 0; | ||
s.tint = Color.White; | ||
s.material = null; | ||
return s; | ||
}, | ||
100 | ||
); | ||
public current: ExcaliburGraphicsContextState = this._pool.rent(true); | ||
private _states: ExcaliburGraphicsContextState[] = []; | ||
|
||
private _getDefaultState() { | ||
return { | ||
opacity: 1, | ||
z: 0, | ||
tint: Color.White, | ||
material: null as Material | ||
}; | ||
} | ||
|
||
private _cloneState() { | ||
return { | ||
opacity: this.current.opacity, | ||
z: this.current.z, | ||
tint: this.current.tint.clone(), | ||
material: this.current.material // TODO is this going to cause problems when cloning | ||
}; | ||
private _cloneState(dest: ContextState) { | ||
dest.opacity = this.current.opacity; | ||
dest.z = this.current.z; | ||
dest.tint = this.current.tint.clone(); // TODO remove color alloc | ||
dest.material = this.current.material; // TODO is this going to cause problems when cloning | ||
return dest; | ||
} | ||
|
||
public save(): void { | ||
this._states.push(this.current); | ||
this.current = this._cloneState(); | ||
this.current = this._cloneState(this._pool.rent()); | ||
} | ||
|
||
public restore(): void { | ||
this._pool.return(this.current); | ||
this.current = this._states.pop(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.