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: Build station and depot vehicle lists from shared order lists. #11676
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems reasonable to me.
I'm seeing four times essentially the same loop, with only small differences in conditionals at different places. It looks to me like this can be written in a single templated function, with some small functions for the differentiating logic. Like whether to filter on company or not, what type of order is allowed, and how to add to a collection. That would split the internals of the (complicated) loops from the actual important bits for each of the instances. It's bit similar to something like |
7dd93d8
to
dc01a17
Compare
I've duffed the code a bit, replacing nested if conditions with continue. And here's a good thing. Making locals out of dbg: [misc] [Old] 3663 us [avg: 3663.0 us] |
I had a play with this and ended up with the function like this.
This seems to be at least about 50% slower. Not sure if I'm doing it wrong, or if it's because it needs to capture variables. |
Maybe do the same for |
Ah, good point! |
dc01a17
to
b5647b8
Compare
Performance difference since #11686: dbg: [misc] [old] 207814 us [avg: 2078.1 us] But please double check I haven't got the conditions wrong :) |
…ists. The brings some performance advantages: * No need to iterate all vehicles and check for primary vehicle as only vehicles that can have orders are listed. * Shared orders only need to be tested once instead of for each vehicle sharing them. * Vehicle tests only need to be performed on the first shared vehicle instead of all.
b5647b8
to
1e5a5f0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the category of massive bike shedding... but if you're looking for optimal performance, some things that might help:
- move the
order->GetDestination
check to before theorder->IsType
checks. For an order it's generally way more likely that the destination ID differs from what you want, than whether it's station vs depot, as such the destination check drops way more and that saves the two or three IsType expressions. Technicallyorder->GetDestination
requires the order to be one of a few specific types, but that isn't checked and I think the documentation oforder->GetDestination
is not even listing all of them. - when is_deity is true, you can make a separate call where the vehicle predicate just returns true, which ought to remove the whole predicate with minor optimisations; then for the other branch you only test for owner. It's more code, but fewer comparisons, so it might make a difference.
Of both I'm definitely not saying that we should do it, but rather that it could be done and could improve performance even more. Even though I do not think this code is called that often.
Splitting the deity check seems to save on average about 20µs (so about 10%) in the stationlist case. |
Motivation / Problem
When building lists of vehicles from stations and depots, we iterate all vehicles, filter to find suitable vehicles, and then check each other to see if it matches the station/depot we're listing.
This is a bit bruteforce, and means for shared orders, we are checking order lists multiple times.
Description
Instead, build station and depot vehicle lists from shared order lists.
This brings some performance advantages:
Stats from Xarick's corona test game:
Stats from Wentbourne:
Limitations
This is reasonable improvement, and only uses data that we already have.
Checklist for review
Some things are not automated, and forgotten often. This list is a reminder for the reviewers.