π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[mobile][photos] optimize getClustersForMemoryLane fn using batching
Priyanshu Dangaree206e0b2751b
diff --git a/mobile/apps/photos/lib/db/ml/db.dart b/mobile/apps/photos/lib/db/ml/db.dartindex b70fe1333d..3f6eb42af8 100644--- a/mobile/apps/photos/lib/db/ml/db.dart+++ b/mobile/apps/photos/lib/db/ml/db.dart@@ -22,6 +22,7 @@ import "package:photos/main.dart" show isProcessBg;import "package:photos/models/ml/clip.dart";import "package:photos/models/ml/face/face.dart";import "package:photos/models/ml/face/face_with_embedding.dart";+import "package:photos/models/ml/face/person.dart";import "package:photos/models/ml/ml_versions.dart";import "package:photos/models/ml/vector.dart";import "package:photos/service_locator.dart";@@ -2784,23 +2785,45 @@ class MLDataDB with SqlDbBase implements IMLDataDB<int> {await db.execute(sql, params);}- Future<Set<String>> getClustersForMemoryLane() async {+ Future<Set<String>> getClustersForMemoryLane(+ List<PersonEntity> persons,+ ) async {+ const batchSize = 256;+ final assigned = <String>{};+ for (final person in persons) {+ for (final cluster in person.data.assigned) {+ assigned.add(cluster.id);+ }+ }+final db = await asyncDB;+ final clusters = <String>{};+ var offset = 0;const String sql ='''- SELECT fc.$clusterIDColumn, COUNT(*) AS count- FROM $faceClustersTable fc- WHERE fc.$clusterIDColumn IS NOT NULL- AND NOT EXISTS (- SELECT 1- FROM $clusterPersonTable cp- WHERE cp.$clusterIDColumn = fc.$clusterIDColumn- )- GROUP BY fc.$clusterIDColumn- ORDER BY count DESC- LIMIT 20+ SELECT $clusterIDColumn, COUNT(*) AS count+ FROM $faceClustersTable+ WHERE $clusterIDColumn IS NOT NULL+ GROUP BY $clusterIDColumn+ ORDER BY count DESC, $clusterIDColumn+ LIMIT ? OFFSET ?''';- final result = await db.getAll(sql);- return result.map((row) => row[clusterIDColumn] as String).toSet();+ while (clusters.length < 20) {+ final batch = await db.getAll(sql, [batchSize, offset]);+ for (final row in batch) {+ final cluster = row[clusterIDColumn] as String;+ if (!assigned.contains(cluster)) {+ clusters.add(cluster);+ if (clusters.length == 20) {+ break;+ }+ }+ }+ if (batch.length < batchSize) {+ break;+ }+ offset += batchSize;+ }+ return clusters;}}diff --git a/mobile/apps/photos/lib/services/memory_lane/memory_lane_service.dart b/mobile/apps/photos/lib/services/memory_lane/memory_lane_service.dartindex 3aed5c5c18..62bca064ce 100644--- a/mobile/apps/photos/lib/services/memory_lane/memory_lane_service.dart+++ b/mobile/apps/photos/lib/services/memory_lane/memory_lane_service.dart@@ -112,7 +112,7 @@ class MemoryLaneService {schedulePersonRecompute(person.remoteID, force: force);}if (flagService.internalUser) {- final clusters = await _mlDataDB.getClustersForMemoryLane();+ final clusters = await _mlDataDB.getClustersForMemoryLane(persons);for (final cluster in clusters) {schedulePersonRecompute(cluster, isCluster: true, force: force);}