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: Improve LineCache queries #9417

Merged
merged 1 commit into from Aug 16, 2021

Conversation

fsimonis
Copy link
Contributor

@fsimonis fsimonis commented Jul 7, 2021

Motivation / Problem

After #9416, one major source of temporary allocations is GetCachedParagraphLayout.
The key of the std::map contains a std::string, while the cache access uses a const char* + size.
The current version constructs the key to query the linecache, which needs to allocate and copy the string for each query.

With C++14, some queries of std::map allow to query using a different type than key using a transparent comparator.

Description

This PR adds the support to query the linecache without copying the string.

Building pieces are:

  • A non-owning type of the cacheline key using a std::string_view
  • A transparent comparator
  • A 2 phase query algorithm. First query with the lightweight query type. Then insert using the key type if needed.

After this PR and #9416, the allocations are dominated by the SpriteSorter.

Limitations

none

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 affects the save game format? (label 'savegame upgrade')
  • This PR affects the GS/AI API? (label 'needs review: Script API')
    • ai_changelog.hpp, gs_changelog.hpp need updating.
    • The compatibility wrappers (compat_*.nut) need updating.
  • This PR affects the NewGRF API? (label 'needs review: NewGRF')

Copy link
Member

@LordAro LordAro left a comment

Choose a reason for hiding this comment

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

Minor codestyle things

commit message needs some tweaking - "CodeChange" -> "Codechange"

src/gfx_layout.cpp Outdated Show resolved Hide resolved
src/gfx_layout.h Outdated Show resolved Hide resolved

/** Comparator for std::map */
struct LineCacheCompare {
using is_transparent = void; ///< Enable map queries with various key types
Copy link
Member

Choose a reason for hiding this comment

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

Don't understand why this is necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is required to enable some overloads in std::map. Example, 3-4

Adds the support to query the linecache without copying the string.
This uses a custom transparent comparator in conjunction with
a query type using a std::string_view.
@fsimonis
Copy link
Contributor Author

@LordAro I implemented the changes. This PR is ready for review.

@LordAro LordAro changed the title CodeChange: Improve LineCache queries Codechange: Improve LineCache queries Aug 16, 2021
Copy link
Member

@LordAro LordAro left a comment

Choose a reason for hiding this comment

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

Sure? Would like to see someone else approve it first though

@michicc michicc merged commit ac4a7d0 into OpenTTD:master Aug 16, 2021
@fsimonis fsimonis deleted the improve-linecache branch March 23, 2022 11:22
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