-
-
Notifications
You must be signed in to change notification settings - Fork 844
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: Replace path cache deques with vectors. #12345
base: master
Are you sure you want to change the base?
Conversation
a4d827c
to
b43cff1
Compare
b43cff1
to
ff9f086
Compare
std::reverse(std::begin(rv->path), std::end(rv->path)); | ||
} | ||
} | ||
|
||
void Save(Vehicle *v) const override |
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.
A reference probably makes more sense in this context, a nullptr vehicle doesn't make much sense. But I guess I lot of the code here is C-style passing pointers so in that sense it would be consistent. Whatever you prefer.
Same for the other functions btw.
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.
It's the interface to SaveLoad that uses pointers, I'm not adding to changing anything here.
Ship and RoadVehicle path caches use a std::deque, which is quite memory hungry, especially for RoadVehicle which has two. std::deque was used to be able to push/pop from either end. Change to use a single std::vector each, which is now push/popped from the back.
ff9f086
to
b4839cc
Compare
I also have an alternative version of this that uses a standard saveload struct list for the road vehicle path cache, which avoids having to split and merge. However the code for merging is still needed for loading existing saves... |
I think the "standard" way would be best in the long term. If we ever were to change/add something, we don't have to reinvent the conversion if you've already have implemented that. |
Motivation / Problem
RoadVehicle path cache has two std::deques which is quite awkward to manage, and also quite memory hungry.
Ship path cache use a single std::deque.
std::deque was used to be able to push/pop from either end.
#12275 attempts to partially address RoadVehicle's path cache by hiding the fact it has two deques.
With this PR I address both the split, and the memory usage.
Description
Instead, change both to use a single
std::vector
each. ForRoadVehicle
s this now contains astd::pair<Trackdir, TileIndex>
which simplifies usage.This is now push/popped from the back, so sometimes needs to be reversed.
Also a special-case for ships that was copied across in the original implementation, which no longer exists for ships, has been removed for RVs. As far as I can tell this never made any sense, as an RV destination tile cannot have a path finding choice, so would never appear in the path cache anyway.
Limitations
Performance not compared. No regression expected for Ships, but RVs has a little bit of erasing which could perhaps be improved.
Saveload code for RoadVehicle is a bit gnarly as std::pair is difficult to saveload, so it always uses two temporary vectors. Maybe using a struct instead would help.
Checklist for review
Some things are not automated, and forgotten often. This list is a reminder for the reviewers.