Skip to content

Commit feac3e2

Browse files
committed
Merge branch 'dynamicbuffer'
2 parents 5133fdb + e21f161 commit feac3e2

File tree

4 files changed

+47
-30
lines changed

4 files changed

+47
-30
lines changed

sqlite3/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 2.4.7
2+
3+
- Web: Improve performance of in-memory and IndexedDB file system implementations.
4+
15
## 2.4.6
26

37
- WebAssembly: Call `_initialize` function of sqlite3 module if one is present.

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

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'dart:math';
88
import 'dart:typed_data';
99

1010
import 'package:meta/meta.dart';
11+
import 'package:typed_data/typed_buffers.dart';
1112
import 'package:web/web.dart' as web;
1213

1314
import '../../constants.dart';
@@ -533,7 +534,12 @@ final class IndexedDbFileSystem extends BaseVirtualFileSystem {
533534
final name = entry.key;
534535
final fileId = entry.value;
535536

536-
_memory.fileData[name] = await _asynchronous.readFully(fileId);
537+
final buffer = Uint8Buffer();
538+
final data = await _asynchronous.readFully(fileId);
539+
buffer.length = data.length;
540+
buffer.setRange(0, data.length, data);
541+
542+
_memory.fileData[name] = buffer;
537543
}
538544
}
539545

@@ -642,18 +648,24 @@ class _IndexedDbFile implements VirtualFileSystemFile {
642648
void xWrite(Uint8List buffer, int fileOffset) {
643649
vfs._checkClosed();
644650

645-
final previousContent = vfs._memory.fileData[path] ?? Uint8List(0);
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+
658+
final previousContent = vfs._memory.fileData[path] ?? Uint8Buffer();
659+
final previousList =
660+
previousContent.buffer.asUint8List(0, previousContent.length);
646661
memoryFile.xWrite(buffer, fileOffset);
647662

648-
if (!vfs._inMemoryOnlyFiles.contains(path)) {
649-
// We need to copy the buffer for the write because it will become invalid
650-
// after this synchronous method returns.
651-
final copy = Uint8List(buffer.length);
652-
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);
653666

654-
vfs._submitWork(_WriteFileWorkItem(vfs, path, previousContent)
655-
..writes.add(_OffsetAndBuffer(fileOffset, copy)));
656-
}
667+
vfs._submitWork(_WriteFileWorkItem(vfs, path, previousList)
668+
..writes.add(_OffsetAndBuffer(fileOffset, copy)));
657669
}
658670
}
659671

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

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ import 'dart:math';
22
import 'dart:typed_data';
33

44
import 'package:path/path.dart' as p;
5+
import 'package:typed_data/typed_buffers.dart';
56

67
import '../../constants.dart';
78
import '../../vfs.dart';
89
import 'utils.dart';
910

1011
final class InMemoryFileSystem extends BaseVirtualFileSystem {
11-
final Map<String, Uint8List?> fileData = {};
12+
final Map<String, Uint8Buffer?> fileData = {};
1213

1314
InMemoryFileSystem({super.name = 'dart-memory', super.random});
1415

@@ -34,7 +35,7 @@ final class InMemoryFileSystem extends BaseVirtualFileSystem {
3435
final create = flags & SqlFlag.SQLITE_OPEN_CREATE;
3536

3637
if (create != 0) {
37-
fileData[pathStr] = Uint8List(0);
38+
fileData[pathStr] = Uint8Buffer();
3839
} else {
3940
throw VfsException(SqlError.SQLITE_CANTOPEN);
4041
}
@@ -69,7 +70,8 @@ class _InMemoryFile extends BaseVfsFile {
6970
if (file == null || file.length <= offset) return 0;
7071

7172
final available = min(buffer.length, file.length - offset);
72-
buffer.setRange(0, available, file, offset);
73+
final list = file.buffer.asUint8List(0, file.length);
74+
buffer.setRange(0, available, list, offset);
7375
return available;
7476
}
7577

@@ -102,12 +104,12 @@ class _InMemoryFile extends BaseVfsFile {
102104
void xTruncate(int size) {
103105
final file = vfs.fileData[path];
104106

105-
final result = Uint8List(size);
106-
if (file != null) {
107-
result.setRange(0, min(size, file.length), file);
107+
if (file == null) {
108+
vfs.fileData[path] = Uint8Buffer();
109+
vfs.fileData[path]!.length = size;
110+
} else {
111+
file.length = size;
108112
}
109-
110-
vfs.fileData[path] = result;
111113
}
112114

113115
@override
@@ -117,19 +119,17 @@ class _InMemoryFile extends BaseVfsFile {
117119

118120
@override
119121
void xWrite(Uint8List buffer, int fileOffset) {
120-
final file = vfs.fileData[path] ?? Uint8List(0);
121-
final increasedSize = fileOffset + buffer.length - file.length;
122+
var file = vfs.fileData[path];
122123

123-
if (increasedSize <= 0) {
124-
// Can write directy
125-
file.setRange(fileOffset, fileOffset + buffer.length, buffer);
126-
} else {
127-
// We need to grow the file first
128-
final newFile = Uint8List(file.length + increasedSize)
129-
..setAll(0, file)
130-
..setAll(fileOffset, buffer);
124+
if (file == null) {
125+
file = Uint8Buffer();
126+
vfs.fileData[path] = file;
127+
}
131128

132-
vfs.fileData[path] = newFile;
129+
var endIndex = fileOffset + buffer.length;
130+
if (endIndex > file.length) {
131+
file.length = endIndex;
133132
}
133+
file.setRange(fileOffset, endIndex, buffer);
134134
}
135135
}

sqlite3/pubspec.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: sqlite3
22
description: Provides lightweight yet convenient bindings to SQLite by using dart:ffi
3-
version: 2.4.6
3+
version: 2.4.7
44
homepage: https://github.com/simolus3/sqlite3.dart/tree/main/sqlite3
55
issue_tracker: https://github.com/simolus3/sqlite3.dart/issues
66

@@ -27,6 +27,7 @@ dependencies:
2727
meta: ^1.3.0
2828
path: ^1.8.0
2929
web: ^1.0.0
30+
typed_data: ^1.3.2
3031

3132
dev_dependencies:
3233
analyzer: ^6.4.1

0 commit comments

Comments
 (0)