Skip to content
Merged
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
59 changes: 9 additions & 50 deletions src/core/TextureMemoryManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ export class TextureMemoryManager {
private debugLogging: boolean;
private lastCleanupTime = 0;
private baselineMemoryAllocation: number;
private needsDefrag = false;

public criticalCleanupRequested = false;
public doNotExceedCriticalThreshold: boolean;
Expand Down Expand Up @@ -191,11 +190,10 @@ export class TextureMemoryManager {
this.memUsed -= texture.memUsed;

if (byteSize === 0) {
// PERFORMANCE: Mark for deletion instead of splice (zero overhead)
// PERFORMANCE: Mark for deletion, slot will be reused later
const index = this.loadedTextures.indexOf(texture);
if (index !== -1) {
this.loadedTextures[index] = null;
this.needsDefrag = true;
}
texture.memUsed = 0;
return;
Expand All @@ -204,7 +202,13 @@ export class TextureMemoryManager {
texture.memUsed = byteSize;
this.memUsed += byteSize;
if (this.loadedTextures.indexOf(texture) === -1) {
this.loadedTextures.push(texture);
// PERFORMANCE: Reuse empty slots before appending
const emptyIndex = this.loadedTextures.indexOf(null);
if (emptyIndex !== -1) {
this.loadedTextures[emptyIndex] = texture;
} else {
this.loadedTextures.push(texture);
}
}
}

Expand All @@ -225,19 +229,6 @@ export class TextureMemoryManager {
return this.memUsed > this.criticalThreshold;
}

/**
* Check if defragmentation is needed
*
* @remarks
* Returns true if the loadedTextures array has null entries that need
* to be compacted. Called by platform during idle periods.
*
* @returns true if defragmentation should be performed
*/
checkDefrag() {
return this.needsDefrag;
}

/**
* Destroy a texture and null out its array position
*
Expand All @@ -250,11 +241,10 @@ export class TextureMemoryManager {
);
}

// PERFORMANCE: Null out array position instead of splice (zero overhead)
// PERFORMANCE: Null out array position, slot will be reused later
const index = this.loadedTextures.indexOf(texture);
if (index !== -1) {
this.loadedTextures[index] = null;
this.needsDefrag = true;
}

// Destroy texture and update memory counters
Expand Down Expand Up @@ -334,37 +324,6 @@ export class TextureMemoryManager {
}
}

/**
* Defragment the loadedTextures array by removing null entries
*
* @remarks
* This should be called during idle periods to compact the array
* after null-marking deletions. Zero overhead during critical cleanup.
*/
defragment() {
if (!this.needsDefrag) {
return;
}

// PERFORMANCE: Single-pass compaction
let writeIndex = 0;
for (
let readIndex = 0;
readIndex < this.loadedTextures.length;
readIndex++
) {
const texture = this.loadedTextures[readIndex];
if (texture !== null && texture !== undefined) {
this.loadedTextures[writeIndex] = texture;
writeIndex++;
}
}

// Trim array to new size
this.loadedTextures.length = writeIndex;
this.needsDefrag = false;
}

/**
* Get the current texture memory usage information
*
Expand Down
4 changes: 0 additions & 4 deletions src/core/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,6 @@ export const startLoop = (stage: Stage) => {
stage.txMemManager.cleanup();
}

if (stage.txMemManager.checkDefrag() === true) {
stage.txMemManager.defragment();
}

stage.flushFrameEvents();
return;
}
Expand Down