Perf: Remove redundant calls via computer (#1043)

This commit is contained in:
Neeraj Gupta 2023-04-29 14:09:13 +05:30 committed by GitHub
commit 0d6a5fc435
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 27 deletions

View file

@ -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( late Tuple2<Set<String>, List<File>> result;
_getLocalIDsAndFilesFromAssets, if (assetsInPath.isEmpty) {
param: <String, dynamic>{ result = const Tuple2({}, []);
"pathEntity": pathEntity, } else {
"fromTime": fromTime, result = await Computer.shared().compute(
"alreadySeenLocalIDs": alreadySeenLocalIDs, _getLocalIDsAndFilesFromAssets,
"assetList": assetsInPath, param: <String, dynamic>{
}, "pathEntity": pathEntity,
taskName: "fromTime": fromTime,
"getLocalPathAssetsAndFiles-${pathEntity.name}-count-${assetsInPath.length}", "alreadySeenLocalIDs": alreadySeenLocalIDs,
); "assetList": assetsInPath,
alreadySeenLocalIDs.addAll(result.item1); },
uniqueFiles.addAll(result.item2); 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",

View file

@ -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); await getLocalPathAssetsAndFiles(fromTime, toTime);
// 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(),
);
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() +