Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
FMorschel opened this issue Jan 7, 2025 · 0 comments
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. linter-lint-request P4

Comments

@FMorschel
Copy link
Contributor

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.

@FMorschel FMorschel added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. linter-lint-request P4
Projects
None yet
Development

No branches or pull requests

2 participants