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

Optimize control code #13354

Merged
merged 13 commits into from
Aug 18, 2024
Merged

Conversation

JoergAtGithub
Copy link
Member

This PR collects several low level optimizations for our control code. See the single commit messages.

We have currently 36816 ControlDoublePrivate instances in Mixxx the memory realignment reduces the size from 464 byte to 448 byte, this translates to 0.5 MegaByte heap memory reduction.
Furthermore the first fields are aligned to fit into a 128 byte SSE register. But I guess there are very few operation, which can benefit from an SSE copy etc. (in theory we could further optimize for SSE, by reducing ControlValueAtomic from 136byte to 128byte).

Old memory alignment:
ControlDoublePrivateBefore

New memory alignment:
ControlDoublePrivateThisPR

@daschuer
Copy link
Member

The reason that ControlDoublePrivate is so big is a nasty regression. It should be only for bites. The fix is here: #13355

// The control value.
ControlValueAtomic<double> m_value;
// The default control value.
ControlValueAtomic<double> m_defaultValue;
Copy link
Member

Choose a reason for hiding this comment

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

How do we protect that for get cluttered again? Maybe define regions via comments?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

@JoergAtGithub JoergAtGithub Jun 30, 2024

Choose a reason for hiding this comment

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

I tried -Wpadded on CI, but it does not only warn about padding inside of the data structure, but also if the overall size is not matching the byte alignment size, e.g. any class with 6 bytes would need to filled up with 2 explicit padding bytes.

I found also a currently unused macro in

#define M_PACK(statement) __pragma( pack(push, 1) ) statement __pragma( pack(pop) )

Maybe someone on Linux could try: https://linux.die.net/man/1/pahole

Copy link
Member

@Swiftb0y Swiftb0y Jun 30, 2024

Choose a reason for hiding this comment

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

Okay that makes sense I guess when the compiler expects you to nest these packed data structures. The only other potential solution I found was to generate two versions of the type, one packed (potentially with suboptimal alignment and then static_assert(sizeof(T) == sizeof(_T_packed)) (potentially with a bit of rounding to manually account for the overall size). I wouldn't know how to accomplish that without copy pasting or a macro (doing the copy-pasting automatically) though.

Copy link
Member

Choose a reason for hiding this comment

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

Let's not over engineer it. Just a comment state informs there reader that the oder of the variables has been chosen to avoid memory gaps due to alignment is probably sufficient.

Copy link
Member Author

Choose a reason for hiding this comment

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

It's already added:

mixxx/src/control/control.h

Lines 203 to 205 in 6728c91

// Note: keep the order of the members below to not introduce gaps due to
// memory alignment in this often used class. Whether to track value changes
// with the stats framework.

src/control/controlobject.cpp Outdated Show resolved Hide resolved
src/control/controlobject.cpp Outdated Show resolved Hide resolved
src/control/controlpushbutton.cpp Outdated Show resolved Hide resolved
src/control/pollingcontrolproxy.h Outdated Show resolved Hide resolved
@JoergAtGithub
Copy link
Member Author

I merged in the changes from #13355, but I noticed, that the memory consumption of ControlDoublePrivate is much bigger in this Main branch, than in the 2.4 branch.

2.4:
grafik

Main:
grafik

This PR:
grafik

@daschuer
Copy link
Member

I assume you build main with Qt6 and 2.4 with Qt5. Maybe the sizeof(QString) has changed due to the Small String Optimization?

@JoergAtGithub
Copy link
Member Author

I assume you build main with Qt6 and 2.4 with Qt5.

Yes, I did!

@daschuer
Copy link
Member

daschuer commented Jun 13, 2024

Qt6 has no SSO. The number increased due to the use of a common QArrayDataPointer with holding two pointers one the header for implicit sharing, one to the actual data and one to the size which is now also 64 bit. This allows faster read only access and allows to the store strings longer than 2.147.483.648 characters (more than 480 Bibles)

src/control/control.h Show resolved Hide resolved
…nction and made use of it where applicable.

Unified the two ButtonMode enums to a common enum class.
@JoergAtGithub
Copy link
Member Author

@daschuer Friendly Ping

Copy link
Member

@Swiftb0y Swiftb0y left a comment

Choose a reason for hiding this comment

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

a couple comments, thanks for the cleanup.

src/control/controlpushbutton.h Outdated Show resolved Hide resolved
src/control/controlpushbutton.cpp Outdated Show resolved Hide resolved
src/control/controlbuttonmode.h Outdated Show resolved Hide resolved
src/control/controlpushbutton.h Outdated Show resolved Hide resolved
Copy link
Member

@Swiftb0y Swiftb0y left a comment

Choose a reason for hiding this comment

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

LGTM. Thank you.

@Swiftb0y
Copy link
Member

@daschuer please resolve your review

@daschuer daschuer merged commit 9802bae into mixxxdj:main Aug 18, 2024
13 checks passed
@daschuer
Copy link
Member

Thank you!

@JoergAtGithub JoergAtGithub deleted the optimize_control_code branch August 18, 2024 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants