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

Inplace construct error message in StatusOr while data type is large enough #1198

Merged
merged 2 commits into from
Dec 21, 2022

Conversation

PragmaTwice
Copy link
Member

@PragmaTwice PragmaTwice commented Dec 20, 2022

We currently use std::unique_ptr<std::string> in StatusOr<T> to represent the error message (in the union type: T or error message), in order to make the error message as small as possible. Hence the size of StatusOr<int> will be equal to 2 * sizeof(void*) rather than sizeof(void*) + sizeof(std::string), where sizeof(std::string) is 32 in libstdc++ (so basically 16 vs 40).
But if sizeof(T) is large than sizeof(std::string) by itself, it is unnecessary to construct the error message on heap for a more compact stack size (since it will never be more compact). Thus we can construct the error message in StatusOr in-place while data type is large enough (i.e. sizeof(T) >= sizeof(std::string)).

@PragmaTwice PragmaTwice marked this pull request as ready for review December 20, 2022 09:45
Copy link
Contributor

@torwig torwig left a comment

Choose a reason for hiding this comment

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

LGTM

@PragmaTwice
Copy link
Member Author

Thanks all. Merging...

@PragmaTwice PragmaTwice merged commit 4516b75 into apache:unstable Dec 21, 2022
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.

2 participants