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 vehicle viewport hash to find clicked vehicle. #11675

Merged
merged 1 commit into from Jan 3, 2024

Conversation

PeterN
Copy link
Member

@PeterN PeterN commented Jan 3, 2024

Motivation / Problem

Clicking on a vehicle in a viewport needs to find the which vehicle was clicked on. This is done by iterating through all vehicles that exist to find the nearest one.

On a game with a lot of vehicles this can take some time (but only in the order of milliseconds.)

Description

Use the existing vehicle viewport hash instead.

This avoids having to iterate all vehicles, which can be a minor performance improvement when there are many vehicles.

Performance measurements with the old stalwart wentbourne game:

version release debug
master avg 1100µs (1.1ms) avg 6500µs (6.5ms)
this pull request 5-20µs 10-30µs

Limitations

The code is more complex as it duplicates the viewport hash loop from ViewportAddVehicles(). While that could be abstracted out to allow reuse, it would then be slightly detrimental to viewport drawing performance.

This is a fairly unnecessary performance change, as a user cannot click on vehicles fast enough to notice an issue.

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')

This avoids having to iterate all vehicles, which can be an performance improvement when there are many vehicles.
Copy link
Member

@TrueBrain TrueBrain left a comment

Choose a reason for hiding this comment

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

I assume you tested it :)

@PeterN
Copy link
Member Author

PeterN commented Jan 3, 2024

I assume you tested it :)

"It's so quick you don't even see the window appear..."

@PeterN PeterN merged commit 3864902 into OpenTTD:master Jan 3, 2024
19 checks passed
@PeterN PeterN deleted the checkclickonvehiclehash branch January 3, 2024 18:36
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