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.
This commit is contained in:
Tunglies
2026-11-19 21:46:06 +08:00
parent e90f8c500f
commit f0e1460a04
2 changed files with 35 additions and 13 deletions
+1
View File
@@ -63,6 +63,7 @@
<details>
<summary><strong> 🚀 优化改进 </strong></summary>
- 优化首页流量图与代理、订阅、日志、设置页面的渲染效率,长列表滚动与实时刷新更流畅
- 优化连接、规则和日志筛选:切换页面后保留过滤条件
- 优化全局模式代理列表:仅显示 `GLOBAL` 节点,选中其中的策略组时才显示该组
- 优化设置页开关和选项所在行的鼠标悬停高亮,便于对应说明与控件
+34 -13
View File
@@ -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 {