Skip to content

armgit / plexo

Public Imported from GitHub
0

Speed up downloads by combining multiple network connections in parallel

Decouple download block size from display granularity

Anmol Kapil196ce9b32372
diff --git a/src/main/download/downloadManager.ts b/src/main/download/downloadManager.ts
index 4b7946b..87026e7 100644
--- a/src/main/download/downloadManager.ts
+++ b/src/main/download/downloadManager.ts
@@ -72,6 +72,15 @@ function retryDelayMs(attempt: number): number {
return Math.min(RETRY_BASE_DELAY_MS * 2 ** (attempt - 1), RETRY_MAX_DELAY_MS)
}
+/** Total live speed across a download's worker connections (bounded by MAX_CHUNKS, unlike blocks). */
+function sumChunkSpeeds(runtime: DownloadRuntime): number {
+ let total = 0
+ for (const chunk of runtime.state.chunks) {
+ total += chunk.speedBytesPerSec
+ }
+ return total
+}
+
/** Waits, but returns early if the signal aborts (pause/cancel shouldn't wait out a retry backoff). */
function delay(ms: number, signal: AbortSignal): Promise<void> {
return new Promise((resolve) => {
@@ -185,16 +194,23 @@ export class DownloadManager {
)
const canSplit = requestPayload.supportsRanges && requestPayload.totalBytes > 0
- const TARGET_BLOCKS = 64
- const MIN_BLOCK_BYTES = 4 * 1024 * 1024 // 4 MB minimum block size
+ // Block size stays fixed regardless of file size, so work granularity — and
+ // therefore resumability and load-balancing across workers — doesn't degrade
+ // on huge files. MAX_REAL_BLOCKS is only a safety valve for pathologically
+ // large files (multi-TB) so the block array doesn't blow up; it grows the
+ // block size instead of the count once a file is big enough to hit it.
+ // The UI caps how many cells it renders separately (see BlockGrid), by
+ // bucketing these blocks rather than by shrinking their count here.
+ const BASE_BLOCK_BYTES = 8 * 1024 * 1024 // 8 MB
+ const MAX_REAL_BLOCKS = 4096
let blockSizeBytes = 0
const blocks: BlockState[] = []
if (canSplit) {
blockSizeBytes = Math.max(
- MIN_BLOCK_BYTES,
- Math.ceil(requestPayload.totalBytes / TARGET_BLOCKS)
+ BASE_BLOCK_BYTES,
+ Math.ceil(requestPayload.totalBytes / MAX_REAL_BLOCKS)
)
let offset = 0
let bIdx = 0
@@ -601,7 +617,12 @@ export class DownloadManager {
}
chunk.speedBytesPerSec = pushSpeedSample(samples, chunk.bytesDownloaded, now)
- this.recomputeAggregates(runtime)
+ // This runs on every socket data event, so it folds the delta in rather than re-summing
+ // every block — that sum is O(blocks), and a large file has thousands of them. The other
+ // callers of recomputeAggregates are rare enough to afford the full pass, and each one
+ // re-derives the true total, so any drift here cannot accumulate.
+ runtime.state.bytesDownloaded += deltaBytes
+ runtime.state.speedBytesPerSec = sumChunkSpeeds(runtime)
this.scheduleUpdate(runtime)
}
@@ -610,10 +631,7 @@ export class DownloadManager {
(sum, entry) => sum + entry.bytesDownloaded,
0
)
- runtime.state.speedBytesPerSec = runtime.state.chunks.reduce(
- (sum, entry) => sum + entry.speedBytesPerSec,
- 0
- )
+ runtime.state.speedBytesPerSec = sumChunkSpeeds(runtime)
}
private scheduleUpdate(runtime: DownloadRuntime): void {
diff --git a/src/renderer/src/components/BlockGrid.tsx b/src/renderer/src/components/BlockGrid.tsx
index 4ca5fa9..ebf108d 100644
--- a/src/renderer/src/components/BlockGrid.tsx
+++ b/src/renderer/src/components/BlockGrid.tsx
@@ -1,7 +1,103 @@
-import type { BlockState } from '@shared/types'
+import type { BlockState, BlockStatus } from '@shared/types'
+import { useCallback, useRef, useState } from 'react'
import { FONT_MONO, type NetworkVisual } from '../theme'
import { formatBytes, type NetworkGroup } from '../utils/format'
+// The grid is a byte-space map of the file: cells run left-to-right, top-to-bottom, and each
+// is colored by the network that downloaded most of it.
+//
+// Vocabulary, kept consistent with the streams table: a *chunk* is one real 8 MB unit the
+// downloader actually fetches (what NetworkRow labels "Chunk #N"), and a *block* is one grid
+// square, which normalizes a fixed number of consecutive chunks into a single cell. The
+// legend states that ratio and every cell's tooltip names the exact chunk range it covers,
+// so the two views can be read against each other.
+//
+// How many blocks there are is itself information — more blocks means a bigger file — so the
+// count is derived from the file's size alone and never from the window's. Resizing only
+// changes how those blocks wrap into rows, the same way a paragraph rewraps without gaining
+// or losing words. Chunks are a fixed 8 MB, so their count is already proportional to file
+// size; grouping a fixed number of them per block preserves that proportionality.
+const CHUNKS_PER_BLOCK = 8 // ~64 MB of file per grid square
+const MIN_CELLS = 8
+const MAX_CELLS = 256
+
+const TARGET_CELL_PX = 12
+const CELL_GAP_PX = 3
+const CELL_HEIGHT_PX = 13
+const MIN_COLS = 8
+const MAX_ROWS = 6
+
+interface DisplayCell {
+ status: BlockStatus
+ interfaceId?: string
+ fillRatio: number
+ totalBytes: number
+ bytesDownloaded: number
+ /** 1-based, inclusive chunk numbers this block covers — matches the "Chunk #N" badges
+ * in the streams table, so a hovered block points back at a specific stream's work. */
+ firstChunk: number
+ lastChunk: number
+}
+
+/** Aggregates chunks into exactly `cellCount` contiguous blocks, sizes differing by at most one. */
+function bucketBlocks(blocks: BlockState[], cellCount: number): DisplayCell[] {
+ const cells: DisplayCell[] = []
+
+ for (let i = 0; i < cellCount; i++) {
+ const groupStart = Math.floor((i * blocks.length) / cellCount)
+ const groupEnd = Math.floor(((i + 1) * blocks.length) / cellCount)
+
+ let totalBytes = 0
+ let bytesDownloaded = 0
+ let hasError = false
+ let hasDownloading = false
+ let allCompleted = true
+ const bytesByInterface = new Map<string, number>()
+
+ for (let j = groupStart; j < groupEnd; j++) {
+ const block = blocks[j]
+ totalBytes += block.rangeEnd !== null ? block.rangeEnd - block.rangeStart + 1 : 0
+ bytesDownloaded += block.bytesDownloaded
+ if (block.status === 'error') hasError = true
+ if (block.status === 'downloading') hasDownloading = true
+ if (block.status !== 'completed') allCompleted = false
+ if (block.interfaceId) {
+ bytesByInterface.set(
+ block.interfaceId,
+ (bytesByInterface.get(block.interfaceId) || 0) + block.bytesDownloaded
+ )
+ }
+ }
+
+ let dominantInterfaceId: string | undefined
+ let dominantBytes = -1
+ for (const [interfaceId, bytes] of bytesByInterface) {
+ if (bytes > dominantBytes) {
+ dominantBytes = bytes
+ dominantInterfaceId = interfaceId
+ }
+ }
+
+ cells.push({
+ status: allCompleted
+ ? 'completed'
+ : hasError
+ ? 'error'
+ : hasDownloading
+ ? 'downloading'
+ : 'pending',
+ interfaceId: dominantInterfaceId,
+ fillRatio: totalBytes > 0 ? bytesDownloaded / totalBytes : 0,
+ totalBytes,
+ bytesDownloaded,
+ firstChunk: groupStart + 1,
+ lastChunk: groupEnd
+ })
+ }
+
+ return cells
+}
+
interface BlockGridProps {
blocks?: BlockState[]
groups: NetworkGroup[]
@@ -9,7 +105,6 @@ interface BlockGridProps {
knownSize: boolean
remainingBytes: number
isPaused?: boolean
- blockSizeBytes?: number
}
export function BlockGrid({
@@ -18,10 +113,24 @@ export function BlockGrid({
visuals,
knownSize,
remainingBytes,
- isPaused = false,
- blockSizeBytes
+ isPaused = false
}: BlockGridProps): React.JSX.Element {
- // If we have 2D blocks from range splitting, render the rich 2D Block Matrix
+ const [gridWidth, setGridWidth] = useState(0)
+ const [hoveredIndex, setHoveredIndex] = useState<number | null>(null)
+ const observerRef = useRef<ResizeObserver | null>(null)
+
+ // A callback ref rather than useEffect: the measured node only exists on the grid branch
+ // below, so this has to re-observe whenever that node mounts or unmounts.
+ const measureGrid = useCallback((node: HTMLDivElement | null) => {
+ observerRef.current?.disconnect()
+ if (!node) return
+ const observer = new ResizeObserver((entries) => {
+ setGridWidth(entries[0]?.contentRect.width ?? 0)
+ })
+ observer.observe(node)
+ observerRef.current = observer
+ }, [])
+
if (blocks && blocks.length > 1) {
const visualByInterfaceId = new Map<string, NetworkVisual>()
groups.forEach((g, idx) => {
@@ -30,12 +139,43 @@ export function BlockGrid({
}
})
- const effectiveBlockSize =
- blockSizeBytes ||
- (blocks[0]?.rangeEnd !== null ? blocks[0].rangeEnd - blocks[0].rangeStart + 1 : 0)
+ const cellCount = Math.min(
+ blocks.length,
+ Math.max(MIN_CELLS, Math.min(MAX_CELLS, Math.ceil(blocks.length / CHUNKS_PER_BLOCK)))
+ )
+ const fittedCols = Math.floor((gridWidth + CELL_GAP_PX) / (TARGET_CELL_PX + CELL_GAP_PX))
+ // A narrow window must not wrap into an unbounded stack of rows — past MAX_ROWS the cells
+ // shrink below their target width instead of adding another row.
+ const minColsForRowCap = Math.ceil(cellCount / MAX_ROWS)
+ const cols = Math.min(cellCount, Math.max(MIN_COLS, fittedCols, minColsForRowCap))
+ const cells = gridWidth > 0 ? bucketBlocks(blocks, cellCount) : []
+ const chunkBytes =
+ blocks[0].rangeEnd !== null ? blocks[0].rangeEnd - blocks[0].rangeStart + 1 : 0
+ const chunksPerBlock = cells.length > 0 ? Math.round(blocks.length / cells.length) : 0
- // 32 columns yields a clean 2-row grid for ~64 blocks
- const cols = blocks.length <= 32 ? blocks.length : Math.ceil(blocks.length / 2)
+ // Hovering reads out into the legend line rather than a native `title` tooltip: the grid
+ // re-renders on every progress push, which resets Chromium's tooltip timer so it never
+ // appears on an active block — and a tooltip advertises nothing to hover in the first place.
+ const hoveredCell = hoveredIndex !== null ? cells[hoveredIndex] : undefined
+ let readout: string
+ if (hoveredCell) {
+ const hoveredVisual = hoveredCell.interfaceId
+ ? visualByInterfaceId.get(hoveredCell.interfaceId)
+ : undefined
+ const range =
+ hoveredCell.firstChunk === hoveredCell.lastChunk
+ ? `Chunk #${hoveredCell.firstChunk}`
+ : `Chunks #${hoveredCell.firstChunk}–${hoveredCell.lastChunk}`
+ const where = hoveredVisual?.name ?? (hoveredCell.status === 'pending' ? 'queued' : '—')
+ readout = `${range} · ${formatBytes(hoveredCell.bytesDownloaded)} / ${formatBytes(hoveredCell.totalBytes)} · ${where}`
+ } else if (chunksPerBlock > 1) {
+ // Group sizes differ by one when the chunk count doesn't divide evenly, so don't state
+ // a ratio as exact when it is only the average.
+ const approx = blocks.length % cells.length === 0 ? '' : '~'
+ readout = `${cells.length} blocks · ${approx}${chunksPerBlock} × ${formatBytes(chunkBytes)} chunks`
+ } else {
+ readout = `${cells.length} blocks · ${formatBytes(chunkBytes)} each`
+ }
return (
<div
@@ -83,59 +223,104 @@ export function BlockGrid({
</div>
)
})}
+ {cells.length > 0 && chunkBytes > 0 && (
+ <div
+ style={{
+ marginLeft: 'auto',
+ font: `500 10px/1 ${FONT_MONO}`,
+ color: 'var(--text-tertiary)',
+ fontVariantNumeric: 'tabular-nums'
+ }}
+ title={
+ chunksPerBlock > 1
+ ? `This file downloads as ${blocks.length} chunks of ${formatBytes(chunkBytes)}. Each square groups ${chunksPerBlock} of them so the grid stays readable.`
+ : `This file downloads as ${blocks.length} chunks of ${formatBytes(chunkBytes)}, one per square.`
+ }
+ >
+ {readout}
+ </div>
+ )}
</div>
<div
+ ref={measureGrid}
+ onMouseLeave={() => setHoveredIndex(null)}
style={{
display: 'grid',
gridTemplateColumns: `repeat(${cols}, minmax(0, 1fr))`,
- gap: 3,
- width: '100%'
+ gap: CELL_GAP_PX,
+ width: '100%',
+ minHeight: CELL_HEIGHT_PX
}}
>
- {blocks.map((block) => {
- const visual = block.interfaceId
- ? visualByInterfaceId.get(block.interfaceId)
- : undefined
- const blockSize =
- block.rangeEnd !== null ? block.rangeEnd - block.rangeStart + 1 : effectiveBlockSize
-
- let bg = 'rgba(255, 255, 255, 0.05)'
- let border = '0.5px solid rgba(255, 255, 255, 0.08)'
+ {cells.map((cell, index) => {
+ const visual = cell.interfaceId ? visualByInterfaceId.get(cell.interfaceId) : undefined
+
+ // Base track uses theme-aware tokens (not hardcoded white-based rgba) so a
+ // mostly-pending bucket stays visible in light theme, not just dark.
+ let background = 'var(--track-bg)'
+ let border = '0.5px solid var(--border-strong)'
let boxShadow = 'none'
let opacity = 1
+ let fillColor = visual?.solid || 'var(--color-accent)'
- if (block.status === 'completed') {
- bg = visual?.solid || 'var(--color-wifi)'
- border = 'none'
- opacity = 0.92
- } else if (block.status === 'downloading') {
- bg = visual?.bg || 'var(--color-accent-bg)'
+ if (cell.status === 'downloading') {
+ background = visual?.bg || 'var(--color-accent-bg)'
border = `1px solid ${visual?.solid || 'var(--color-accent)'}`
boxShadow = isPaused ? 'none' : `0 0 7px ${visual?.solid || 'var(--color-accent)'}`
opacity = isPaused ? 0.6 : 1
- } else if (block.status === 'error') {
- bg = 'var(--color-danger)'
+ } else if (cell.status === 'error') {
+ fillColor = 'var(--color-danger)'
+ border = 'none'
+ } else if (cell.status === 'completed') {
border = 'none'
+ opacity = 0.92
}
- const networkName = visual?.name || (block.interfaceId ? 'Assigned' : 'Pending')
- const title = `Block #${block.index + 1} (${formatBytes(blockSize)}) · ${networkName} · ${block.status}`
+ const networkName = visual?.name || (cell.interfaceId ? 'Assigned' : 'Pending')
+ // Named by chunk range rather than cell index, so a hovered square maps onto the
+ // "Chunk #N" badges the streams table shows for each active connection.
+ const chunkLabel =
+ cell.firstChunk === cell.lastChunk
+ ? `Chunk #${cell.firstChunk}`
+ : `Chunks #${cell.firstChunk}–${cell.lastChunk}`
+ const title = `${chunkLabel} · ${formatBytes(cell.bytesDownloaded)} / ${formatBytes(cell.totalBytes)} · ${networkName} · ${cell.status}`
+ const rawFillPercent = Math.min(1, Math.max(0, cell.fillRatio)) * 100
+ // A cell aggregates several real blocks, so early progress within it can be a
+ // fraction of a percent — floor it to a visible sliver rather than 0 width.
+ const fillPercent = rawFillPercent > 0 ? Math.max(6, Math.round(rawFillPercent)) : 0
return (
<div
- key={block.index}
+ key={index}
title={title}
+ onMouseEnter={() => setHoveredIndex(index)}
style={{
- height: 13,
+ position: 'relative',
+ height: CELL_HEIGHT_PX,
borderRadius: 2.5,
- background: bg,
+ background,
border,
boxShadow,
opacity,
- transition: 'background 0.15s, opacity 0.15s, box-shadow 0.15s'
+ outline: hoveredIndex === index ? '1.5px solid var(--text-secondary)' : 'none',
+ outlineOffset: 1,
+ overflow: 'hidden',
+ transition: 'opacity 0.15s, box-shadow 0.15s'
}}
- />
+ >
+ {fillPercent > 0 && (
+ <div
+ style={{
+ position: 'absolute',
+ inset: 0,
+ width: `${fillPercent}%`,
+ background: fillColor,
+ transition: 'width 0.15s, background 0.15s'
+ }}
+ />
+ )}
+ </div>
)
})}
</div>
diff --git a/src/renderer/src/screens/DownloadingScreen.tsx b/src/renderer/src/screens/DownloadingScreen.tsx
index dcbac95..c919ebf 100644
--- a/src/renderer/src/screens/DownloadingScreen.tsx
+++ b/src/renderer/src/screens/DownloadingScreen.tsx
@@ -418,7 +418,6 @@ export function DownloadingScreen({ download }: { download: DownloadState }): Re
knownSize={knownSize}
remainingBytes={remainingBytes}
isPaused={isPaused}
- blockSizeBytes={download.blockSizeBytes}
/>
</div>