Skip to content

Optimize google::api::expr::Unparse by removing unnecessary construction and copy of SourceInfo.#1800

Merged
copybara-service[bot] merged 1 commit into
masterfrom
test_839855268
Dec 5, 2025
Merged

Optimize google::api::expr::Unparse by removing unnecessary construction and copy of SourceInfo.#1800
copybara-service[bot] merged 1 commit into
masterfrom
test_839855268

Conversation

@copybara-service
Copy link
Copy Markdown

Optimize google::api::expr::Unparse by removing unnecessary construction and copy of SourceInfo.

const auto& var = ptr == nullptr ? ProtoMessage() : *ptr; can call ProtoMessage::ProtoMessage constructor whenever ptr is null. But, due to how ternary operators behave compiler will copy *ptr even when ptr is not null.

OTOH, const auto& var = ptr == nullptr ? ProtoMessage::default_instance() : *ptr; won't do the similar call because ProtoMessage::default_instance() already returns a const reference.

@copybara-service copybara-service Bot force-pushed the test_839855268 branch 4 times, most recently from 8da1cfd to 79d135b Compare December 5, 2025 04:18
…ction and copy of `SourceInfo`.

`const auto& var = ptr == nullptr ? ProtoMessage() : *ptr;` can call `ProtoMessage::ProtoMessage` constructor whenever `ptr` is null. But, due to how ternary operators behave compiler will copy `*ptr` even when `ptr` is not null.

OTOH, `const auto& var = ptr == nullptr ? ProtoMessage::default_instance() : *ptr;` won't do the similar call because `ProtoMessage::default_instance()` already returns a const reference.

- The subtle difference between ternary operator and if-else can be seen here: https://godbolt.org/z/bPvjjn6dP
- The overall change is similar to this: https://godbolt.org/z/8Phd9nzEh

PiperOrigin-RevId: 840528597
@copybara-service copybara-service Bot merged commit 20671a0 into master Dec 5, 2025
@copybara-service copybara-service Bot deleted the test_839855268 branch December 5, 2025 04:52
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.

1 participant