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
Merged

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

Minor codestyle things

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

Loading

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

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

@LordAro LordAro Jul 8, 2021

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?

Loading

Copy link
Contributor Author

@fsimonis fsimonis Jul 8, 2021

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

Loading

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 fsimonis force-pushed the improve-linecache branch from 0b48ce5 to d0f8b19 Jul 8, 2021
@fsimonis
Copy link
Contributor Author

@fsimonis fsimonis commented Jul 25, 2021

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

Loading

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

@LordAro LordAro left a comment

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

Loading

@michicc michicc merged commit ac4a7d0 into OpenTTD:master Aug 16, 2021
15 checks passed
Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants