Skip to content

perf(mobile): optimize date loading with batch loading #17240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

andreymir
Copy link
Contributor

Description

Introduce DateBatchLoader to reduce the number of database queries by loading dates in batches, improving performance when querying large lists.

My Immich library has 30k assets. On my Fairphone 5 Android phone Add Photos to Album screen takes ~20 seconds before showing anything. The bottleneck is in _buildRenderList in /mobile/lib/widgets/asset_grid/asset_grid_data_structure.dart.
For GroupAssetsBy.none case it queries dates from the database by 1 row for each section. With section size 60 assets and 30k assets in the library that makes 500 separate queries and takes a long time.

The optimization in this PR introduce DateBatchLoader to query dates in batches of 60000 rows (1000 sections).

How Has This Been Tested?

Tested on Android only (Fairphone 5).

Before

screen-20250330-144007-compressed.mp4

After

screen-20250330-185714-compressed.mp4

Checklist:

  • I have performed a self-review of my own code
  • I have no unrelated changes in the PR.
  • I have followed naming conventions/patterns in the surrounding code

Copy link
Contributor

@zackpollard zackpollard left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This LGTM but I will let someone that works on mobile actually merge this

Copy link
Member

@shenlong-tanwen shenlong-tanwen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you fix the failing checks?

Introduce DateBatchLoader to reduce the number of database queries by loading dates in batches, improving performance when querying large lists.
@andreymir andreymir force-pushed the optimize-asset-grid-performance branch from 72cd31d to 7a6512b Compare March 31, 2025 18:51
@andreymir
Copy link
Contributor Author

Can you fix the failing checks?

@shenlong-tanwen, thanks for checking the PR, fixed linter errors.

Copy link
Member

@shenlong-tanwen shenlong-tanwen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH, This is just loading 60K datetime objects, only to use a maximum of 1000 of them and discard the others. But this should fix the slowness for the time being until we get to rework the render grid.

@alextran1502
Copy link
Contributor

Thank you for the PR. During testing I see this error message poped up

I/flutter (30903): RangeError (start): Invalid value: Only valid value is 0: 255
I/flutter (30903): #0      RangeError.checkValidRange (dart:core/errors.dart:388)
I/flutter (30903): #1      ListExtensions.slice (package:collection/src/list_extensions.dart:242)
I/flutter (30903): #2      RenderList.loadAssets (package:immich_mobile/widgets/asset_grid/asset_grid_data_structure.dart:104)
I/flutter (30903): #3      _Section.build.<anonymous closure> (package:immich_mobile/widgets/asset_grid/immich_asset_grid_view.dart:667)
I/flutter (30903): #4      _LayoutBuilderElement._rebuildWithConstraints.updateChildCallback (package:flutter/src/widgets/layout_builder.dart:201)
I/flutter (30903): #5      BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:3056)
I/flutter (30903): #6      _LayoutBuilderElement._rebuildWithConstraints (package:flutter/src/widgets/layout_builder.dart:240)
I/flutter (30903): #7      RenderObject.invokeLayoutCallback.<anonymous closure> (package:flutter/src/rendering/object.dart:2827)
I/flutter (30903): #8      PipelineOwner._enableMutationsToDirtySubtrees (package:flutter/src/rendering/object.dart:1161)
I/flutter (30903): #9      RenderObject.invokeLayoutCallback (package:flutter/src/rendering/object.dart:2826)
I/flutter (30903): #10     RenderConstrainedLayoutBuilder.rebuildIfNecessary (package:flutter/src/widgets/layout_builder.dart:293)
I/flutter (30903): #11     _RenderLayoutBuilder.performLayout (package:flutter/src/widgets/layout_builder.dart:390)
I/flutter (30903): #12     RenderObject.layout (package:flutter/src/rendering/object.dart:2715)
I/flutter (30903): #13     RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:115)
I/flutter (30903): #14     RenderObject.layout (package:flutter/src/rendering/object.dart:2715)
I/flutter (30903): #15     RenderProxyBoxMixin.performLayout (package:flutter/src/rendering/proxy_box.dart:115)
I/flutter (30903): #16     RenderObject.layout (package:flutter/src/rendering/object.dart:2715)
I/flutter (30903): #17     RenderSliverList.performLayout (package:flutter/src/rendering/sliver_list.dart:213)
I/flutter (30903): #18     RenderObject.layout (package:flutter/src/rendering/object.dart:2715)
I/flutter (30903): #19     RenderSliverEdgeInsetsPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:133)
I/flutter (30903): #20     RenderSliverPadding.performLayout (package:flutter/src/rendering/sliver_padding.dart:371)
I/flutter (30903): #21     RenderObject.layout (package:flutter/src/rendering/object.dart:2715)
I/flutter (30903): #22     RenderViewportBase.layoutChildSequence (package:flutter/src/rendering/viewport.dart:608)
I/flutter (30903): #23     UnboundedRenderViewport._attemptLayout (package:scrollable_positioned_list/src/viewport.dart:263)
I/flutter (30903): #24     UnboundedRenderViewport.performLayout (package:scrollable_positioned_list/src/viewport.dart:182)
I/flutter (30903): #25     RenderObject._layoutWithoutResize (package:flutter/src/rendering/object.dart:2548)
I/flutter (30903): #26     PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:1112)
I/flutter (30903): #27     PipelineOwner.flushLayout (package:flutter/src/rendering/object.dart:1125)
I/flutter (30903): #28     RendererBinding.drawFrame (package:flutter/src/rendering/binding.dart:616)
I/flutter (30903): #29     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:1231)
I/flutter (30903): #30     RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:482)
I/flutter (30903): #31     SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1442)
I/flutter (30903): #32     SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1355)
I/flutter (30903): #33     SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:1208)
I/flutter (30903): #34     _invoke (dart:ui/hooks.dart:316)
I/flutter (30903): #35     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:428)
I/flutter (30903): #36     _drawFrame (dart:ui/hooks.dart:288)

