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

Codechange: Use locals for company/deity mode during loops. #11686

Merged
merged 1 commit into from
Jan 5, 2024

Conversation

PeterN
Copy link
Member

@PeterN PeterN commented Jan 5, 2024

Motivation / Problem

Script methods that build lists with ownership checks call ScriptObject::GetCompany() and/or ScriptCopmanyMode::IsDeity() for every iteration.

While individually these are not very expensive, the cost adds up.

Description

Instead, store the value of these calls in local variables before iterating the loops. This gives a reasonable benefit with large games and heavy scripts, though of course in practice results vary.

This is the result using Xarick's corona gs save from #10548:

image

Limitations

This is related to but conflicts with #11676. I'm made this a separate PR as it has wider scope and the changes are a bit more straightforward.

Checklist for review

Some things are not automated, and forgotten often. This list is a reminder for the reviewers.

  • The bug fix is important enough to be backported? (label: 'backport requested')
  • This PR touches english.txt or translations? Check the guidelines
  • This PR affects the save game format? (label 'savegame upgrade')
  • This PR affects the GS/AI API? (label 'needs review: Script API')
    • ai_changelog.hpp, game_changelog.hpp need updating.
    • The compatibility wrappers (compat_*.nut) need updating.
  • This PR affects the NewGRF API? (label 'needs review: NewGRF')

Copy link
Contributor

@glx22 glx22 left a comment

Choose a reason for hiding this comment

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

As it already conflicts with the other PR, maybe switch to if (!...) continue where it reduces indentation.

src/script/api/script_enginelist.cpp Outdated Show resolved Hide resolved
@PeterN PeterN merged commit 91ea8e3 into OpenTTD:master Jan 5, 2024
20 checks passed
@PeterN PeterN deleted the script-locals-for-loop branch January 5, 2024 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants