Skip to content
This repository was archived by the owner on Oct 4, 2023. It is now read-only.

[C-2886] Improve cache performance - #3792

Merged
dylanjeffers merged 12 commits into
mainfrom
dj-cache-improvements
Jul 25, 2023
Merged

dylanjeffers merged 12 commits into
mainfrom
dj-cache-improvements

Conversation

@dylanjeffers

@dylanjeffers dylanjeffers commented Jul 24, 2023 •

Copy link
Copy Markdown
Contributor

Description

  • Removes add action and uses redux-thunk to check confirmer before calling addSucceeded action
  • Improves/fixes fast cache option, using a smaller/configurable TTL. This will help prevent thrashing with updating a bunch of values over and over. ADD_SUCCESSFUL action time goes way down with this fix over an average session.
  • Adds "simple" cache option to prevent a ton of rerenders due to cache subscriptions. Doesn't remove this entirely so we can potentially re-implement this. It would be helpful to have subscriptions which would could work in conjunction with TTL.

@dylanjeffers
dylanjeffers requested review from a team and amendelsohn and removed request for a team July 24, 2023 23:05

@amendelsohn amendelsohn left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Wow, very excited to see this in action!!
Excellent job 🚀

[IntKeys.GATED_TRACK_POLL_INTERVAL_MS]: 1000,
[IntKeys.DISCOVERY_NOTIFICATIONS_GENESIS_UNIX_TIMESTAMP]: 0
[IntKeys.DISCOVERY_NOTIFICATIONS_GENESIS_UNIX_TIMESTAMP]: 0,
[IntKeys.CACHE_ENTRY_TTL]: 0

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

is 0 a safe default? Would that invalidate everything immediately?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

what's potentially really silly, is that this makes it falsy, so the DEFAULT value in the code gets used. BUT now that you have me thinking, we should just get rid of the constant and use this.

Comment on lines +56 to +58
// If something is confirming and in the cache, we probably don't
// want to replace it (unless explicit) because we would lose client
// state, e.g. "has_current_user_reposted"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

will this prevent us from doing multiple local updates to the local cache, or is it ok because those would always go through update not add?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yeah exactly ^ (also this is migrated line-for-line by the original saga that did this)

entity.metadata,
mergeCustomizer
)
newMetadata = updateImageCache(existing, entity.metadata, newMetadata)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

safe to remove this?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

oh great find, whew, no not at all!

},
action: { ids: any[] }
) {
[REMOVE_SUCCEEDED](state: CacheState, action: { ids: any[] }) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

love this CacheState change!


dispatch(cacheActions.add(Kind.TRACKS, cacheTracks, false, true))
// // @ts-expect-error
// dispatch(cacheActions.add(Kind.TRACKS, cacheTracks, false, true))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can remove?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

oh great find as well, no


dispatch(cacheActions.add(Kind.TRACKS, cacheTracks, false, true))
// // @ts-expect-error
// dispatch(cacheActions.add(Kind.TRACKS, cacheTracks, false, true))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can remove?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

thanks, was testing something out when i removed them, bringing back

} from '@audius/common'
import { cacheActions, Kind, makeUid } from '@audius/common'
import { call, select, put } from 'typed-redux-saga'
import { call, put, select } from 'typed-redux-saga'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🔡 🎉

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

lmao

confirmTransaction,
RequestConfirmationError
RequestConfirmationError,
put

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I worry this will get confusing and people will forget. Do we only need it when doing cache adds? What happens when we forget?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yeah it's super confusing. basically folks should literally not need to use it, it was just that redux-saga types don't know how to deal with dispatch allowing a ThunkAction. so i think will only be needed for the few times we call put(add(...))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

hmm ok I guess that's fine. Wondering if there's a place we can put a comment so someone dealing with the type error won't waste time on it

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added a comment :)

Comment on lines -210 to -254
export function* add(kind, entries, replace, persist) {
// Get cached things that are confirming
const confirmCalls = yield select(getConfirmCalls)
const cache = yield select(getCache, { kind })
const confirmCallsInCache = pick(
cache.entries,
Object.keys(confirmCalls).map((kindId) => getIdFromKindId(kindId))
)

const entriesToAdd = []
const entriesToSubscribe = []
entries.forEach((entry) => {
// If something is confirming and in the cache, we probably don't
// want to replace it (unless explicit) because we would lose client
// state, e.g. "has_current_user_reposted"
if (!replace && entry.id in confirmCallsInCache) {
entriesToSubscribe.push({ uid: entry.uid, id: entry.id })
} else {
entriesToAdd.push(entry)
}
})
if (entriesToAdd.length > 0) {
yield put(
cacheActions.addSucceeded({
kind,
entries: entriesToAdd,
replace,
persist
})
)
}
if (entriesToSubscribe.length > 0) {
yield put(cacheActions.subscribe(kind, entriesToSubscribe))
}
}

