diff --git a/mobile/apps/photos/lib/services/machine_learning/ml_computer.dart b/mobile/apps/photos/lib/services/machine_learning/ml_computer.dart index 4856134fdd..d89b5d5a4a 100644 --- a/mobile/apps/photos/lib/services/machine_learning/ml_computer.dart +++ b/mobile/apps/photos/lib/services/machine_learning/ml_computer.dart @@ -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 onDispose() async { - _isRustInit = false; - } } diff --git a/mobile/apps/photos/lib/utils/isolate/isolate_operations.dart b/mobile/apps/photos/lib/utils/isolate/isolate_operations.dart index c82143f55e..7ace292438 100644 --- a/mobile/apps/photos/lib/utils/isolate/isolate_operations.dart +++ b/mobile/apps/photos/lib/utils/isolate/isolate_operations.dart @@ -63,8 +63,7 @@ Future 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; final exact = args["exact"] as bool; @@ -199,9 +198,26 @@ Future isolateFunction( /// Caching case IsolateOperation.clearAllIsolateCache: + _ensureRustDisposed(); _isolateCache.clear(); return true; /// Cases for Caching stop here } } + +Future _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"); + } +}