Skip to content

Commit 0a8e4d6

Browse files
committed
Avoid copying for writes to memory
1 parent a0032ff commit 0a8e4d6

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

sqlite3/lib/src/wasm/vfs/indexed_db.dart

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -648,20 +648,24 @@ class _IndexedDbFile implements VirtualFileSystemFile {
648648
void xWrite(Uint8List buffer, int fileOffset) {
649649
vfs._checkClosed();
650650

651+
if (vfs._inMemoryOnlyFiles.contains(path)) {
652+
// There's nothing to persist, so we just forward the write to the in-
653+
// memory buffer.
654+
memoryFile.xWrite(buffer, fileOffset);
655+
return;
656+
}
657+
651658
final previousContent = vfs._memory.fileData[path] ?? Uint8Buffer();
652659
final previousList =
653660
previousContent.buffer.asUint8List(0, previousContent.length);
654661
memoryFile.xWrite(buffer, fileOffset);
655662

656-
if (!vfs._inMemoryOnlyFiles.contains(path)) {
657-
// We need to copy the buffer for the write because it will become invalid
658-
// after this synchronous method returns.
659-
final copy = Uint8List(buffer.length);
660-
copy.setAll(0, buffer);
663+
// We need to copy the buffer for the write because it will become invalid
664+
// after this synchronous method returns.
665+
final copy = Uint8List(buffer.length)..setAll(0, buffer);
661666

662-
vfs._submitWork(_WriteFileWorkItem(vfs, path, previousList)
663-
..writes.add(_OffsetAndBuffer(fileOffset, copy)));
664-
}
667+
vfs._submitWork(_WriteFileWorkItem(vfs, path, previousList)
668+
..writes.add(_OffsetAndBuffer(fileOffset, copy)));
665669
}
666670
}
667671

0 commit comments

Comments
 (0)