Skip to content

perf(script): Reduce cost of ScriptEngine::findScript() by around 85% - #3096

Open
Mauller wants to merge 1 commit into
TheSuperHackers:mainfrom
Mauller:Mauller/perf-script-lookup
Open

perf(script): Reduce cost of ScriptEngine::findScript() by around 85%#3096
Mauller wants to merge 1 commit into
TheSuperHackers:mainfrom
Mauller:Mauller/perf-script-lookup

Conversation

@Mauller

@Mauller Mauller commented Aug 10, 2026

Copy link
Copy Markdown

This PR dramatically improves script search performance by removing the allocation overhead when comparing script names.

I also tested the same performance degradation against a version of asciistring that does not use the global locks.
But like with the similar waypoint names fix #2761, returning constant references to the asciistrings was significantly more performant.

All of these tests were performed in a retail release, letting the same AoD map level run to its conclusion.
So this is not a debug only issue.


Flame graphs and function usage comparisons.

Before:
image
image

After:
image

image

video comparison.

This video shows the most recent GO build against a tweaked GO build with the fixes from this PR.

https://youtu.be/aDy8w4sHPKU


TODO

  • Replicate in generals

@Mauller Mauller self-assigned this Aug 10, 2026
@Mauller Mauller added Performance Is a performance concern Gen Relates to Generals ZH Relates to Zero Hour Memory Is memory related Script Is related to Script Engine, SCB labels Aug 10, 2026
@Mauller
Mauller force-pushed the Mauller/perf-script-lookup branch from 66da268 to 73f3c67 Compare August 10, 2026 18:46
@Mauller Mauller changed the title perf(script): improve script search performance by 7-8x (#) perf(script): improve script search performance by 7-8x Aug 10, 2026

@Caball009 Caball009 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.

const got a bit out of hand; use these:

const AsciiString& getName() const { return m_groupName;}
const AsciiString& getName() const { return m_scriptName;}
const AsciiString& getConditionTeamName() const {return m_conditionTeamName;}


Did removing the global lock from the AsciiString member functions provide less performance improvement than these changes? That'd be surprising to me.

Comment thread GeneralsMD/Code/GameEngine/Include/GameLogic/Scripts.h Outdated
Comment thread GeneralsMD/Code/GameEngine/Include/GameLogic/Scripts.h Outdated
Comment thread GeneralsMD/Code/GameEngine/Include/GameLogic/Scripts.h Outdated
@bobtista

bobtista commented Aug 11, 2026

Copy link
Copy Markdown

I found one issue a few times, otherwise this looks good to me, and wow on the performance boost - great find :)

btw potential follow-up after this merges: findScript/findGroup are still O(total scripts) linear scans across every side, called per lookup.

Edit: Just noticed Caball had already found these - sorry for duplicating!

@Mauller

Mauller commented Aug 11, 2026

Copy link
Copy Markdown
Author

const got a bit out of hand; use these:

const AsciiString& getName() const { return m_groupName;} const AsciiString& getName() const { return m_scriptName;} const AsciiString& getConditionTeamName() const {return m_conditionTeamName;}

Did removing the global lock from the AsciiString member functions provide less performance improvement than these changes? That'd be surprising to me.

Yeah, removing the global lock did provide less performance compared to passing references. The same happened with the waypoint and trigger labels fix.

@Mauller
Mauller force-pushed the Mauller/perf-script-lookup branch from 73f3c67 to ef84147 Compare August 11, 2026 06:02
@Mauller

Mauller commented Aug 12, 2026

Copy link
Copy Markdown
Author

Tweaked and ready to go, i have no idea why some checks are not running though.

@Caball009

Copy link
Copy Markdown

It probably detects you didn't make changes to the Generals code yet.

@Mauller

Mauller commented Aug 12, 2026

Copy link
Copy Markdown
Author

It probably detects you didn't make changes to the Generals code yet.

I did not realise that change had been implemented.

@xezon xezon changed the title perf(script): improve script search performance by 7-8x perf(script): Reduce cost of ScriptEngine::findScript() by around 85% Aug 12, 2026
@xezon xezon added Major Severity: Minor < Major < Critical < Blocker and removed Memory Is memory related labels Aug 12, 2026

@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.

Looking good

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

Labels

Gen Relates to Generals Major Severity: Minor < Major < Critical < Blocker Performance Is a performance concern Script Is related to Script Engine, SCB ZH Relates to Zero Hour

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants