Optimize timezone conversion speed in HomePhotos

This commit is contained in:
Ming Ming 2024-02-25 12:49:06 +08:00
parent 177d3c51fb
commit 22ebcebfce
2 changed files with 41 additions and 19 deletions

View file

@ -385,6 +385,7 @@ _ItemTransformerResult _buildItem(_ItemTransformerArgument arg) {
) )
: null; : null;
final tzOffset = clock.now().timeZoneOffset;
final transformed = <_Item>[]; final transformed = <_Item>[];
for (int i = 0; i < sortedFiles.length; ++i) { for (int i = 0; i < sortedFiles.length; ++i) {
final file = sortedFiles[i]; final file = sortedFiles[i];
@ -392,12 +393,13 @@ _ItemTransformerResult _buildItem(_ItemTransformerArgument arg) {
if (item == null) { if (item == null) {
continue; continue;
} }
final date = dateHelper?.onFile(file); final localDate = file.fdDateTime.add(tzOffset);
final date = dateHelper?.onFile(file, localDate: localDate);
if (date != null) { if (date != null) {
transformed.add(_DateItem(date: date, isMonthOnly: !arg.isGroupByDay)); transformed.add(_DateItem(date: date, isMonthOnly: !arg.isGroupByDay));
} }
transformed.add(item); transformed.add(item);
memoryCollectionHelper?.addFile(file); memoryCollectionHelper?.addFile(file, localDate: localDate);
} }
final memoryCollections = memoryCollectionHelper final memoryCollections = memoryCollectionHelper
?.build((year) => L10n.of(arg.locale).memoryAlbumName(today.year - year)); ?.build((year) => L10n.of(arg.locale).memoryAlbumName(today.year - year));

View file

@ -15,15 +15,20 @@ part 'photo_list_util.g.dart';
class DateGroupHelper { class DateGroupHelper {
DateGroupHelper({ DateGroupHelper({
required this.isMonthOnly, required this.isMonthOnly,
}); }) : _tzOffset = clock.now().timeZoneOffset;
DateTime? onFile(FileDescriptor file) { DateTime? onFile(
final newDate = file.fdDateTime.toLocal(); FileDescriptor file, {
if (newDate.year != _currentDate?.year || DateTime? localDate,
newDate.month != _currentDate?.month || }) {
(!isMonthOnly && newDate.day != _currentDate?.day)) { // toLocal is way too slow
_currentDate = newDate; // final localDate = file.fdDateTime.toLocal();
return newDate; localDate ??= file.fdDateTime.add(_tzOffset);
if (localDate.year != _currentDate?.year ||
localDate.month != _currentDate?.month ||
(!isMonthOnly && localDate.day != _currentDate?.day)) {
_currentDate = localDate;
return localDate;
} else { } else {
return null; return null;
} }
@ -31,6 +36,7 @@ class DateGroupHelper {
final bool isMonthOnly; final bool isMonthOnly;
DateTime? _currentDate; DateTime? _currentDate;
final Duration _tzOffset;
} }
/// Build memory collection from files /// Build memory collection from files
@ -42,20 +48,33 @@ class MemoryCollectionHelper {
this.account, { this.account, {
DateTime? today, DateTime? today,
required int dayRange, required int dayRange,
}) : today = (today?.toLocal() ?? clock.now()).toMidnight(), }) : _tzOffset = clock.now().timeZoneOffset,
dayRange = math.max(dayRange, 0); // today = (today?.toLocal() ?? clock.now()).toMidnight(),
dayRange = math.max(dayRange, 0) {
this.today = (today ?? clock.now()).toUtc().add(_tzOffset).toMidnight();
}
void addFile(FileDescriptor f) { void addFile(
final date = f.fdDateTime.toLocal().toMidnight(); FileDescriptor f, {
final diff = today.difference(date).inDays; DateTime? localDate,
}) {
// too slow
// final localDate = f.fdDateTime.toLocal().toMidnight();
localDate = (localDate ?? f.fdDateTime.add(_tzOffset)).toMidnight();
final diff = today.difference(localDate).inDays;
if (diff < 300) { if (diff < 300) {
return; return;
} }
for (final dy in [0, -1, 1]) { for (final dy in [0, -1, 1]) {
if (today.copyWith(year: date.year + dy).difference(date).abs().inDays <= if (today
.copyWith(year: localDate.year + dy)
.difference(localDate)
.abs()
.inDays <=
dayRange) { dayRange) {
_log.fine("[addFile] Add file (${f.fdDateTime}) to ${date.year + dy}"); _log.fine(
_addFileToYear(f, date.year + dy); "[addFile] Add file (${f.fdDateTime}) to ${localDate.year + dy}");
_addFileToYear(f, localDate.year + dy);
break; break;
} }
} }
@ -96,8 +115,9 @@ class MemoryCollectionHelper {
} }
final Account account; final Account account;
final DateTime today; late final DateTime today;
final int dayRange; final int dayRange;
final Duration _tzOffset;
final _data = <int, _MemoryCollectionHelperItem>{}; final _data = <int, _MemoryCollectionHelperItem>{};
} }