aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/messages.js
diff options
context:
space:
mode:
authorAdam Soltys <[email protected]>2026-10-11 19:51:45 -0700
committerAdam Soltys <[email protected]>2026-10-11 19:51:45 -0700
commitfdaf7137139540d2f8de50b2a42d6ff3c036328b (patch)
tree28577cec63e2ced3b2cdde83288e1b234bc2dde3 /src/features/messages.js
parent491904e04fe3d69aa6b417c266aa8b550ffa0534 (diff)
downloadcoinosv3-fdaf7137139540d2f8de50b2a42d6ff3c036328b.tar.gz
coinosv3-fdaf7137139540d2f8de50b2a42d6ff3c036328b.zip
Feed: a wheel scroll through a loading feed no longer stutters on a laptop. The phone was fine (its scroll is the compositor's) but the desktop's wheel coast is driven from the main thread, so every long task froze it, and a feed that was still loading ran dozens of them: each ready post went in through its own admitFeed → holdScroll → full render (200–450 ms apiece; a Popular switch did 49 in a few seconds, one of them 1.3 s). Inside those renders the window's list getter — run on every scroll frame too — filtered every note through the mute/spam/report checks, and each check re-derived the login identity (two parses of the stored state plus a bech32 encode): 11 s of a 2-minute profile. Now ready posts are admitted together, one render per 120 ms, and while the reader is moving the page the batch waits for a quiet moment (up to 1.5 s); nostrLoginIdentity keeps its answer for 250 ms (dropped by its own saves, a signer change or another wallet's key), npubOf is memoized, modOn reads the cached messages state, the foreign-language guess is remembered per post, and the window list is reused between renders. Measured on Adam's dev tab (tools/feed-scroll-jank.js: real CDP wheel notches, chip clicks mid-load, rAF frame gaps, long tasks, a CPU profile that names each busy stretch): stalled time during a switch 1.9–2.5 s → 0–83 ms, no frame over 50 ms on the wheel path, main thread busy 33% → 17%; settled scrolling was already clean. feed-streaming's "tap reveals the oldest ready new post" fails on the previous commit too.
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Diffstat (limited to 'src/features/messages.js')
-rw-r--r--src/features/messages.js58
1 files changed, 53 insertions, 5 deletions
diff --git a/src/features/messages.js b/src/features/messages.js
index 94a5173..278e66f 100644
--- a/src/features/messages.js
+++ b/src/features/messages.js
@@ -4193,11 +4193,19 @@ export function messagesFeature(ctx) {
// language); unknown is never translated unasked
// A post that is only hashtags is judged by them: its tags in a script
// the reader can't read are what wants translating (as glosses).
+ // Asked per row per render; the guess reads the whole text, and a post's
+ // text and the target language don't change, so remember it.
+ const foreignMemo = new Map();
const looksForeign = (ev) => {
const to = transLang();
if (!translatable(ev) || !(deeplKey() || canTranslateTo(to))) return false;
+ const mk = transKey(ev) + '|' + to;
+ if (foreignMemo.has(mk)) return foreignMemo.get(mk);
const text = noteText(ev), { prose, tags } = transParts(text, to);
- return prose.length ? !inLanguage(text, to) : tags.length > 0;
+ const v = prose.length ? !inLanguage(text, to) : tags.length > 0;
+ if (foreignMemo.size > 3000) foreignMemo.clear();
+ foreignMemo.set(mk, v);
+ return v;
};
const langName = (code) => { try { return new Intl.DisplayNames([getLang()], { type: 'language' }).of(code) || code; } catch { return code; } };
// the translation to SHOW for this post now, or null
@@ -4715,8 +4723,14 @@ export function messagesFeature(ctx) {
// (a /username deep link over the unlock screen) scrolls too, and init
// only runs once a wallet opens.
let lastFeedY = 0, pillFrame = 0;
+ // When the reader last moved the page themselves (a wheel notch coasts for
+ // a while after the last one), and when it last scrolled at all: a batch of
+ // arriving posts waits for a quiet moment (see queueAdmit).
+ let lastInputAt = 0, lastScrollAt = 0;
if (typeof window !== 'undefined') {
+ for (const n of ['wheel', 'touchstart', 'touchmove', 'keydown', 'pointerdown']) window.addEventListener(n, () => { lastInputAt = Date.now(); }, { passive: true, capture: true });
window.addEventListener('scroll', () => {
+ lastScrollAt = Date.now();
if (!ui.profilePk && (!(ui.chatOpen && ui.msgView === 'feed') || ui.noteThread)) return;
windowScrolled(ui.profilePk ? notesCache.get(ui.profilePk) : feed);
// back at the top by yourself: the new posts are under your eyes, so
@@ -6707,12 +6721,37 @@ export function messagesFeature(ctx) {
// Cached rows can still be warming. This new row is already prepared
// and can take its stable presentation without waiting for that page.
if (c.booting) { c.prepared ||= new Set(); c.prepared.add(e.id); }
- admitFeed([...(c.deferred || []), e], c, false, opts.newerThan);
- scheduleRepaint();
+ queueAdmit(e, c, opts.newerThan);
return true;
}));
return ready.some(Boolean);
}
+ // Ready posts go in together: one admission — one render with the reader's
+ // row held still — per short interval, not one per post. A Popular switch
+ // used to run dozens of full renders in a few seconds, 200–450 ms each,
+ // and a wheel scroll through them stuttered on a laptop (the wheel coast
+ // is driven from the main thread, so every long render froze it). While
+ // the reader is moving the page the batch waits for a quiet moment, up to
+ // a cap, so nothing much changes under a coasting scroll.
+ const ADMIT_MS = 120, ADMIT_QUIET_MS = 160, ADMIT_INPUT_MS = 2500, ADMIT_MAX_WAIT_MS = 1500;
+ const admitQueues = new Map(); // c → { evs, newerThan, since, timer }
+ function queueAdmit(e, c, newerThan) {
+ let q = admitQueues.get(c);
+ if (!q) admitQueues.set(c, q = { evs: [], newerThan, since: Date.now(), timer: 0 });
+ q.evs.push(e);
+ if (!q.timer) q.timer = setTimeout(() => flushAdmit(c), ADMIT_MS);
+ }
+ const readerMoving = () => Date.now() - lastScrollAt < ADMIT_QUIET_MS && Date.now() - lastInputAt < ADMIT_INPUT_MS;
+ function flushAdmit(c) {
+ const q = admitQueues.get(c);
+ if (!q) return;
+ q.timer = 0;
+ if (c === feed && readerMoving() && Date.now() - q.since < ADMIT_MAX_WAIT_MS) { q.timer = setTimeout(() => flushAdmit(c), ADMIT_QUIET_MS); return; }
+ admitQueues.delete(c);
+ if (c.stopped) return;
+ admitFeed([...(c.deferred || []), ...q.evs], c, false, q.newerThan);
+ scheduleRepaint();
+ }
// Never insert between the rows currently under the reader's eyes. Gap
// fills wait until that part of the feed is offscreen; new posts above
@@ -8706,7 +8745,10 @@ export function messagesFeature(ctx) {
// rooms, DMs, quote cards. Two rules are Amethyst's, and are what makes
// its feeds feel clean: repeated content from strangers is spam, and a
// post your follows reported is folded (not hidden) behind who said so.
- const modOn = (k) => (st().mod || {})[k] !== false;
+ // Per row per check; the cached copy serves (a Settings toggle writes
+ // through save(), which refreshes it) rather than re-parsing the blob
+ // every 50 ms through a long render.
+ const modOn = (k) => ((stCache || st()).mod || {})[k] !== false;
// ---- sensitive posts (NIP-36 content warnings, NSFW accounts) -------------
// A post its author marked sensitive carries a content-warning tag — what a
// Misskey artist's "sensitive" media becomes on the mostr bridge, and what
@@ -11863,7 +11905,13 @@ export function messagesFeature(ctx) {
// reconciles the list by post: a new one at the top is inserted as its
// own node; the rest keep theirs.
const list = visible.slice(0, c.shown || FEED_PAGE);
- c.winList = () => c.notes.filter(shownHere).slice(0, c.shown || FEED_PAGE);
+ // The window's list getter runs on every scroll frame and inside
+ // holdScroll; filtering every note through the mute/spam/report checks
+ // each time was the main thread's single largest cost while a feed
+ // loaded. Anything that changes who is shown goes through render(), which
+ // lands here again; between renders the same notes give the same list.
+ const visNotes = c.notes, visShown = c.shown;
+ c.winList = () => (c.notes === visNotes && c.shown === visShown ? list : c.notes.filter(shownHere).slice(0, c.shown || FEED_PAGE));
const rows = windowedRows(c, list, (ev) => (c.booting && !c.prepared?.has(ev.id)
? keyed(noteRow(ev.pubkey, ev, displayName(ev.pubkey)), ev.id) : feedRow(c, ev)));
// Posts that went in above you while you were reading. A floating pill