Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid quadratic behavior when validating sprites #3379

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/.internal/core/Registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,22 @@ export class Registry {
protected _placeholders: { [index: string]: string } = {};

/**
* A list of invalid(ated) [[Sprite]] objects that need to be re-validated
* Lists of invalid(ated) [[Sprite]] objects that need to be re-validated
* during next cycle.
*
* @ignore Exclude from docs
*/
public invalidSprites: { [index: string]: Array<Sprite> } = {};

/**
* Objects with the keys given by the uids of no longer
* invalid [[Sprite]] objects that should be removed from the
* invalid Sprites.
*
* @ignore Exclude from docs
*/
public validatedSpritesBatchPurge: { [index: string]: any } = {};

/**
* Components are added to this list when their data provider changes to
* a new one or data is added/removed from their data provider.
Expand Down Expand Up @@ -194,6 +203,7 @@ export class Registry {
this.uid = this.getUniqueId();

this.invalidSprites.noBase = [];
this.validatedSpritesBatchPurge.noBase = {};
this.invalidDatas.noBase = [];
this.invalidLayouts.noBase = [];
this.invalidPositions.noBase = [];
Expand Down Expand Up @@ -375,10 +385,16 @@ export class Registry {
*/
public removeFromInvalidSprites(sprite: Sprite) {
if (sprite.baseId) {
$array.remove(this.invalidSprites[sprite.baseId], sprite);
if (!this.validatedSpritesBatchPurge[spriteBaseId][sprite.uid]) {
// not doing batch purge, so remove sprite from invalid sprites right here
$array.remove(this.invalidSprites[sprite.baseId], sprite);
}
}

$array.remove(this.invalidSprites["noBase"], sprite);
if (!this.validatedSpritesBatchPurge[spriteBaseId][sprite.uid]) {
// not doing batch purge, so remove sprite from invalid sprites right here
$array.remove(this.invalidSprites["noBase"], sprite);
}
}


Expand Down
9 changes: 6 additions & 3 deletions src/.internal/core/System.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ export class System {

$object.each(registry.invalidSprites, (key, invalidSprites) => {
let count = 0;

while (invalidSprites.length > 0) {
const toPurge = registry.validatedSpritesBatchPurge[key];
for (const index = invalidSprites.length - 1; index >= 0; index--) {
this.validateLayouts(key);
this.validatePositions(key);

Expand All @@ -287,9 +287,12 @@ export class System {
count = 0;
}

let sprite: Sprite = invalidSprites[invalidSprites.length - 1];
let sprite: Sprite = invalidSprites[index];

// we need to check this, as validateLayout might validate sprite
if (sprite) {
toPurge[sprite.uid] = true;
}
if (sprite && !sprite.isDisposed()) {
if (!sprite._systemCheckIfValidate()) {
// void
Expand Down
1 change: 1 addition & 0 deletions src/.internal/core/utils/Instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ function createChild<T extends Sprite>(htmlElement: $type.Optional<HTMLElement |
let uid = sprite.uid;

registry.invalidSprites[uid] = [];
registry.validatedSpritesBatchPurge[uid] = {};
registry.invalidDatas[uid] = [];
registry.invalidPositions[uid] = [];
registry.invalidLayouts[uid] = [];
Expand Down