perf(stream-stats): throttle the stats tick when the tab is hidden - #998
Open
Endymi0n74 wants to merge 1 commit into
Open
perf(stream-stats): throttle the stats tick when the tab is hidden#998Endymi0n74 wants to merge 1 commit into
Endymi0n74 wants to merge 1 commit into
Conversation
When the document is hidden the stats bar kept polling getStats() every second for no visible benefit. The tick now switches to INTERVAL_BACKGROUND (60 s) via a self-rearming setTimeout — the interval is re-evaluated on every tick (document.hidden), so stats refresh immediately when the tab becomes visible again (visibilitychange listener triggers one update right away). The update() is also guarded against re-entrancy (isUpdating flag with try/finally), which matters now that collect() is async: a slow getStats() call can no longer overlap the next tick. Single file, no behaviour change while the tab is visible (still 1 s). 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Quoi
La barre de stats (StreamStats) appelle
getStats()toutes les secondes même quand l'onglet est caché — inutile (rien de visible à rafraîchir) mais le coût CPU/batterie reste là. Trois changements dans un seul fichier (stream-stats.ts) :document.hidden: le tick passe deREFRESH_INTERVAL(1 s, onglet visible) àStreamStatsCollector.INTERVAL_BACKGROUND(60 s, onglet caché) viagetInterval().setInterval→setTimeoutauto-réarmé : l'intervalle est réévalué à chaque tick (le réarmement se fait en fin d'update, pas une fois austart) → au retour au premier plan, la cadence 1 s reprend immédiatement.visibilitychangedéclenche une mise à jour dès que l'onglet redevient visible (pas d'attente du prochain tick).Au passage,
update()est protégé contre la réentrance (isUpdatingflag + try/finally) :collect()est async, ungetStats()lent ne peut plus chevaucher le tick suivant.Pourquoi c'est sûr
isUpdatingne change que le timing de la boucle —collect()/getStat()/rendu intacts.INTERVAL_BACKGROUND = 60 * 1000existait déjà dansStreamStatsCollector(inutilisée) — le patch la branche, zéro nouvelle valeur magique.visibilitychangeest ajouté dans le constructeur, aucun retrait nécessaire (la classe est un singleton de session).Détails
src/modules/stream/stream-stats.ts(+45/−21)bun build.ts --version 6.7.12 --variant full→ exit 0 (gate eslint + TS)getStats()complet par seconde ; passé à 1/60 s quand l'onglet est caché — le gain dépend du temps decollect()(report RTC de ~50 entrées ≈ µs), mais supprime 59/60 des appelsgetStats()sur un stream en arrière-plan (lecture prolongée + autre onglet actif, scénario courant).