Optimize Java Lite code generation for message field getters - #29259
Draft
copybara-service[bot] wants to merge 1 commit into
Draft
Optimize Java Lite code generation for message field getters#29259copybara-service[bot] wants to merge 1 commit into
copybara-service[bot] wants to merge 1 commit into
Conversation
Read the message field into a local variable before checking for null
and returning the default instance or the field value.
In D8 debug mode (dev builds), ternary expressions on fields emit two
separate iget-object instructions and an extra goto branch. Reading to a
local variable ensures only a single iget-object instruction is emitted
and eliminates the extra branch in dev dex bytecode.
Before:
```
public Header getHeader() {
return header_ == null ? Header.getDefaultInstance() : header_;
}
```
```
.method public getHeader()LHeader;
iget-object v0, p0, LMyProto;->header_:LHeader; # Read 1
if-nez v0, :cond_9
invoke-virtual {p0}, LMyProto;->getDefaultInstance()LHeader;
move-result-object v0
goto :goto_b # Extra branch jump
:cond_9
iget-object v0, p0, LMyProto;->header_:LHeader; # Read 2 (redundant)
:goto_b
return-object v0
.end method
```
After:
```
.method public getHeader()LHeader;
iget-object v0, p0, LMyProto;->header_:LHeader; # Exactly 1 iget-object
if-nez v0, :cond_8
invoke-virtual {p0}, LMyProto;->getDefaultInstance()LHeader;
move-result-object v0
:cond_8
return-object v0 # 0 goto jumps
.end method
```
PiperOrigin-RevId: 966906244
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Optimize Java Lite code generation for message field getters
Read the message field into a local variable before checking for null
and returning the default instance or the field value.
In D8 debug mode (dev builds), ternary expressions on fields emit two
separate iget-object instructions and an extra goto branch. Reading to a
local variable ensures only a single iget-object instruction is emitted
and eliminates the extra branch in dev dex bytecode.
Before:
After: