hashgraph / hashgraph/pbj

Performance: Replace oneOf if statements with switches

Open
#400 3 comments 0 reactions 0 assignees View on GitHub
Good First Issue
Dominant language
Java
Stars
44
Forks
15
Avg merge
1d 15h
Merged PRs (30d)
12

Description

### Problem

In generated PBJ code in constructors, measure, equals methods etc each oneof option is a seperate if statement. For example:
```
// handle special case where protobuf does not have destination between a OneOf with optional
// value of empty vs an unset OneOf.
if (oneofExample.kind() == OneofExampleOneOfType.INT32_BOXED_ONE_OF && oneofExample.value() == null) {
oneofExample = new OneOf<>(OneofExampleOneOfType.UNSET, null);
}
// handle special case where protobuf does not have destination between a OneOf with optional
// value of empty vs an unset OneOf.
if (oneofExample.kind() == OneofExampleOneOfType.UINT32_BOXED_ONE_OF && oneofExample.value() == null) {
oneofExample = new OneOf<>(OneofExampleOneOfType.UNSET, null);
}
....
```

### Solution

It seems like they could be released by a single if and switch statement like:
```
if (oneofExample.value() == null) {
oneofExample = switch(oneofExample.kind()) {
case OneofExampleOneOfType.INT32_BOXED_ONE_OF -> new OneOf<>(OneofExampleOneOfType.UNSET, null);
case OneofExampleOneOfType.UINT32_BOXED_ONE_OF ->new OneOf<>(OneofExampleOneOfType.UNSET, null);
...
```
And similar in other places in generated code. The hope is this would be faster, code would at least be nicer to read. Would need JMH performance testing to confirm.

### Alternatives

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.