Skip to content

perf(scene): Create light list iterators on the stack at points of use#2646

Open
Mauller wants to merge 1 commit into
TheSuperHackers:mainfrom
Mauller:fix-renderlistiterator-allocation
Open

perf(scene): Create light list iterators on the stack at points of use#2646
Mauller wants to merge 1 commit into
TheSuperHackers:mainfrom
Mauller:fix-renderlistiterator-allocation

Conversation

@Mauller

@Mauller Mauller commented Apr 23, 2026

Copy link
Copy Markdown

This PR helps with a performance issue caused by a non block allocated, heap based, memory allocation that occurs multiple times every frame.

BaseHeightMapRenderObjClass::getStaticDiffuse() was calling RTS3DScene::createLightsIterator() and destroyLightsIterator(RefRenderObjListIterator * it) to retrieve the LightList from the current RTS3DScene.

Since the RefRenderObjListIterator is not properly poolified in the W3D or GameMemory managers, it was causing the game memory manager to constantly acquire, zero, then release system memory.

In this tweak i added an extra function in RTS3DScene to retrieve a pointer to the LightList instead.
This then allowed a stack created RefRenderObjListIterator to be created within BaseHeightMapRenderObjClass::getStaticDiffuse() instead.

This then resulted in a 20-40% performance improvement at the start of the game depending on map.

EDIT:

I have updated this to also remove the create and destroy iterator functions. I then upated all current call sites to use a stack created iterator where necessary or removed using the iterator if it was unused.

@Mauller Mauller self-assigned this Apr 23, 2026
@Mauller Mauller added Performance Is a performance concern Memory Is memory related Gen Relates to Generals ZH Relates to Zero Hour labels Apr 23, 2026
@greptile-apps

greptile-apps Bot commented Apr 23, 2026

Copy link
Copy Markdown

Greptile Summary

This PR removes heap allocation for scene-light iterators in terrain rendering. The main changes are:

  • Adds access to the scene light list.
  • Creates light iterators on the stack at use sites.
  • Removes the obsolete iterator factory and destructor APIs.

Confidence Score: 5/5

This looks safe to merge.

  • No blocking issues found in the changed code.

Important Files Changed

Filename Overview
Core/GameEngineDevice/Source/W3DDevice/GameClient/BaseHeightMap.cpp Uses a stack-local iterator while calculating static terrain lighting.
Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DTerrainVisual.cpp Removes the temporary scene-light iterator during terrain initialization.
Core/GameEngineDevice/Source/W3DDevice/GameClient/W3DView.cpp Uses a stack-local iterator during terrain center updates.
Generals/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DScene.h Exposes the embedded scene light list and removes obsolete iterator APIs.
Generals/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DScene.cpp Removes obsolete heap-based iterator helper implementations.
GeneralsMD/Code/GameEngineDevice/Include/W3DDevice/GameClient/W3DScene.h Keeps the Zero Hour scene interface aligned with the Generals variant.
GeneralsMD/Code/GameEngineDevice/Source/W3DDevice/GameClient/W3DScene.cpp Removes the mirrored obsolete iterator helper implementations.

Reviews (3): Last reviewed commit: "perf(heighmap): Create light list iterat..." | Re-trigger Greptile

@Mauller Mauller changed the title perf(heighmap): Create light list iterator on the stack instead in BaseHeightMapRenderObjClass::getStaticDiffuse() perf(heighmap): Create light list iterator on the stack in BaseHeightMapRenderObjClass::getStaticDiffuse() Apr 23, 2026
@Mauller

Mauller commented Apr 23, 2026

Copy link
Copy Markdown
Author

Flame graph showing before and after comparisons when loading Han Dynasty V3

Before:
image

After:
image

@Mauller

Mauller commented Apr 23, 2026

Copy link
Copy Markdown
Author

Some ingame with overlay fps for comparisson.

Before:
image

After:
image

@xezon xezon left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On my wip height map change I removed the entire thing.

If I recall correctly it is entirely unused.

Image

@Mauller

Mauller commented Apr 24, 2026

Copy link
Copy Markdown
Author

On my wip height map change I removed the entire thing.

If I recall correctly it is entirely unused.

Are you certain?

Need to check if its just unused in a lot of maps but used when point and directional lights are on a map.

@xezon

xezon commented Apr 24, 2026

Copy link
Copy Markdown

Not 100% certain, but at the time I reworked height map I found no evidence that it is used for anything but is passed into many functions.

@Mauller

Mauller commented Apr 24, 2026

Copy link
Copy Markdown
Author

Not 100% certain, but at the time I reworked height map I found no evidence that it is used for anything but is passed into many functions.

I think i remember some people wanting to add point and directional lights into the game again, so might be good to leave in the code for now but add this optimisation to get rid of the performance issue with it.

Comment thread Core/GameEngineDevice/Source/W3DDevice/GameClient/BaseHeightMap.cpp
@Mauller Mauller force-pushed the fix-renderlistiterator-allocation branch from f5ed1e1 to de432ef Compare April 27, 2026 17:51
@Mauller

Mauller commented Apr 27, 2026

Copy link
Copy Markdown
Author

Updated to remove the create and destroy iterator functions and all affected call sites.

@Skyaero42 Skyaero42 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like it

@Mauller Mauller changed the title perf(heighmap): Create light list iterator on the stack in BaseHeightMapRenderObjClass::getStaticDiffuse() perf(scene): Create ligh list iterator on the stack at points of use Apr 28, 2026
@Mauller Mauller changed the title perf(scene): Create ligh list iterator on the stack at points of use perf(scene): Create light list iterator on the stack at points of use Apr 28, 2026
@Mauller Mauller changed the title perf(scene): Create light list iterator on the stack at points of use perf(scene): Create light list iterators on the stack at points of use May 2, 2026
@Caball009

Caball009 commented Jul 9, 2026

Copy link
Copy Markdown

@Mauller Could you rebase this branch?

@xezon I assume this hasn't been merged yet because it'd be superseded by your own changes eventually. Is that right? AFAICT it could have been merged two months ago, though.

…e heap in BaseHeightMapRenderObjClass::getStaticDiffuse()
@Mauller Mauller force-pushed the fix-renderlistiterator-allocation branch from de432ef to 5f6d113 Compare July 13, 2026 21:08
@Mauller

Mauller commented Jul 13, 2026

Copy link
Copy Markdown
Author

Rebased and tweaked

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Gen Relates to Generals Memory Is memory related Performance Is a performance concern ZH Relates to Zero Hour

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants