Skip to content

Commit 8a23805

Browse files
committed
refactor: Reduce branching, optimise hot paths, remove asserts where no longer needed
1 parent 8d5a69c commit 8a23805

File tree

8 files changed

+352
-402
lines changed

8 files changed

+352
-402
lines changed

src/core/CoreNode.ts

Lines changed: 209 additions & 205 deletions
Large diffs are not rendered by default.

src/core/CoreTextNode.ts

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,19 @@ import type {
2525
TrFailedEventHandler,
2626
TrLoadedEventHandler,
2727
} from './text-rendering/renderers/TextRenderer.js';
28-
import { CoreNode, UpdateType, type CoreNodeProps } from './CoreNode.js';
28+
import {
29+
CoreNode,
30+
CoreNodeRenderState,
31+
UpdateType,
32+
type CoreNodeProps,
33+
} from './CoreNode.js';
2934
import type { Stage } from './Stage.js';
3035
import type { CoreRenderer } from './renderers/CoreRenderer.js';
3136
import type {
3237
NodeTextFailedPayload,
3338
NodeTextLoadedPayload,
3439
} from '../common/CommonTypes.js';
3540
import type { RectWithValid } from './lib/utils.js';
36-
import { assertTruthy } from '../utils.js';
3741

3842
export interface CoreTextNodeProps extends CoreNodeProps, TrProps {
3943
/**
@@ -211,7 +215,7 @@ export class CoreTextNode extends CoreNode implements CoreTextNodeProps {
211215
this._textRendererOverride,
212216
);
213217

214-
if (!textRenderer) {
218+
if (textRenderer === null) {
215219
console.warn(
216220
'Text Renderer not found for font',
217221
this.trState.props.fontFamily,
@@ -370,23 +374,24 @@ export class CoreTextNode extends CoreNode implements CoreTextNodeProps {
370374
override update(delta: number, parentClippingRect: RectWithValid) {
371375
super.update(delta, parentClippingRect);
372376

373-
assertTruthy(this.globalTransform);
374-
375377
// globalTransform is updated in super.update(delta)
376-
this.textRenderer.set.x(this.trState, this.globalTransform.tx);
377-
this.textRenderer.set.y(this.trState, this.globalTransform.ty);
378+
this.textRenderer.set.x(this.trState, this.globalTransform!.tx);
379+
this.textRenderer.set.y(this.trState, this.globalTransform!.ty);
378380
}
379381

380-
override checkBasicRenderability() {
381-
if (this.worldAlpha === 0 || this.isOutOfBounds() === true) {
382-
return false;
382+
override updateIsRenderable() {
383+
// If the node is out of bounds or has an alpha of 0, it is not renderable
384+
if (
385+
this.worldAlpha === 0 ||
386+
this.renderState <= CoreNodeRenderState.OutOfBounds
387+
) {
388+
this.setRenderable(false);
389+
return;
383390
}
384391

385-
if (this.trState && this.trState.props.text !== '') {
386-
return true;
392+
if (this.trState !== undefined && this.trState.props.text !== '') {
393+
this.setRenderable(true);
387394
}
388-
389-
return false;
390395
}
391396

392397
override setRenderable(isRenderable: boolean) {
@@ -395,11 +400,9 @@ export class CoreTextNode extends CoreNode implements CoreTextNodeProps {
395400
}
396401

397402
override renderQuads(renderer: CoreRenderer) {
398-
assertTruthy(this.globalTransform);
399-
400403
// If the text renderer does not support rendering quads, fallback to the
401404
// default renderQuads method
402-
if (!this.textRenderer.renderQuads) {
405+
if (this.textRenderer.renderQuads === undefined) {
403406
super.renderQuads(renderer);
404407
return;
405408
}
@@ -408,8 +411,8 @@ export class CoreTextNode extends CoreNode implements CoreTextNodeProps {
408411

409412
// Prevent quad rendering if parent has a render texture
410413
// and this node is not the render texture
411-
if (this.parentHasRenderTexture) {
412-
if (!renderer.renderToTextureActive) {
414+
if (this.parentHasRenderTexture === true) {
415+
if (renderer.renderToTextureActive === false) {
413416
return;
414417
}
415418
// Prevent quad rendering if parent render texture is not the active render texture

src/core/CoreTextureManager.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,13 @@ export class CoreTextureManager extends EventEmitter {
481481
return;
482482
}
483483

484-
const startTime = this.platform.getTimeStamp();
484+
const platform = this.platform;
485+
const startTime = platform.getTimeStamp();
485486

486487
// Process priority queue
487488
while (
488489
this.priorityQueue.length > 0 &&
489-
this.platform.getTimeStamp() - startTime < maxProcessingTime
490+
platform.getTimeStamp() - startTime < maxProcessingTime
490491
) {
491492
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
492493
const texture = this.priorityQueue.pop()!;
@@ -498,7 +499,7 @@ export class CoreTextureManager extends EventEmitter {
498499
// Process uploads
499500
while (
500501
this.uploadTextureQueue.length > 0 &&
501-
this.platform.getTimeStamp() - startTime < maxProcessingTime
502+
platform.getTimeStamp() - startTime < maxProcessingTime
502503
) {
503504
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
504505
this.uploadTexture(this.uploadTextureQueue.pop()!);
@@ -507,7 +508,7 @@ export class CoreTextureManager extends EventEmitter {
507508
// Process downloads
508509
while (
509510
this.downloadTextureSourceQueue.length > 0 &&
510-
this.platform.getTimeStamp() - startTime < maxProcessingTime
511+
platform.getTimeStamp() - startTime < maxProcessingTime
511512
) {
512513
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
513514
const texture = this.downloadTextureSourceQueue.shift()!;

src/core/Stage.ts

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,12 @@ export class Stage {
378378
* Start a new frame draw
379379
*/
380380
drawFrame() {
381-
const { renderer, renderRequested } = this;
382-
assertTruthy(renderer);
381+
const { renderer, renderRequested, root } = this;
382+
const txMemManager = this.txMemManager;
383383

384384
// Update tree if needed
385-
if (this.root.updateType !== 0) {
386-
this.root.update(this.deltaTime, this.root.clippingRect);
385+
if (root.updateType !== 0) {
386+
root.update(this.deltaTime, root.clippingRect);
387387
}
388388

389389
// Process some textures
@@ -393,12 +393,12 @@ export class Stage {
393393
renderer.reset();
394394

395395
// Check if we need to cleanup textures
396-
if (this.txMemManager.criticalCleanupRequested === true) {
397-
this.txMemManager.cleanup(false);
396+
if (txMemManager.criticalCleanupRequested === true) {
397+
txMemManager.cleanup(false);
398398

399-
if (this.txMemManager.criticalCleanupRequested === true) {
399+
if (txMemManager.criticalCleanupRequested === true) {
400400
// If we still need to cleanup, request another but aggressive cleanup
401-
this.txMemManager.cleanup(true);
401+
txMemManager.cleanup(true);
402402
}
403403
}
404404

@@ -412,13 +412,13 @@ export class Stage {
412412
this.addQuads(this.root);
413413

414414
// Perform render pass
415-
renderer?.render();
415+
renderer.render();
416416

417417
this.calculateFps();
418418
this.calculateQuads();
419419

420420
// Reset renderRequested flag if it was set
421-
if (renderRequested) {
421+
if (renderRequested === true) {
422422
this.renderRequested = false;
423423
}
424424
}
@@ -668,18 +668,23 @@ export class Stage {
668668
*/
669669
protected resolveNodeDefaults(props: Partial<CoreNodeProps>): CoreNodeProps {
670670
const color = props.color ?? 0xffffffff;
671-
const colorTl = props.colorTl ?? props.colorTop ?? props.colorLeft ?? color;
672-
const colorTr =
673-
props.colorTr ?? props.colorTop ?? props.colorRight ?? color;
674-
const colorBl =
675-
props.colorBl ?? props.colorBottom ?? props.colorLeft ?? color;
676-
const colorBr =
677-
props.colorBr ?? props.colorBottom ?? props.colorRight ?? color;
678-
679-
let data = {};
680-
if (this.options.inspector === true) {
681-
data = santizeCustomDataMap(props.data ?? {});
682-
}
671+
const colorTop = props.colorTop ?? color;
672+
const colorBottom = props.colorBottom ?? color;
673+
const colorLeft = props.colorLeft ?? color;
674+
const colorRight = props.colorRight ?? color;
675+
676+
const colorTl = props.colorTl ?? colorTop ?? colorLeft ?? color;
677+
const colorTr = props.colorTr ?? colorTop ?? colorRight ?? color;
678+
const colorBl = props.colorBl ?? colorBottom ?? colorLeft ?? color;
679+
const colorBr = props.colorBr ?? colorBottom ?? colorRight ?? color;
680+
681+
const scale = props.scale ?? null;
682+
const mount = props.mount ?? 0;
683+
const pivot = props.pivot ?? 0.5;
684+
685+
const data = this.options.inspector
686+
? santizeCustomDataMap(props.data ?? {})
687+
: {};
683688

684689
return {
685690
x: props.x ?? 0,
@@ -691,39 +696,37 @@ export class Stage {
691696
boundsMargin: props.boundsMargin ?? null,
692697
clipping: props.clipping ?? false,
693698
color,
694-
colorTop: props.colorTop ?? color,
695-
colorBottom: props.colorBottom ?? color,
696-
colorLeft: props.colorLeft ?? color,
697-
colorRight: props.colorRight ?? color,
698-
colorBl,
699-
colorBr,
699+
colorTop,
700+
colorBottom,
701+
colorLeft,
702+
colorRight,
700703
colorTl,
701704
colorTr,
705+
colorBl,
706+
colorBr,
702707
zIndex: props.zIndex ?? 0,
703708
zIndexLocked: props.zIndexLocked ?? 0,
704709
parent: props.parent ?? null,
705710
texture: props.texture ?? null,
706711
textureOptions: props.textureOptions ?? {},
707712
shader: props.shader ?? this.defShaderNode,
708-
// Since setting the `src` will trigger a texture load, we need to set it after
709-
// we set the texture. Otherwise, problems happen.
710713
src: props.src ?? null,
711714
srcHeight: props.srcHeight,
712715
srcWidth: props.srcWidth,
713716
srcX: props.srcX,
714717
srcY: props.srcY,
715-
scale: props.scale ?? null,
716-
scaleX: props.scaleX ?? props.scale ?? 1,
717-
scaleY: props.scaleY ?? props.scale ?? 1,
718-
mount: props.mount ?? 0,
719-
mountX: props.mountX ?? props.mount ?? 0,
720-
mountY: props.mountY ?? props.mount ?? 0,
721-
pivot: props.pivot ?? 0.5,
722-
pivotX: props.pivotX ?? props.pivot ?? 0.5,
723-
pivotY: props.pivotY ?? props.pivot ?? 0.5,
718+
scale,
719+
scaleX: props.scaleX ?? scale ?? 1,
720+
scaleY: props.scaleY ?? scale ?? 1,
721+
mount,
722+
mountX: props.mountX ?? mount,
723+
mountY: props.mountY ?? mount,
724+
pivot,
725+
pivotX: props.pivotX ?? pivot,
726+
pivotY: props.pivotY ?? pivot,
724727
rotation: props.rotation ?? 0,
725728
rtt: props.rtt ?? false,
726-
data: data,
729+
data,
727730
imageType: props.imageType,
728731
strictBounds: props.strictBounds ?? this.strictBounds,
729732
};

src/core/TextureMemoryManager.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -323,12 +323,15 @@ export class TextureMemoryManager {
323323

324324
cleanup(aggressive: boolean = false) {
325325
const critical = this.criticalCleanupRequested;
326+
const criticalThreshold = this.criticalThreshold;
327+
const memUsed = this.memUsed;
328+
const stage = this.stage;
326329
this.lastCleanupTime = this.frameTime;
327330

328331
if (critical === true) {
329-
this.stage.queueFrameEvent('criticalCleanup', {
332+
stage.queueFrameEvent('criticalCleanup', {
330333
memUsed: this.memUsed,
331-
criticalThreshold: this.criticalThreshold,
334+
criticalThreshold: criticalThreshold,
332335
});
333336
}
334337

@@ -342,19 +345,19 @@ export class TextureMemoryManager {
342345
this.cleanupQuick(critical);
343346

344347
// if we're still above the target threshold, do a deep cleanup
345-
if (aggressive === true && this.memUsed >= this.criticalThreshold) {
348+
if (aggressive === true && memUsed >= criticalThreshold) {
346349
this.cleanupDeep(critical);
347350
}
348351

349-
if (this.memUsed >= this.criticalThreshold) {
350-
this.stage.queueFrameEvent('criticalCleanupFailed', {
351-
memUsed: this.memUsed,
352-
criticalThreshold: this.criticalThreshold,
352+
if (memUsed >= criticalThreshold) {
353+
stage.queueFrameEvent('criticalCleanupFailed', {
354+
memUsed: memUsed,
355+
criticalThreshold: criticalThreshold,
353356
});
354357

355358
if (this.debugLogging === true || isProductionEnvironment === false) {
356359
console.warn(
357-
`[TextureMemoryManager] Memory usage above critical threshold after cleanup: ${this.memUsed}`,
360+
`[TextureMemoryManager] Memory usage above critical threshold after cleanup: ${memUsed}`,
358361
);
359362
}
360363
} else {

src/core/renderers/webgl/WebGlCtxTexture.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818
*/
1919

2020
import type { Dimensions } from '../../../common/CommonTypes.js';
21-
import { assertTruthy } from '../../../utils.js';
2221
import type { TextureMemoryManager } from '../../TextureMemoryManager.js';
2322
import type { WebGlContextWrapper } from '../../lib/WebGlContextWrapper.js';
2423
import type { Texture } from '../../textures/Texture.js';
25-
import { isPowerOfTwo } from '../../utils.js';
2624
import { CoreContextTexture } from '../CoreContextTexture.js';
2725
import { isHTMLImageElement } from './internal/RendererUtils.js';
2826

@@ -57,7 +55,6 @@ export class WebGlCtxTexture extends CoreContextTexture {
5755
this.load();
5856
return null;
5957
}
60-
assertTruthy(this._nativeCtxTexture);
6158
return this._nativeCtxTexture;
6259
}
6360

0 commit comments

Comments
 (0)