// Adds entries but first checks if they are confirming.
// If they are, don't add or else we could be in an inconsistent state.
function* watchAdd() {
yield takeEvery(cacheActions.ADD, function* (action) {
const { kind, entries, replace, persist } = action
yield call(add, kind, entries, replace, persist)
})
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🥳

yield put(cacheActions.setCacheType({ cacheType: 'fast' }))
let cacheType = 'normal'

if (fastCache || (isNativeMobile && fastMobileCache)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

do we want to be able to enable it for web but not mobile? seems like we can either do both or just mobile this way

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

thanks for the callout here, i realized we dont need a separate "fast" and fast-mobile" now that desktop issues are fixed. this was to allow us to roll forward with the fast cache on mobile cause desktop was acting up for a while there. but we are consistent now, so will remove.

@audius-infra

Copy link
Copy Markdown
Collaborator

Preview this change https://demo.audius.co/dj-cache-improvements

@dylanjeffers
dylanjeffers merged commit 5af77ec into main Jul 25, 2023
@dylanjeffers
dylanjeffers deleted the dj-cache-improvements branch July 25, 2023 18:46
dylanjeffers added a commit that referenced this pull request Jul 25, 2023
audius-infra pushed a commit that referenced this pull request Jul 29, 2023
[5e99303] Add favorite test and fix aria-label (#3817) Raymond Jacobson
[ccc32ce] [C-2908 C-2744] fix desktop follow button (#3816) Dylan Jeffers
[c0679c3] [PAY-1660] Fix layout issues with TrackTile socials row with a lot of stats (#3815) Randy Schott
[089a9e6] Pin stripe package versions (#3813) Reed
[a281267] [C-2774] Update upload inputs (#3806) Dylan Jeffers
[f504ef9] [C-2901] Fix menu types (#3811) Dylan Jeffers
[cb9a385] [C-2905] Update Text types and props to camelCase (#3810) Kyle Shanks
[027b3a5] [PAY-1624] Implement Purchase modal (#3808) Randy Schott
[deadb5f] [C-2902] Update the upload forms to use the typography component (#3809) Kyle Shanks
[039c951] [C-801] Fix oauth nodes (#3807) Raymond Jacobson
[c3765c7] Update typography component to use classnames (#3805) Kyle Shanks
[cab0a3e] Switch to Stripe package instead of script (#3798) Reed
[a84126f] [C-2890] Add first version of a typography component to web (#3796) Kyle Shanks
[4addddc] Fix mobile prem-content drawer unlocking margin (#3804) Reed
[233b585] [C-2857] Remove get blocknumber (#3802) Dylan Jeffers
[d113bdb] Prepare for 1.5.34 full app release (#3801) Dylan Jeffers
[2f09db4] [C-2887] Fix collection button widths (#3800) Dylan Jeffers
[8158e10] [PAY-1655] Add ColorValue prop to Text component (#3799) Reed
[fe4bc6a] Revert cacheActions.add thunk (#3797) Dylan Jeffers
[2370bbe] [PAY-1650] Update play/preview buttons on track details to use HarmonyButton (#3795) Randy Schott
[3579dc2] [PAY-1651] Implements Harmony Buttons (#3794) Randy Schott
[5af77ec] [C-2886] Improve cache performance (#3792) Dylan Jeffers
[6fb78f1] [PAY-1587] Mobile USDC Purchase Drawer Skeleton (#3793) Reed
[1277a41] [C-2883] Migrate confirmer to common (#3788) Dylan Jeffers
[ce2548e] [plat-1111] add usdc purchase seller and buyer notifications (#3770) sabrina-kiam
[bc04f52] Fix mobile LockedStatusBadge padding (#3790) Reed
[8943078] [C-2680] Attribution Modal (#3778) Andrew Mendelsohn
@AudiusProject AudiusProject deleted a comment from linear Bot Sep 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants