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: Reorder some high-use structs to reduce their size. #11201

Merged
merged 2 commits into from
Sep 2, 2023

Conversation

PeterN
Copy link
Member

@PeterN PeterN commented Aug 15, 2023

Motivation / Problem

Alignment of struct members can create gaps in structs, so that the memory required for each is larger than necessary.

Description

Manually reorder some high use structs to reduce padding:

Struct Old size New Size
GoodsEntry 144 136
CargoPacket 40 32

Limitations

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

@TrueBrain
Copy link
Member

Nice! And smart :D

Did we ever settle on a = 1 or a{1} as class member initializer? Can't remember what the result of the conversation was .. or if there was even a result :)

@PeterN
Copy link
Member Author

PeterN commented Aug 15, 2023

I preferred {}, because you can init an array, or vector (etc.) in the same way.

GRFError sets precedent! ;)

I have other patches that do it this way too, but I seem to have not pushed them upstream yet :)

This reduces GoodsEntry from 144 to 136 bytes, therefore reducing Station from 9704 bytes to 9192 bytes.
This reduces CargoPacket from 40 bytes to 32 bytes each.
@PeterN PeterN marked this pull request as ready for review September 2, 2023 20:00
@PeterN PeterN enabled auto-merge (squash) September 2, 2023 20:41
@PeterN PeterN merged commit bd150df into OpenTTD:master Sep 2, 2023
20 checks passed
@PeterN PeterN deleted the reorder-structs branch September 2, 2023 21:03
JGRennison added a commit to JGRennison/OpenTTD-patches that referenced this pull request Sep 2, 2023
Reduce size from 40 bytes to 32 bytes.
See: OpenTTD/OpenTTD#11201
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

2 participants