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: Remove std::function from Pool iteration wrapper #7911

Conversation

JGRennison
Copy link
Contributor

Remove std::function from Pool iteration wrapper.
Add a separate functor templated wrapper for filtered Pool iteration.

Using std::function results in very poor code generation, and significantly increased code size.
It requires the use of expensive indirect calls, prevents optimisations and inlining, requires various exception and storage overheads, etc.
Trivial functions such as ResetVehicleColourMap() are more than 10x larger in code size than before the change to using PoolIterator instead of macros.
The use of a filter functor even in the unfiltered case creates a significant penalty relative to just checking if the filter is null/empty.

std::function can be removed entirely in the case of non-filtered iteration (the vast majority of cases), and replaced with zero-overhead template functors for filtered iteration, this results in comparable code size and performance to before the change to using PoolIterator.

@LordAro LordAro requested a review from glx22 January 6, 2020 19:23
{
return Pool::IterateWrapper<Engine>(from, [vt](size_t index) { return Engine::Get(index)->type == vt; });
return Pool::IterateWrapperFiltered<Engine, EngineTypeFilter>(from, EngineTypeFilter{ vt });
Copy link
Member

Choose a reason for hiding this comment

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

Without having actually tried it, but a lambda is supposed to implicitly convert to a functor.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately you cannot use a lambda as a template parameter, and the full typename needs to be in the return type
Very broadly, lambdas only usefully convert to functors when using them as a parameter to a templated function call.

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.

You can remove

#include <functional>
as I added it for std::function

Add a separate template wrapper for filtered iteration
@JGRennison JGRennison force-pushed the remove-std-function-pool-iteration-wrapper branch from d25c640 to 17ed94c Compare January 7, 2020 00:02
@LordAro LordAro merged commit 150dfba into OpenTTD:master Jan 7, 2020
@JGRennison JGRennison deleted the remove-std-function-pool-iteration-wrapper branch January 9, 2024 19:16
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

4 participants