Skip to content
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

Unify dimensions cache between lightbox and feed #6047

Merged
merged 14 commits into from
Nov 4, 2024
Merged

Unify dimensions cache between lightbox and feed #6047

merged 14 commits into from
Nov 4, 2024

Conversation

gaearon
Copy link
Contributor

@gaearon gaearon commented Nov 1, 2024

We have two pieces of code doing essentially the same thing but with separate caches. (The "thing" is measuring image sizes and caching them.) I'm unifying them into a single cache:

  • Now it's always an LRU (previous feed cache grew unbounded for each item without known aspectRatio)
  • The hook forces to specify knownDimensions which should catch cases where we know them beforehand
  • Changes to src while rendering are now handled
  • Initial state is now reading from the cache if there's a hit
  • The hook returns undefined when the dimensions aren't known yet (instead of returning 1 for ratio)
  • The hook also now returns a tuple (aspect ratio first) where the ratio is calculated safely (for div 0).

In the final commit, I'm changing the lightbox code to use the dimensions plumbed down from the record when available. This means that for the vast majority of images, we won't trigger the "download and measure" codepath anymore.

Test Plan

Add setTimeout inside the hook before resolving and add knownDimensions = undefined to force loading.

Verify the feed jumps a bit as image dimensions get resolved (this is the worst case, e.g. when the records are done by clients that don't fill in aspectRatio) but that it works. Verify you can open the lightbox still.

Add some logs to cache hits, misses, and active request tracking. Verify they work as expected.

Check solo lightbox, gallery lightbox, and profile avi lightbox.

Verify hardcoding width: 0 or height: 0 in the resize callback doesn't crash the lightbox.

Remove the setTimeout and knownDimensions = undefined. Verify we mostly use knownDimensions except for records with missing aspectRatio like https://bsky.app/profile/unseenjapan.com/post/3l5mkbqrqq72y. Verify the same for lightbox.

@arcalinea arcalinea temporarily deployed to aspect-1 - social-app PR #6047 November 1, 2024 02:50 — with Render Destroyed
import {Image} from 'react-native'

import type {Dimensions} from '#/lib/media/types'

const sizes: Map<string, Dimensions> = new Map()
type CacheStorageItem<T> = {key: string; value: T}
const createCache = <T>(cacheSize: number) => ({
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is copypasta from deleted file

Copy link

github-actions bot commented Nov 1, 2024

Old size New size Diff
8.12 MB 8.12 MB 0 B (0.00%)

Copy link
Member

@mozzius mozzius left a comment

Choose a reason for hiding this comment

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

tested, looks good

@gaearon gaearon merged commit 174988b into main Nov 4, 2024
6 checks passed
@gaearon gaearon deleted the aspect-1 branch November 4, 2024 21:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants