Proper rust init in MLComputer isolate

This commit is contained in:
laurenspriem
2025-08-11 17:24:36 +05:30
parent ad90d2e37a
commit dbf6f6aa37
2 changed files with 18 additions and 11 deletions

View File

@@ -29,8 +29,6 @@ class MLComputer extends SuperIsolate {
@override
bool get shouldAutomaticDispose => false;
bool _isRustInit = false;
// Singleton pattern
MLComputer._privateConstructor();
static final MLComputer instance = MLComputer._privateConstructor();
@@ -44,9 +42,7 @@ class MLComputer extends SuperIsolate {
final result = await runInIsolate(IsolateOperation.bulkVectorSearch, {
"clipFloat32": clipFloat32,
"exact": exact,
"initRust": !_isRustInit,
});
_isRustInit = true;
return result;
} catch (e, s) {
_logger.severe("Could not run bulk vector search in MLComputer", e, s);
@@ -161,9 +157,4 @@ class MLComputer extends SuperIsolate {
rethrow;
}
}
@override
Future<void> onDispose() async {
_isRustInit = false;
}
}

View File

@@ -63,8 +63,7 @@ Future<dynamic> isolateFunction(
) async {
switch (function) {
case IsolateOperation.bulkVectorSearch:
final initRust = args["initRust"] as bool? ?? false;
if (initRust) await RustLib.init();
await _ensureRustLoaded();
final clipFloat32 = args["clipFloat32"] as List<Float32List>;
final exact = args["exact"] as bool;
@@ -199,9 +198,26 @@ Future<dynamic> isolateFunction(
/// Caching
case IsolateOperation.clearAllIsolateCache:
_ensureRustDisposed();
_isolateCache.clear();
return true;
/// Cases for Caching stop here
}
}
Future<void> _ensureRustLoaded() async {
final bool loaded = _isolateCache["rustLibLoaded"] as bool? ?? false;
if (!loaded) {
await RustLib.init();
_isolateCache["rustLibLoaded"] = true;
}
}
void _ensureRustDisposed() {
final bool loaded = _isolateCache["rustLibLoaded"] as bool? ?? false;
if (loaded) {
RustLib.dispose();
_isolateCache.remove("rustLibLoaded");
}
}