Skip to content

Commit

Permalink
Improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
simc committed Feb 14, 2020
1 parent e75a30a commit f74da5c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
9 changes: 5 additions & 4 deletions hive/lib/src/io/buffered_file_writer.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:io';

class BufferedFileWriter {
static const defaultMaxBufferSize = 1000000;
static const defaultMaxBufferSize = 64000;

final RandomAccessFile file;

Expand All @@ -11,11 +11,12 @@ class BufferedFileWriter {

BufferedFileWriter(this.file, [this.maxBufferSize = defaultMaxBufferSize]);

Future<void> write(List<int> bytes) async {
Future<void> write(List<int> bytes) {
_buffer.add(bytes);
if (_buffer.length >= maxBufferSize) {
await flush();
return flush();
}
_buffer.add(bytes);
return Future.value();
}

Future<void> flush() {
Expand Down
8 changes: 4 additions & 4 deletions hive/test/tests/io/buffered_file_writer_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ void main() {
var writer = BufferedFileWriter(file, 10);

await writer.write([1, 2, 3, 4, 5, 6]);
await writer.write([7, 8, 9, 10]);
await writer.write([7, 8, 9]);
verifyZeroInteractions(file);

await writer.write([11]);
await writer.write([10]);
verify(file.writeFrom([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]));

reset(file);
await writer.flush();
verify(file.writeFrom([11]));
verifyZeroInteractions(file);

await writer.write([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]);
await writer.write([]);
verify(file.writeFrom([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]));
});

Expand Down

0 comments on commit f74da5c

Please sign in to comment.