惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
V
V2EX
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
云风的 BLOG
云风的 BLOG
Blog — PlanetScale
Blog — PlanetScale
Recent Announcements
Recent Announcements
Last Week in AI
Last Week in AI
博客园 - Franky
Microsoft Security Blog
Microsoft Security Blog
Hugging Face - Blog
Hugging Face - Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Vercel News
Vercel News
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
罗磊的独立博客
H
Help Net Security
月光博客
月光博客
Martin Fowler
Martin Fowler
博客园 - 【当耐特】
宝玉的分享
宝玉的分享
P
Proofpoint News Feed
GbyAI
GbyAI
腾讯CDC
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Combining Virtual Scroll With AI — Keeping 50,000 Log Lin...
hiyoyo · 2026-05-05 · via DEV Community
Cover image for Combining Virtual Scroll With AI — Keeping 50,000 Log Lines Fast While Adding Gemini

hiyoyo

If this is useful, a ❤️ helps others find it.

All tests run on an 8-year-old MacBook Air.

HiyokoLogcat renders 50,000+ log lines without freezing, and has a Gemini AI button on every error line.

These two features interact in non-obvious ways. Here's what I had to think through.


The core tension

Virtual scroll works by only rendering visible rows. Rows outside the viewport are unmounted from the DOM.

AI buttons live on rows. If a row is unmounted, its button state is gone too.

This means: if a user triggers a diagnosis, scrolls away, and scrolls back — the loading state is lost and the overlay might not reappear correctly.


Solution: lift AI state out of the row

Don't store diagnosis state inside the row component. Store it at the top level, keyed by line index.

// Top level — persists regardless of scroll position
const [diagnosisStates, setDiagnosisStates] = useState<
  Record
>({});
const [diagnosisResults, setDiagnosisResults] = useState<
  Record
>({});

// Pass down to each row
const handleDiagnose = async (idx: number) => {
  setDiagnosisStates(prev => ({ ...prev, [idx]: 'loading' }));

  try {
    const result = await invoke('diagnose', { idx });
    setDiagnosisResults(prev => ({ ...prev, [idx]: result }));
    setDiagnosisStates(prev => ({ ...prev, [idx]: 'done' }));
  } catch {
    setDiagnosisStates(prev => ({ ...prev, [idx]: 'error' }));
  }
};

When the row remounts after scrolling back, it reads its state from the top-level map. The diagnosis result is still there.


The ring buffer + virtual scroll interaction

The Rust backend keeps a ring buffer of 2,000 lines. As new logs arrive, old ones are evicted.

If line 847 triggered a diagnosis and then 2,000 new lines arrive, line 847 is gone from the buffer. The diagnosis result is still in the React state — but if the user tries to re-diagnose, the context is gone.

Handle this gracefully:

const handleRediagnose = async (idx: number) => {
  if (idx < bufferStartIdx) {
    // Line is no longer in the ring buffer
    showToast('このログ行はバッファから削除されました');
    return;
  }
  await handleDiagnose(idx);
};


Performance: don't re-render all rows on diagnosis

When diagnosis state updates, you don't want all 50,000 rows to re-render.

Use React.memo on the row component and pass only the relevant state slice:

const LogRow = React.memo(({
  line,
  diagnosisState,
  onDiagnose,
}: LogRowProps) => {
  // Only re-renders when its own diagnosisState changes
  return (


      {line.message}
      {line.level === 'E' && (

      )}


  );
});

With React.memo, a diagnosis on line 847 only re-renders row 847 — not the 49,999 others.


The library: react-virtuoso

I use react-virtuoso for virtual scroll. It handles the mount/unmount lifecycle cleanly and integrates well with dynamic item heights (log lines vary in length).

import { Virtuoso } from 'react-virtuoso';

 (
     handleDiagnose(idx)}
    />
  )}
/>


Hiyoko PDF Vault → https://hiyokoko.gumroad.com/l/HiyokoPDFVault
X → @hiyoyok