From f0e1460a04814590426499d79fe9c36cc9884ad6 Mon Sep 17 00:00:00 2001 From: Tunglies <77394545+Tunglies@users.noreply.github.com> Date: Thu, 19 Nov 2026 21:46:06 +0800 Subject: [PATCH] perf(home): build traffic range snapshots in one pass The 200ms graph snapshot built three to four intermediate arrays on the compressed path before posting to the worker. It now fills one preallocated result in a single pass and only sorts when the compressed and raw ranges actually interleave. --- Changelog.md | 1 + src/utils/traffic-sampler.ts | 47 ++++++++++++++++++++++++++---------- 2 files changed, 35 insertions(+), 13 deletions(-) diff --git a/Changelog.md b/Changelog.md index 51c123c80..e082aa8c6 100644 --- a/Changelog.md +++ b/Changelog.md @@ -63,6 +63,7 @@
🚀 优化改进 +- 优化首页流量图与代理、订阅、日志、设置页面的渲染效率,长列表滚动与实时刷新更流畅 - 优化连接、规则和日志筛选:切换页面后保留过滤条件 - 优化全局模式代理列表:仅显示 `GLOBAL` 节点,选中其中的策略组时才显示该组 - 优化设置页开关和选项所在行的鼠标悬停高亮,便于对应说明与控件 diff --git a/src/utils/traffic-sampler.ts b/src/utils/traffic-sampler.ts index 9fecbd19a..2be80a4cf 100644 --- a/src/utils/traffic-sampler.ts +++ b/src/utils/traffic-sampler.ts @@ -124,10 +124,10 @@ export class TrafficDataSampler { ) { rawStart++ } - const rawData = this.rawBuffer.slice(rawStart) + const rawCount = this.rawBuffer.length - rawStart if (minutes <= this.config.rawDataMinutes) { - return rawData + return this.rawBuffer.slice(rawStart) } const compressedCutoffUpper = @@ -140,19 +140,40 @@ export class TrafficDataSampler { ) { compressedStart++ } + let compressedEnd = compressedStart + while ( + compressedEnd < this.compressedBuffer.length && + this.compressedBuffer[compressedEnd].timestamp <= compressedCutoffUpper + ) { + compressedEnd++ + } - const compressedData = this.compressedBuffer - .slice(compressedStart) - .filter((p) => p.timestamp <= compressedCutoffUpper) - .map((p) => ({ - up: p.up, - down: p.down, - timestamp: p.timestamp, - })) + // Single preallocated result; raw entries keep their stored identity. + const compressedCount = compressedEnd - compressedStart + const result: ITrafficDataPoint[] = new Array(compressedCount + rawCount) + for (let i = 0; i < compressedCount; i++) { + const point = this.compressedBuffer[compressedStart + i] + result[i] = { + up: point.up, + down: point.down, + timestamp: point.timestamp, + } + } + for (let i = 0; i < rawCount; i++) { + result[compressedCount + i] = this.rawBuffer[rawStart + i] + } - return [...compressedData, ...rawData].sort( - (a, b) => a.timestamp - b.timestamp, - ) + // Compressed points precede raw ones except across the trim boundary; + // sort only when the two ranges actually interleave. + if ( + compressedCount > 0 && + rawCount > 0 && + this.compressedBuffer[compressedEnd - 1].timestamp > + this.rawBuffer[rawStart].timestamp + ) { + result.sort((a, b) => a.timestamp - b.timestamp) + } + return result } getStats(): ISamplerStats {