Can you please help double check what caused this?

@andreymir
Copy link
Contributor Author

Thank you for the PR. During testing I see this error message poped up

I/flutter (30903): RangeError (start): Invalid value: Only valid value is 0: 255
I/flutter (30903): #0      RangeError.checkValidRange (dart:core/errors.dart:388)
I/flutter (30903): #1      ListExtensions.slice (package:collection/src/list_extensions.dart:242)
I/flutter (30903): #2      RenderList.loadAssets (package:immich_mobile/widgets/asset_grid/asset_grid_data_structure.dart:104)

Looking at the stacktrace I think the error is not related to the changes. It happens in loadAssets function at line 104:

return _buf.slice(offset - _bufOffset, offset - _bufOffset + count);

Seems like in some situations it tries to take a slice from an empty buffer. Do you have the steps to reproduce the error? I was not able to reproduce it.

@alextran1502
Copy link
Contributor

alextran1502 commented Apr 10, 2025

Do you have the steps to reproduce the error? I was not able to reproduce it.

I don't I was testing normal usage and triggered the error, it is quite sporadic so I don't have reproducible steps

@andreymir
Copy link
Contributor Author

Maybe the issue here because loadAssets is called with totalCount instead of count? In other places it pass count.

mobile/lib/widgets/asset_grid/immich_asset_grid_view.dart

  @override
  Widget build(
    BuildContext context,
  ) {
    return LayoutBuilder(
      builder: (context, constraints) {
        final width = constraints.maxWidth / assetsPerRow -
            margin * (assetsPerRow - 1) / assetsPerRow;
        final rows = (section.count + assetsPerRow - 1) ~/ assetsPerRow;
        final List<Asset> assetsToRender = scrolling
            ? []
            : renderList.loadAssets(section.offset, section.count);
        return Column(
          key: ValueKey(section.offset),
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            if (section.type == RenderAssetGridElementType.monthTitle)
              _MonthTitle(date: section.date),
            if (section.type == RenderAssetGridElementType.groupDividerTitle ||
                section.type == RenderAssetGridElementType.monthTitle)
              _Title(
                selectionActive: selectionActive,
                title: section.title!,
                assets: scrolling
                    ? []
                    : **renderList.loadAssets(section.offset, section.totalCount),**  // <-- should be **count** instead?
                allAssetsSelected: allAssetsSelected,
                selectAssets: selectAssets,
                deselectAssets: deselectAssets,
              ),
          ],
        );
      },
    );
  }
}

I could try to update it to count, but not sure how to verify that it fixes the problem since I could not see the error. Any advice?

@alextran1502 alextran1502 enabled auto-merge (squash) April 14, 2025 19:03
@alextran1502 alextran1502 merged commit bd92748 into immich-app:main Apr 14, 2025
39 checks passed
savely-krasovsky pushed a commit to savely-krasovsky/immich that referenced this pull request Jun 8, 2025
)

* perf(mobile): optimize date loading with batch loading

Introduce DateBatchLoader to reduce the number of database queries by loading dates in batches, improving performance when querying large lists.

* remove unused totalCount parameter from DateBatchLoader

---------

Co-authored-by: Alex <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants