浏览代码

Perf: Remove redundant calls via computer (#1043)

Neeraj Gupta 2 年之前
父节点
当前提交
0d6a5fc435
共有 2 个文件被更改,包括 28 次插入27 次删除
  1. 19 16
      lib/services/local/local_sync_util.dart
  2. 9 11
      lib/services/local_sync_service.dart

+ 19 - 16
lib/services/local/local_sync_util.dart

@@ -16,7 +16,6 @@ const assetFetchPageSize = 2000;
 Future<Tuple2<List<LocalPathAsset>, List<File>>> getLocalPathAssetsAndFiles(
 Future<Tuple2<List<LocalPathAsset>, List<File>>> getLocalPathAssetsAndFiles(
   int fromTime,
   int fromTime,
   int toTime,
   int toTime,
-  Computer computer,
 ) async {
 ) async {
   final pathEntities = await _getGalleryList(
   final pathEntities = await _getGalleryList(
     updateFromTime: fromTime,
     updateFromTime: fromTime,
@@ -31,19 +30,24 @@ Future<Tuple2<List<LocalPathAsset>, List<File>>> getLocalPathAssetsAndFiles(
   final List<File> uniqueFiles = [];
   final List<File> uniqueFiles = [];
   for (AssetPathEntity pathEntity in pathEntities) {
   for (AssetPathEntity pathEntity in pathEntities) {
     final List<AssetEntity> assetsInPath = await _getAllAssetLists(pathEntity);
     final List<AssetEntity> assetsInPath = await _getAllAssetLists(pathEntity);
-    final Tuple2<Set<String>, List<File>> result = await computer.compute(
-      _getLocalIDsAndFilesFromAssets,
-      param: <String, dynamic>{
-        "pathEntity": pathEntity,
-        "fromTime": fromTime,
-        "alreadySeenLocalIDs": alreadySeenLocalIDs,
-        "assetList": assetsInPath,
-      },
-      taskName:
-          "getLocalPathAssetsAndFiles-${pathEntity.name}-count-${assetsInPath.length}",
-    );
-    alreadySeenLocalIDs.addAll(result.item1);
-    uniqueFiles.addAll(result.item2);
+    late Tuple2<Set<String>, List<File>> result;
+    if (assetsInPath.isEmpty) {
+      result = const Tuple2({}, []);
+    } else {
+      result = await Computer.shared().compute(
+        _getLocalIDsAndFilesFromAssets,
+        param: <String, dynamic>{
+          "pathEntity": pathEntity,
+          "fromTime": fromTime,
+          "alreadySeenLocalIDs": alreadySeenLocalIDs,
+          "assetList": assetsInPath,
+        },
+        taskName:
+            "getLocalPathAssetsAndFiles-${pathEntity.name}-count-${assetsInPath.length}",
+      );
+      alreadySeenLocalIDs.addAll(result.item1);
+      uniqueFiles.addAll(result.item2);
+    }
     localPathAssets.add(
     localPathAssets.add(
       LocalPathAsset(
       LocalPathAsset(
         localIDs: result.item1,
         localIDs: result.item1,
@@ -120,14 +124,13 @@ Future<LocalDiffResult> getDiffWithLocal(
   Set<String> existingIDs, // localIDs of files already imported in app
   Set<String> existingIDs, // localIDs of files already imported in app
   Map<String, Set<String>> pathToLocalIDs,
   Map<String, Set<String>> pathToLocalIDs,
   Set<String> invalidIDs,
   Set<String> invalidIDs,
-  Computer computer,
 ) async {
 ) async {
   final Map<String, dynamic> args = <String, dynamic>{};
   final Map<String, dynamic> args = <String, dynamic>{};
   args['assets'] = assets;
   args['assets'] = assets;
   args['existingIDs'] = existingIDs;
   args['existingIDs'] = existingIDs;
   args['invalidIDs'] = invalidIDs;
   args['invalidIDs'] = invalidIDs;
   args['pathToLocalIDs'] = pathToLocalIDs;
   args['pathToLocalIDs'] = pathToLocalIDs;
-  final LocalDiffResult diffResult = await computer.compute(
+  final LocalDiffResult diffResult = await Computer.shared().compute(
     _getLocalAssetsDiff,
     _getLocalAssetsDiff,
     param: args,
     param: args,
     taskName: "getLocalAssetsDiff",
     taskName: "getLocalAssetsDiff",

+ 9 - 11
lib/services/local_sync_service.dart

@@ -1,7 +1,6 @@
 import 'dart:async';
 import 'dart:async';
 import 'dart:io';
 import 'dart:io';
 
 
-import 'package:computer/computer.dart';
 import 'package:flutter/foundation.dart';
 import 'package:flutter/foundation.dart';
 import 'package:logging/logging.dart';
 import 'package:logging/logging.dart';
 import 'package:photo_manager/photo_manager.dart';
 import 'package:photo_manager/photo_manager.dart';
@@ -24,7 +23,6 @@ import 'package:tuple/tuple.dart';
 class LocalSyncService {
 class LocalSyncService {
   final _logger = Logger("LocalSyncService");
   final _logger = Logger("LocalSyncService");
   final _db = FilesDB.instance;
   final _db = FilesDB.instance;
-  final Computer _computer = Computer.shared();
   late SharedPreferences _prefs;
   late SharedPreferences _prefs;
   Completer<void>? _existingSync;
   Completer<void>? _existingSync;
 
 
@@ -194,7 +192,6 @@ class LocalSyncService {
       existingLocalFileIDs,
       existingLocalFileIDs,
       pathToLocalIDs,
       pathToLocalIDs,
       invalidIDs,
       invalidIDs,
-      _computer,
     );
     );
     bool hasAnyMappingChanged = false;
     bool hasAnyMappingChanged = false;
     if (localDiffResult.newPathToLocalIDs?.isNotEmpty ?? false) {
     if (localDiffResult.newPathToLocalIDs?.isNotEmpty ?? false) {
@@ -299,17 +296,18 @@ class LocalSyncService {
     required int toTime,
     required int toTime,
   }) async {
   }) async {
     final Tuple2<List<LocalPathAsset>, List<File>> result =
     final Tuple2<List<LocalPathAsset>, List<File>> result =
-        await getLocalPathAssetsAndFiles(fromTime, toTime, _computer);
-
-    // Update the mapping for device path_id to local file id. Also, keep track
-    // of newly discovered device paths
-    await FilesDB.instance.insertLocalAssets(
-      result.item1,
-      shouldAutoBackup: Configuration.instance.hasSelectedAllFoldersForBackup(),
-    );
+        await getLocalPathAssetsAndFiles(fromTime, toTime);
 
 
     final List<File> files = result.item2;
     final List<File> files = result.item2;
     if (files.isNotEmpty) {
     if (files.isNotEmpty) {
+      // Update the mapping for device path_id to local file id. Also, keep track
+      // of newly discovered device paths
+      await FilesDB.instance.insertLocalAssets(
+        result.item1,
+        shouldAutoBackup:
+            Configuration.instance.hasSelectedAllFoldersForBackup(),
+      );
+
       _logger.info(
       _logger.info(
         "Loaded ${files.length} photos from " +
         "Loaded ${files.length} photos from " +
             DateTime.fromMicrosecondsSinceEpoch(fromTime).toString() +
             DateTime.fromMicrosecondsSinceEpoch(fromTime).toString() +