Skip to content

Commit f8e29bb

Browse files
committed
Reduce size of database workers (#372).
1 parent 3cbab8e commit f8e29bb

40 files changed

Lines changed: 224 additions & 128 deletions

examples/multiplatform/db/sqlite3/web.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Future<CommonDatabase> openSqliteDb() async {
55
const name = 'my_app';
66
// Please download `sqlite3.wasm` from https://github.com/simolus3/sqlite3.dart/releases
77
// into the `web/` dir of your Flutter app. See `README.md` for details.
8-
final sqlite = await WasmSqlite3.loadFromUrl(Uri.parse('sqlite3.wasm'));
8+
final sqlite = await WasmSqlite3.loadFromUrlString('sqlite3.wasm');
99
final fileSystem = await IndexedDbFileSystem.open(dbName: name);
1010
sqlite.registerVirtualFileSystem(fileSystem, makeDefault: true);
1111
return sqlite.open(name);

sqlite3/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
- Support `native_toolchain_c` versions `0.18.x`.
44
- Upgrade to SQLite version 3.53.1.
5+
- Allow disabling `BigInt` support via `-Dsqlite3.dartbigints=false`, which can be used to
6+
reduce code size on the web if nothing else uses `BigInt` values.
57

68
## 3.3.1
79

sqlite3/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ databases. The [hook options page](./doc/hook.md) describe this setup.
5151
When binding parameters to queries, the supported types are `ìnt`,
5252
`double`, `String`, `List<int>` (for `BLOB`) and `null`.
5353
Result sets will use the same set of types.
54-
On the web (but only on the web), `BigInt` is supported as well.
54+
55+
On the web (when compiled with `dart2js`), `BigInt` is supported as well to represent 64bit integers.
56+
Support for this can be disabled with `-Dsqlite3.dartbigints=false`.
5557

5658
## WASM (web support)
5759

@@ -85,7 +87,7 @@ import 'package:sqlite3/common.dart';
8587
import 'package:sqlite3/wasm.dart';
8688
8789
Future<WasmSqlite3> loadSqlite() async {
88-
final sqlite = await WasmSqlite3.loadFromUrl(Uri.parse('sqlite3.wasm'));
90+
final sqlite = await WasmSqlite3.loadFromUrlString('sqlite3.wasm');
8991
final fileSystem = await IndexedDbFileSystem.open(dbName: 'my_app');
9092
sqlite.registerVirtualFileSystem(fileSystem, makeDefault: true);
9193
return sqlite;
@@ -123,7 +125,7 @@ To test the encryption integration, download `sqlite3mc.wasm` from the [releases
123125
of this package and use that as a URL to load sqlite3 on the web:
124126

125127
```dart
126-
final sqlite3 = await WasmSqlite3.loadFromUrl(Uri.parse('sqlite3mc.wasm'));
128+
final sqlite3 = await WasmSqlite3.loadFromUrlString('sqlite3mc.wasm');
127129
sqlite3.registerVirtualFileSystem(InMemoryFileSystem(), makeDefault: true);
128130
129131
final database = sqlite3.open('/database')

sqlite3/doc/common.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Future<CommonDatabase> openDatabase() async {
3838
import 'package:sqlite3/wasm.dart';
3939
4040
Future<CommonDatabase> openDatabase() async {
41-
final sqlite = await WasmSqlite3.loadFromUrl(Uri.parse('sqlite3.wasm'));
41+
final sqlite = await WasmSqlite3.loadFromUrlString('sqlite3.wasm');
4242
final fs = await IndexedDbFileSystem.open(dbName: 'app.db');
4343
sqlite.registerVirtualFileSystem(fs, makeDefault: true);
4444
return sqlite.open('/app.db');

sqlite3/example/web/main.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ Future<void> main() async {
1111

1212
startIndexedDb.onClick.listen((_) async {
1313
startIndexedDb.remove();
14-
final sqlite3 = await WasmSqlite3.loadFromUrl(
15-
Uri.parse('sqlite3.debug.wasm'),
16-
);
14+
final sqlite3 = await WasmSqlite3.loadFromUrlString('sqlite3.debug.wasm');
1715

1816
print(sqlite3.version);
1917

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// Whether to support binding [BigInt] values.
2+
///
3+
/// This is enabled by default, and disabling it is experimental. This is mainly
4+
/// relevant for web workers, which don't really benefit from using [BigInt]
5+
/// values internally:
6+
///
7+
/// - Over the wire, we receive values as `JSBigInt`.
8+
/// - To bind values to SQLite, we need to use `JSBigInt` as well.
9+
///
10+
/// By removing support for [BigInt]s and their conversion to `JSBigInt`, we
11+
/// save around 20kb from the default worker with `dart compile js -O4`.
12+
const supportDartBigInts = bool.fromEnvironment(
13+
'sqlite3.dartbigints',
14+
defaultValue: true,
15+
);

sqlite3/lib/src/constants.dart

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,9 +544,6 @@ const SQLITE_DELETE = 9;
544544
const SQLITE_INSERT = 18;
545545
const SQLITE_UPDATE = 23;
546546

547-
final bigIntMinValue64 = BigInt.parse('-9223372036854775808');
548-
final bigIntMaxValue64 = BigInt.parse('9223372036854775807');
549-
550547
// Connection config options https://www.sqlite.org/c3ref/c_dbconfig_defensive.html
551548
const SQLITE_DBCONFIG_DQS_DML = 1013;
552549
const SQLITE_DBCONFIG_DQS_DDL = 1014;

sqlite3/lib/src/implementation/statement.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import '../compile_options.dart';
12
import '../constants.dart';
23
import '../result_set.dart';
34
import '../statement.dart';
@@ -210,7 +211,10 @@ base class StatementImplementation extends CommonPreparedStatement {
210211
final rc = switch (param) {
211212
null => statement.sqlite3_bind_null(i),
212213
int() => statement.sqlite3_bind_int64(i, param),
213-
BigInt() => statement.sqlite3_bind_int64BigInt(i, param.checkRange),
214+
BigInt() when supportDartBigInts => statement.sqlite3_bind_int64BigInt(
215+
i,
216+
param.checkRange,
217+
),
214218
bool() => statement.sqlite3_bind_int64(i, param ? 1 : 0),
215219
double() => statement.sqlite3_bind_double(i, param),
216220
String() => statement.sqlite3_bind_text(i, param),

sqlite3/lib/src/implementation/utils.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ extension BigIntRangeCheck on BigInt {
88
}
99
return this;
1010
}
11+
12+
static final bigIntMinValue64 = -(BigInt.one << 63);
13+
static final bigIntMaxValue64 = (BigInt.one << 63) - BigInt.one;
1114
}
1215

1316
int eTextRep(bool deterministic, bool directOnly, bool subtype) {

sqlite3/lib/src/in_memory_vfs.dart

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

4-
import 'package:path/path.dart' as p;
54
import 'package:typed_data/typed_buffers.dart';
65

76
import 'constants.dart';
7+
import 'platform/platform.dart';
88
import 'vfs.dart';
99
import 'utils.dart';
1010

@@ -32,7 +32,7 @@ final class InMemoryFileSystem extends BaseVirtualFileSystem {
3232

3333
@override
3434
String xFullPathName(String path) {
35-
return p.url.normalize('/$path');
35+
return pathToAbsoluteAndNormalize(path);
3636
}
3737

3838
@override

0 commit comments

Comments
 (0)