-
Notifications
You must be signed in to change notification settings - Fork 5.9k
*: Optimize struct memory usage by adjust field order #11629
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
Conversation
Codecov Report
@@ Coverage Diff @@
## master #11629 +/- ##
================================================
- Coverage 81.5272% 81.2134% -0.3138%
================================================
Files 432 426 -6
Lines 93635 91885 -1750
================================================
- Hits 76338 74623 -1715
- Misses 11868 11896 +28
+ Partials 5429 5366 -63 |
@Deardrops please add some proper labels for this PR. |
any performance result? |
It's better to add |
Done
I choose current (48 byte total, reported by type concatFunction struct {
aggFunction
separator string
sepInited bool
maxLen uint64
truncated bool
} new (40 byte total): type concatFunctionNew struct {
aggFunction
separator string
maxLen uint64
sepInited bool
truncated bool
} Benchmark result:
In my view, It's unnecessary. Althought all structed will be scanned, not all struct will be changed.(some of them are too complicated to maintain). There is no to add the static check tool into |
/run-all-tests |
The follow-up code should do the check themself, not that automatically. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@Deardrops It's better to add Not every programmer knows or realizes the memory alignment of struct. We should use tools to notify them about this. In order to not file such a huge PR in the future. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Static check could be the guide, but not the rule. It's not a good idea that add this static check in |
/LGTM |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@Deardrops If we do not require the programmer to check the memory alignment, the percentage of unaligned struct will be increased to more than 20% in the future. It's hard to make every programmer to check the memory alignment manually every time they file a PR. |
I don't think we should introduce static checking tools into MakeFile. |
What problem does this PR solve?
In golang, the compiler performs memory alignment on the fields in the structure.
For example, There are two struct with same field but different order.
In


x86_64
platform, each fields will align to8 byte
, as shown in the figures:Total: 8 byte + 8 byte + 8 byte = 24 byte
Total: 8 byte + 8 byte = 16 byte
Different field orders determine the memory size of the structure.
Reasonable field orders can reduce memory overhead.
What is changed and how it works?
Using golangci-lint static check all code in TiDB
And then we deal with the struct needed optimization one by one, there are three cases:
test
file, and it will not affect the memory usage when the program is running, no changes will be made.Check List
Tests
Code changes
Side effects
Related changes