google / google/built_value.dart

Formatting generated code is slow

Open
#1,335 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Dart
Stars
886
Forks
195
Avg merge
1d 11h
Merged PRs (30d)
4

Description

In `end_to_end_test`:

```
> dart format --language-version=3.6 lib/values.g.dart
Formatted 1 file (0 changed) in 2.61 seconds.
```

This is with the current/deprecated "short" formatting style. The new formatter is much faster:

```
dart format --language-version=3.7 lib/values.g.dart
Formatted lib/values.g.dart
Formatted 1 file (1 changed) in 0.76 seconds.
```

Note that there is overhead from the command startup, so the speedup is bigger than 3x.

Increasing the line width can help but it's not pretty and the `// dart format width=1000000` trick is only possible from 3.7; I'd like to land something that helps existing users.

It looks like it's this clause in the output that causes the problems:

```
_$result = _$v ??
new _$CompoundValueNoNestingField._(
simpleValue: BuiltValueNullFieldError.checkNotNull(
simpleValue, r'CompoundValueNoNestingField', 'simpleValue'),
validatedValue: validatedValue,
simpleValueWithNested: simpleValueWithNested.build(),
validatedValueWithNested: _validatedValueWithNested?.build());

```

and it can be made fast by adding one trailing comma.

```
_$result = _$v ??
new _$CompoundValueNoNestingField._(
simpleValue: BuiltValueNullFieldError.checkNotNull(
simpleValue, r'CompoundValueNoNestingField', 'simpleValue'),
validatedValue: validatedValue,
simpleValueWithNested: simpleValueWithNested.build(),
validatedValueWithNested: _validatedValueWithNested?.build(),
);

> dart format --language-version=3.6 lib/values.g.dart
Formatted 1 file (0 changed) in 0.72 seconds.
```

Both versions are fast with "tall" style.

I have a suspicion I'll want to backport this change, so I'll land the absolute minimal fix, i.e. this one comma, first.

A format benchmark also looks like a good thing to have.

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.