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: Replace path cache deques with vectors. #12345

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

PeterN
Copy link
Member

@PeterN PeterN commented Mar 21, 2024

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. For RoadVehicles this now contains a std::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.

Type Master PR
Ship 640 bytes + 584 bytes +
RoadVehicle 776 bytes + 640 bytes +
  • Extra memory outside the object is used by both deque and vector.

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.

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

@PeterN PeterN changed the title Codechange: Ship and RoadVehicle path caches use a std::deque, which Codechange: Replace path cache queues with vectors. Mar 21, 2024
@PeterN PeterN force-pushed the path-cache-vector branch 2 times, most recently from a4d827c to b43cff1 Compare March 21, 2024 09:47
@PeterN PeterN marked this pull request as ready for review March 21, 2024 18:30
@PeterN PeterN changed the title Codechange: Replace path cache queues with vectors. Codechange: Replace path cache deques with vectors. Mar 21, 2024
src/saveload/vehicle_sl.cpp Show resolved Hide resolved
std::reverse(std::begin(rv->path), std::end(rv->path));
}
}

void Save(Vehicle *v) const override
Copy link
Contributor

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.

Copy link
Member Author

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.

src/saveload/vehicle_sl.cpp Outdated Show resolved Hide resolved
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.
@PeterN
Copy link
Member Author

PeterN commented Apr 22, 2024

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

@rubidium42
Copy link
Contributor

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.

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

3 participants