Skip to content

Reading files asynchronously is 40% slower than reading them synchronously #19156

Open
@peter-ahe-google

Description

The async.dart program below is 40% slower than sync.dart:

==> sync.dart <==
import 'dart:io';

main(List<String> arguments) {
  Stopwatch sw = new Stopwatch()..start();
  Map<String, String> sources = <String, String>{};
  Uri testScheme = new Uri(scheme: 'org-dartlang-test');
  for (String argument in arguments) {
    Uri uri = testScheme.resolve(argument);
    String source =
        new File.fromUri(Uri.base.resolve(argument)).readAsStringSync();
    sources['$uri'] = source;
  }
  sw.stop();
  print(sw.elapsedMilliseconds);
}

==> async.dart <==
import 'dart:io';

import 'dart:async';

main(List<String> arguments) {
  Stopwatch sw = new Stopwatch()..start();
  Map<String, String> sources = <String, String>{};
  Future<String> futures = new List<Future<String>>();
  Uri testScheme = new Uri(scheme: 'org-dartlang-test');
  for (String argument in arguments) {
    Uri uri = testScheme.resolve(argument);
    Future<String> future =
        new File.fromUri(Uri.base.resolve(argument)).readAsString();
    futures.add(future.then((String source) {
      sources['$uri'] = future;
    }));
  }
  Future.wait(futures).then((_) {
    sw.stop();
    print(sw.elapsedMilliseconds);
  });
}

I've measured this at r36733. On my Mac which is a dual 3.06 GHz 6-Core Intel Xeon running OS X version 10.9.3 (mavericks).

I've run the test 10 times (after initial runs to ensure all files are cached) using:

for i in {1..10} ; do ./sdk/bin/dart -pp/ sync.dart tests/compiler/dart2js_extra/*.dart; done

for i in {1..10} ; do ./sdk/bin/dart -pp/ async.dart tests/compiler/dart2js_extra/*.dart; done

On average sync runs in 54.6 milliseconds (average deviation 0.48), and async runs in 75.1 milliseconds (average deviation 0.96). 75.1/54.6 is 138%.

Metadata

Assignees

No one assigned

    Labels

    area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.library-iotype-bugIncorrect behavior (everything from a crash to more subtle misbehavior)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions