file.dart icon indicating copy to clipboard operation
file.dart copied to clipboard

MemoryFileSystem is 10x slower to write than LocalFileSystem

Open mymikemiller opened this issue 4 years ago • 6 comments

With the code below, MemoryFileSystem is taking 10 times longer to write to a file than it takes when switching out with LocalFileSystem.

import 'dart:io';

import 'package:file/file.dart' as f;
import 'package:path/path.dart' as p;
import 'package:file/memory.dart';
import 'package:file/local.dart';

final memoryFileSystem = MemoryFileSystem();
final localFileSystem = LocalFileSystem();

f.Directory createTempDirectory(f.FileSystem filesystem) {
  final root = filesystem.systemTempDirectory.childDirectory('test');
  root.createSync();
  final newTempFolder = root.createTempSync();
  return newTempFolder;
}

Future<void> main() async {
  final fs = memoryFileSystem;
  // final fs = localFileSystem;

  final tempDirectory = createTempDirectory(fs);
  final file = fs.file(p.join(tempDirectory.path, 'test.txt'));
  var output = file.openWrite(mode: FileMode.writeOnlyAppend);

  var i = 0;

  final stopwatch = Stopwatch()..start();
  while (i++ < 500000) {
    output.add([i]);
  }
  print('closing after ${stopwatch.elapsed}');
  await output.close();

  print('done in ${stopwatch.elapsed}');
}

My results are as follows:

localFileSystem: closing after 0:00:00.093014 done in 0:00:22.261997

memoryFileSystem: closing after 0:00:00.255479 done in 0:03:48.186438

I would think memoryFileSystem should actually be faster since it's not writing to disk. Either way, a 10x slowdown seems excessive and means I'll need to find another solution for my needs. Or maybe you can spot something I'm doing wrong here so I can use MemoryFileSystem in my production app where write speed is important.

mymikemiller avatar Jul 31 '21 00:07 mymikemiller