Skip to content

Optimize Java Lite code generation for message field getters - #29259

Draft
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_966906244
Draft

Optimize Java Lite code generation for message field getters#29259
copybara-service[bot] wants to merge 1 commit into
mainfrom
test_966906244

Conversation

@copybara-service

Copy link
Copy Markdown

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:

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

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
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