google / google/protobuf.dart

Types for better dart2js code generation

Open
#157 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
572
Forks
196
Avg merge
1h 59m
Merged PRs (30d)
2

Description

dart2js generates poor code for repeated elements. The reason is that, although repeated elements are always represented as PbLists, dart2js cannot infer this since the only type assertion is to List.

An example of poor code is

```dart
return reportSelector.axis.first;
```

compiles to
```js
return J.get$first$ax(reportSelector._fieldSet._$get$3(0, 1, null));
```
`J.get$first$ax` is a dynamic dispatch indexed off the runtime type.

The proto generated code has:
```
List get axis => $_get(0, 1, null);

bool get includeTotalRow => $_get(3, 6, false);
set includeTotalRow(bool v) { $_setBool(3, 6, v); }
bool hasIncludeTotalRow() => $_has(3, 6);
void clearIncludeTotalRow() => clearField(6);
```

With --trust-type-annotations, dart2js will know that `pb.includeTotalRow` is a bool, but since there is no PbList in the generated code and the source is `dynamic`, dart2js can't infer that `pb.axis` is a list.

A good option is to change the generated code to contain the full type as a type annotation.

```dart
List get axis {
PbList list = $_get(0, 1, null);
return list;
}
```

What is also required is to ensure all paths actually return a PbList.

- [ ] Creating a ReadOnlyPbList to return for missing fields of read-only protobufs
- [ ] Only use the above typeful form when there are no mixins overriding the default list representation

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.