Skip to content

Formatting generated code is slow #1335

@davidmorgan

Description

@davidmorgan

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions