Skip to content

Linter to suggest removing the Future return type for the synchronous option #59852

@FMorschel

Description

@FMorschel

After some discussions over at #58862 (comment), if we ever end up having some form of lint that was merely a hint or nudge to point to the user another option, I'd like to ask for a feature like #59814 but that would encourage users to change the return type for the function.

As per @scheglov in #59814 (comment):

When we can change the return type, it would be really good to do so.

Benchmark code
Future<int> getValueAsync() async {
  return 42;
}

int getValueSync() {
  return 42;
}

Future<void> runAsyncBenchmark() async {
  final timer = Stopwatch()..start();
  var acc = 0;
  for (var i = 0; i < 10000000; i++) {
    acc = (acc + await getValueAsync()) & 0xFFFFFF;
  }
  timer.stop();
  print('Async Benchmark: ${timer.elapsedMicroseconds} us');
}

Future<void> runSyncBenchmark() async {
  final timer = Stopwatch()..start();
  var acc = 0;
  for (var i = 0; i < 10000000; i++) {
    acc = (acc + getValueSync()) & 0xFFFFFF;
  }
  timer.stop();
  print(' Sync Benchmark: ${timer.elapsedMicroseconds} us');
}

void main() async {
  // Warm-up (very basic in this example)
  print('--- warm-up');
  await runAsyncBenchmark();
  await runSyncBenchmark();

  // Run benchmarks for measurement
  print('--- benchmark');
  await runAsyncBenchmark();
  await runSyncBenchmark();
}

Results

--- warm-up
Async Benchmark: 895403 us
 Sync Benchmark: 3889 us
--- benchmark
Async Benchmark: 888209 us
 Sync Benchmark: 3973 us

It looks more than 100x faster for this trivial code.

Opening this issue so this idea is not lost and others can chime in.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P4area-devexpFor issues related to the analysis server, IDE support, linter, `dart fix`, and diagnostic messages.devexp-linterIssues with the analyzer's support for the linter packagelinter-lint-proposaltype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions