feat: enhance profile loading speed

This commit is contained in:
Kieran 2023-05-10 14:24:22 +01:00
parent 6de9c566e7
commit a56591996b
Signed by: Kieran
GPG Key ID: DE71CEB3925BE941
3 changed files with 19 additions and 6 deletions

View File

@ -15,6 +15,8 @@ export default abstract class FeedCache<TCached> {
#hooks: Array<HookFilter> = []; #hooks: Array<HookFilter> = [];
#snapshot: Readonly<Array<TCached>> = []; #snapshot: Readonly<Array<TCached>> = [];
#changed = true; #changed = true;
#hits = 0;
#miss = 0;
protected onTable: Set<string> = new Set(); protected onTable: Set<string> = new Set();
protected cache: Map<string, TCached> = new Map(); protected cache: Map<string, TCached> = new Map();
@ -23,7 +25,10 @@ export default abstract class FeedCache<TCached> {
this.#table = table; this.#table = table;
setInterval(() => { setInterval(() => {
console.debug( console.debug(
`[${this.#name}] ${this.cache.size} loaded, ${this.onTable.size} on-disk, ${this.#hooks.length} hooks` `[${this.#name}] ${this.cache.size} loaded, ${this.onTable.size} on-disk, ${this.#hooks.length} hooks, ${(
(this.#hits / (this.#hits + this.#miss)) *
100
).toFixed(1)} % hit`
); );
}, 5_000); }, 5_000);
} }
@ -56,7 +61,13 @@ export default abstract class FeedCache<TCached> {
getFromCache(key?: string) { getFromCache(key?: string) {
if (key) { if (key) {
return this.cache.get(key); const ret = this.cache.get(key);
if (ret) {
this.#hits++;
} else {
this.#miss++;
}
return ret;
} }
} }

View File

@ -17,8 +17,8 @@ export interface ProfilePreviewProps {
} }
export default function ProfilePreview(props: ProfilePreviewProps) { export default function ProfilePreview(props: ProfilePreviewProps) {
const pubkey = props.pubkey; const pubkey = props.pubkey;
const user = useUserProfile(pubkey);
const { ref, inView } = useInView({ triggerOnce: true }); const { ref, inView } = useInView({ triggerOnce: true });
const user = useUserProfile(inView ? pubkey : undefined);
const options = { const options = {
about: true, about: true,
...props.options, ...props.options,

View File

@ -19,11 +19,13 @@ class ProfileLoaderService {
* Request profile metadata for a set of pubkeys * Request profile metadata for a set of pubkeys
*/ */
TrackMetadata(pk: HexKey | Array<HexKey>) { TrackMetadata(pk: HexKey | Array<HexKey>) {
const bufferNow = [];
for (const p of Array.isArray(pk) ? pk : [pk]) { for (const p of Array.isArray(pk) ? pk : [pk]) {
if (p.length > 0) { if (p.length > 0 && this.WantsMetadata.add(p)) {
this.WantsMetadata.add(p); bufferNow.push(p);
} }
} }
UserCache.buffer(bufferNow);
} }
/** /**
@ -55,7 +57,7 @@ class ProfileLoaderService {
.filter(a => (UserCache.getFromCache(a)?.loaded ?? 0) < expire); .filter(a => (UserCache.getFromCache(a)?.loaded ?? 0) < expire);
const missing = new Set([...missingFromCache, ...expired]); const missing = new Set([...missingFromCache, ...expired]);
if (missing.size > 0) { if (missing.size > 0) {
console.debug(`Wants profiles: ${missingFromCache.length} missing, ${expired.length} expired`); console.debug(`[UserCache] Wants profiles: ${missingFromCache.length} missing, ${expired.length} expired`);
const sub = new RequestBuilder(`profiles`); const sub = new RequestBuilder(`profiles`);
sub sub