SPB Git

Repository Public

CHART_PERFORMANCE.md 18.8 KB

# Chart renderer — performance measurement and the Metal decision (2.0.0)

Decision: Core Graphics (SwiftUI Canvas) is kept; Metal is not adopted. The measurement showed that the 1.0.1 renderer missed the 120 Hz budget on long series, but the time was spent in the app's own per-frame work (building paths with one point per source bar for every indicator, one calendar lookup per visible bar for the time axis), not in rasterisation. Bounding that work by the window width — which is what a GPU renderer would also have required — removed the problem: the same 50 000-bar chart with every indicator went from a median of 165 ms of main-thread work per frame to 4.8 ms. What remains above budget is stated below.

# Method

A DEBUG-only harness in the app (Features/Charting/ChartBench.swift, compiled out of Release) runs the real ChartCanvas in a real on-screen window:

text
VQ_DERIVED=… Scripts/run-dev.sh -- -VQSection charting -VQXChartNoSync 1 -VQXChartBench 10000,20000,50000,100000 \
    -VQXChartBenchOut "$HOME/Library/Containers/ai.spboucher.vquant.debug/Data/tmp/chart-bench.json"
  • Series: a seeded geometric random walk with intrabar extremes (deterministic), used for the benchmark only and never shown as data. 10 000, 20 000, 50 000 and 100 000 bars. The server's own limit is 50 000 bars per request.
  • Scenarios, each driven by a CADisplayLink for 240 consecutive frames after 10 warm-up frames, every frame changing the visible window so the whole chart is redrawn: zoom at full extent (all bars visible, alternating between 100 % and 98 % of the series — the worst case: a full redraw of everything), pan with 2 000 bars visible, pan with 200 bars visible (every candle drawn individually).
  • With and without indicators (all of SMA 20/50/200, EMA 20, Bollinger, volume, RSI, MACD against none), and with 0 or 50 drawings (all six kinds, spread over the series; drawings did not exist in 1.0.1).
  • Sizes: 1000 × 640 pt (the plot of a default main window) and 1728 × 1000 pt. The built-in panel of the test machine is 1312 × 848 pt at the scaling in use, so the second window extends past the screen edge: its main-thread cost is measured in full, but the window server may not composite the hidden part. A true 5K surface is covered by the off-screen measure below (2560 × 1440 pt at 2x = 5120 × 2880 px).
  • What is recorded per frame: main = time the main run loop was busy between two display-link ticks (SwiftUI update + the Canvas draw closure + the Core Animation commit) — the number that decides whether a frame is missed; closure = time inside ChartCanvas.draw alone; delivered tick interval (a frame is counted missed when the interval exceeds 1.5 display periods); process CPU time per frame (all threads). Median / p95 / worst, in ms.
  • Off-screen full redraw: ImageRenderer → CGImage at 2x, 10 timed renders after 2 warm-ups: the CPU rasterisation cost of one complete frame, also the cost of a PNG export.
  • Memory: phys_footprint of the process before loading a series, after loading it and after its scenarios.

Limits of the method, stated plainly: the build is Debug (-Onone), so absolute numbers are pessimistic for a Release build (not measured: the harness does not exist in Release); GPU time in the render server is not measured directly, only through missed frames; other engineers' builds and UI tests were running on the same Mac during both runs, which explains isolated worst-case spikes (single frames of 30–170 ms in otherwise flat series) in both tables; one run each, no averaging across runs.

# Machine

  • Apple M5 Max, 48 GB, macOS Version 27.0 (Build 26A5425a)
  • Display: 1312×848 pt @2.0x, up to 120 Hz; the display link ran at 120 Hz (budget 8.33 ms)
  • Before: Core Graphics (SwiftUI Canvas), 1.0.1 renderer (commit e5201a3 + the timing hooks). After: Core Graphics (SwiftUI Canvas), 2.0 renderer (aligned reduction cached per zoom level).

# Results — main-thread work per frame (ms, median / p95 / worst) and missed frames out of 240

# 1000 × 640 pt

Bars Scenario Indicators 1.0.1 renderer missed 2.0 renderer missed 2.0 with 50 drawings missed
10000 zoom at full extent none 6.1 / 6.5 / 7.0 0 2.0 / 2.9 / 6.7 0 4.5 / 6.9 / 8.4 0
10000 pan, 2 000 bars visible none 3.0 / 3.2 / 3.5 0 3.1 / 4.6 / 7.3 0 4.4 / 7.5 / 9.5 0
10000 pan, 200 bars visible none 2.1 / 2.2 / 2.7 0 2.4 / 3.7 / 4.7 0 4.7 / 7.2 / 8.1 0
10000 zoom at full extent all 39.4 / 50.4 / 165.9 240 5.0 / 6.3 / 7.4 0 5.5 / 6.1 / 7.3 0
10000 pan, 2 000 bars visible all 12.0 / 13.1 / 28.8 109 5.1 / 6.1 / 7.7 0 5.2 / 5.8 / 10.3 0
10000 pan, 200 bars visible all 3.5 / 4.1 / 4.5 0 3.9 / 5.8 / 8.9 0 4.7 / 6.5 / 14.0 0
20000 zoom at full extent none 10.6 / 11.2 / 12.5 73 2.9 / 4.6 / 5.1 0 4.7 / 6.4 / 7.9 0
20000 pan, 2 000 bars visible none 3.0 / 3.4 / 4.1 0 2.8 / 4.4 / 5.8 0 4.8 / 6.8 / 9.3 0
20000 pan, 200 bars visible none 2.0 / 2.5 / 2.9 0 2.0 / 2.8 / 4.2 0 4.6 / 7.2 / 8.4 0
20000 zoom at full extent all 74.6 / 99.8 / 116.6 240 4.9 / 6.7 / 8.6 0 5.2 / 5.6 / 6.4 0
20000 pan, 2 000 bars visible all 8.9 / 13.0 / 108.7 36 4.9 / 5.8 / 6.9 0 4.8 / 5.3 / 7.6 0
20000 pan, 200 bars visible all 4.7 / 6.0 / 7.6 0 3.8 / 6.2 / 7.6 0 4.7 / 5.8 / 7.6 0
50000 zoom at full extent none 16.9 / 19.8 / 22.8 212 2.6 / 3.4 / 3.9 0 4.9 / 6.3 / 8.1 0
50000 pan, 2 000 bars visible none 3.0 / 3.5 / 8.1 0 2.3 / 3.0 / 3.8 0 4.6 / 6.8 / 7.5 0
50000 pan, 200 bars visible none 2.4 / 3.6 / 4.7 0 1.8 / 2.4 / 6.3 0 4.1 / 7.5 / 8.4 0
50000 zoom at full extent all 164.7 / 181.5 / 360.9 240 4.8 / 6.2 / 9.8 0 6.7 / 7.4 / 8.9 0
50000 pan, 2 000 bars visible all 8.9 / 10.7 / 11.7 26 3.8 / 6.6 / 8.1 0 6.1 / 6.7 / 8.2 0
50000 pan, 200 bars visible all 3.4 / 4.3 / 5.3 0 2.6 / 4.0 / 7.1 0 5.0 / 5.5 / 5.9 0
100000 zoom at full extent none 29.8 / 34.0 / 99.3 240 2.6 / 3.0 / 4.9 0 5.1 / 7.7 / 11.4 0
100000 pan, 2 000 bars visible none 2.8 / 4.2 / 5.0 0 2.8 / 4.0 / 7.4 0 4.7 / 5.9 / 7.4 0
100000 pan, 200 bars visible none 1.9 / 3.1 / 4.1 0 1.8 / 2.5 / 5.5 0 4.5 / 6.6 / 7.6 0
100000 zoom at full extent all 444.3 / 494.4 / 941.0 240 4.4 / 6.5 / 15.0 1 6.4 / 7.5 / 9.2 0
100000 pan, 2 000 bars visible all 12.7 / 13.5 / 14.0 133 4.8 / 6.2 / 8.2 1 5.1 / 5.9 / 7.5 0
100000 pan, 200 bars visible all 3.1 / 3.4 / 6.6 0 3.5 / 5.5 / 7.5 0 4.5 / 7.1 / 8.4 0

# 1728 × 1000 pt

Bars Scenario Indicators 1.0.1 renderer missed 2.0 renderer missed 2.0 with 50 drawings missed
10000 zoom at full extent none 7.4 / 8.2 / 9.1 0 4.0 / 5.8 / 8.6 0 5.1 / 5.9 / 8.2 0
10000 pan, 2 000 bars visible none 3.6 / 5.8 / 54.3 2 3.5 / 5.2 / 7.6 0 4.5 / 6.4 / 8.1 0
10000 pan, 200 bars visible none 2.4 / 3.0 / 49.1 2 3.2 / 4.9 / 7.0 0 4.2 / 7.7 / 8.6 0
10000 zoom at full extent all 51.7 / 58.2 / 92.8 240 4.3 / 6.6 / 8.3 0 6.7 / 7.3 / 8.2 0
10000 pan, 2 000 bars visible all 12.4 / 13.6 / 14.5 120 4.1 / 7.3 / 9.3 0 6.0 / 6.7 / 7.9 0
10000 pan, 200 bars visible all 3.6 / 3.9 / 7.2 0 4.6 / 6.2 / 8.2 0 4.7 / 5.6 / 6.3 0
20000 zoom at full extent none 7.4 / 7.8 / 35.6 1 4.3 / 6.0 / 7.8 0 4.9 / 7.1 / 19.2 2
20000 pan, 2 000 bars visible none 3.2 / 5.5 / 31.1 1 4.3 / 6.2 / 6.9 0 4.5 / 6.9 / 10.2 0
20000 pan, 200 bars visible none 3.1 / 4.1 / 5.6 0 2.6 / 4.9 / 5.3 0 4.1 / 7.6 / 8.6 0
20000 zoom at full extent all 68.2 / 73.0 / 174.3 240 4.6 / 5.7 / 7.8 0 7.0 / 7.6 / 9.8 0
20000 pan, 2 000 bars visible all 8.5 / 9.4 / 10.0 10 3.9 / 7.2 / 10.4 0 5.9 / 6.4 / 7.9 0
20000 pan, 200 bars visible all 4.6 / 6.1 / 8.7 0 4.3 / 6.4 / 7.7 0 4.7 / 5.6 / 7.0 0
50000 zoom at full extent none 24.1 / 25.6 / 101.1 240 2.7 / 4.9 / 6.1 0 5.0 / 6.4 / 9.4 0
50000 pan, 2 000 bars visible none 3.8 / 4.8 / 47.1 1 3.4 / 4.6 / 6.8 0 4.7 / 6.9 / 8.5 0
50000 pan, 200 bars visible none 2.4 / 2.9 / 3.4 0 2.9 / 4.2 / 5.2 0 4.5 / 6.6 / 7.9 0
50000 zoom at full extent all 230.3 / 249.8 / 515.6 240 4.7 / 5.1 / 10.9 0 7.7 / 8.7 / 11.8 0
50000 pan, 2 000 bars visible all 10.3 / 13.7 / 14.7 70 4.3 / 7.5 / 175.9 7 6.3 / 7.1 / 7.9 0
50000 pan, 200 bars visible all 3.5 / 5.1 / 8.2 0 4.1 / 6.4 / 8.1 0 4.8 / 6.0 / 8.0 0
100000 zoom at full extent none 45.7 / 49.8 / 130.7 240 2.9 / 5.4 / 7.3 0 5.5 / 6.3 / 7.4 0
100000 pan, 2 000 bars visible none 3.6 / 4.3 / 68.7 2 2.9 / 4.0 / 6.0 0 4.8 / 6.1 / 23.0 5
100000 pan, 200 bars visible none 2.5 / 2.9 / 16.4 1 2.4 / 3.8 / 5.1 0 4.2 / 7.2 / 8.1 0
100000 zoom at full extent all 476.1 / 540.9 / 1055.3 240 4.5 / 6.1 / 15.4 1 7.5 / 8.2 / 10.7 0
100000 pan, 2 000 bars visible all 12.9 / 14.0 / 142.8 124 3.6 / 7.2 / 25.6 1 6.1 / 6.9 / 8.9 0
100000 pan, 200 bars visible all 3.7 / 4.6 / 38.8 2 4.5 / 5.9 / 7.2 0 5.4 / 8.5 / 141.5 4

# Results — draw closure alone (ms, median / p95), 1000 × 640 pt

Bars Scenario Indicators 1.0.1 2.0 2.0 with 50 drawings
10000 zoom at full extent none 5.4 / 5.8 1.3 / 1.9 3.8 / 6.2
10000 pan, 2 000 bars visible none 2.3 / 2.4 2.2 / 3.5 3.8 / 6.8
10000 pan, 200 bars visible none 1.3 / 1.4 1.6 / 2.7 4.3 / 6.6
10000 zoom at full extent all 36.5 / 46.5 3.9 / 4.7 4.7 / 5.1
10000 pan, 2 000 bars visible all 10.6 / 11.5 3.9 / 4.8 4.5 / 5.0
10000 pan, 200 bars visible all 2.5 / 3.0 2.9 / 4.5 4.1 / 5.4
20000 zoom at full extent none 9.9 / 10.4 2.0 / 3.4 4.2 / 5.5
20000 pan, 2 000 bars visible none 2.2 / 2.6 1.9 / 3.1 4.4 / 6.3
20000 pan, 200 bars visible none 1.2 / 1.5 1.3 / 1.8 4.1 / 6.5
20000 zoom at full extent all 68.8 / 92.0 3.8 / 4.9 4.5 / 4.8
20000 pan, 2 000 bars visible all 7.9 / 11.0 3.7 / 4.4 4.2 / 4.5
20000 pan, 200 bars visible all 3.6 / 4.6 2.8 / 4.7 4.3 / 5.2
50000 zoom at full extent none 16.2 / 18.9 1.8 / 2.4 4.1 / 5.3
50000 pan, 2 000 bars visible none 2.1 / 2.4 1.4 / 2.1 3.9 / 6.2
50000 pan, 200 bars visible none 1.5 / 2.5 1.1 / 1.3 3.6 / 6.8
50000 zoom at full extent all 154.0 / 169.0 3.6 / 4.7 5.6 / 6.2
50000 pan, 2 000 bars visible all 7.9 / 9.4 2.8 / 5.2 5.1 / 5.6
50000 pan, 200 bars visible all 2.5 / 3.2 1.8 / 2.7 4.4 / 4.8
100000 zoom at full extent none 29.2 / 32.3 1.8 / 2.1 4.4 / 6.5
100000 pan, 2 000 bars visible none 2.1 / 3.4 1.9 / 2.5 4.0 / 5.1
100000 pan, 200 bars visible none 1.2 / 2.1 1.1 / 1.6 3.9 / 5.9
100000 zoom at full extent all 416.9 / 454.2 3.1 / 4.9 5.4 / 6.3
100000 pan, 2 000 bars visible all 11.4 / 11.9 3.5 / 4.8 4.4 / 5.0
100000 pan, 200 bars visible all 2.3 / 2.5 2.6 / 4.0 3.9 / 6.4

# Results — off-screen full redraw (ms, median / worst of 10)

Bars Bitmap Indicators 1.0.1 2.0 2.0 with 50 drawings
10000 2000 × 1280 px none 4.8 / 4.9 0.6 / 0.6 2.9 / 3.1
10000 2000 × 1280 px all 41.8 / 43.2 1.7 / 1.8 3.9 / 4.0
10000 5120 × 2880 px none 6.8 / 7.1 1.2 / 1.3 3.6 / 3.9
10000 5120 × 2880 px all 48.6 / 48.9 3.8 / 4.0 6.1 / 6.3
20000 2000 × 1280 px none 6.6 / 6.7 0.8 / 0.9 2.9 / 3.2
20000 2000 × 1280 px all 63.9 / 64.5 1.7 / 1.8 3.8 / 3.9
20000 5120 × 2880 px none 7.8 / 8.1 1.5 / 1.5 3.7 / 3.8
20000 5120 × 2880 px all 64.3 / 67.4 3.6 / 3.7 5.8 / 6.0
50000 2000 × 1280 px none 17.0 / 18.0 0.6 / 0.6 2.9 / 3.0
50000 2000 × 1280 px all 219.4 / 237.8 1.6 / 1.8 3.9 / 4.5
50000 5120 × 2880 px none 24.5 / 25.6 1.2 / 1.2 3.7 / 3.7
50000 5120 × 2880 px all 160.9 / 237.8 4.2 / 4.2 6.5 / 6.6
100000 2000 × 1280 px none 41.9 / 44.9 0.8 / 1.0 4.8 / 5.6
100000 2000 × 1280 px all 434.5 / 457.5 3.1 / 5.7 4.9 / 5.6
100000 5120 × 2880 px none 44.2 / 46.7 1.3 / 2.0 4.2 / 4.4
100000 5120 × 2880 px all 355.4 / 435.3 4.6 / 5.2 7.2 / 8.9

# Memory (physical footprint, MB)

Bars Run Before loading Series loaded After its scenarios
10000 1.0.1 82.9 84.9 482.5
20000 1.0.1 482.5 429.2 526.6
50000 1.0.1 526.6 535.3 633.6
100000 1.0.1 633.6 643.6 780.5
10000 2.0 298.5 300.7 636.9
20000 2.0 636.9 640.6 649.8
50000 2.0 639.9 596.5 691.4
100000 2.0 691.4 580.5 1385.6

The footprint figures are dominated by what the harness itself allocates (window backing stores up to 3456 × 2000 px and 5120 × 2880 px bitmaps from ImageRenderer, 59 MB each before release), and the runs are consecutive in one process, so "before loading" of a row includes what earlier rows left behind. What can be read from them: loading a series costs a few megabytes (10 000 bars: about 2 MB, the first row of each run), and the per-zoom-level caches of the 2.0 renderer (at most 24 bucket lists and 96 line envelopes, each the series length divided by the reduction factor) are not visible at this resolution. The idle footprint of the app is in docs/TEST_REPORT.md.

# Reading the numbers

  • 1.0.1 renderer. Fine up to a few thousand visible bars without indicators. With indicators it missed every frame once about 10 000 bars were visible (39 ms median at 10 000 bars, 165 ms at 50 000, 444 ms at 100 000), and without indicators from about 20 000–50 000 visible bars. closure is 90–95 % of main in those rows: the time was in the app's draw code, on the main thread.
  • 2.0 renderer. Over the 96 on-screen scenarios, 22 frames out of 23040 were missed (0.10 %). The highest median is 7.7 ms (50000 bars, 1728 × 1000 pt, zoom at full extent, all indicators, 50 drawings); the highest p95 is 8.7 ms (50000 bars, 1728 × 1000 pt, zoom at full extent, all indicators, 50 drawings). 2 scenarios out of 96 have a p95 above the 8.33 ms budget: 50000 bars, 1728 × 1000 pt, zoom at full extent, all indicators, 50 drawings (p95 8.7 ms); 100000 bars, 1728 × 1000 pt, pan, 200 bars visible, all indicators, 50 drawings (p95 8.5 ms). The cost no longer grows with the series length (compare 10 000 and 100 000 bars): it is set by the window width, the number of indicator lines and the number of drawings.
  • Drawings. 50 drawings add about 2–3 ms per frame in this Debug build (text of Fibonacci levels, notes and measure labels is resolved every frame). It stays within budget at the window size; at 1728 × 1000 pt with every indicator it is the configuration closest to the limit. A chart with 50 drawings is an extreme case, and the cost is per drawing, not per bar.
  • 5K. The off-screen full redraw at 5120 × 2880 px takes 2–15 ms depending on the series and the overlays (1.0.1: 7–355 ms). This is CPU rasterisation through ImageRenderer, slower than the on-screen path; an on-screen 5K measurement needs a 5K display, which this machine does not have.

# What changed in the renderer (the "cheap wins")

  1. Aligned OHLC reduction, cached per zoom level (Downsampler.ohlcAligned, ChartModel.buckets(per:)). 1.0.1 re-merged the visible bars on every frame, starting at the first visible bar, so marks regrouped while panning. Buckets are now aligned on the series (bucket k = bars k·per … (k+1)·per − 1), computed once per reduction factor and sliced per frame. Extremes are still exact (unit-tested); the footer still states "1 mark = n bars".
  2. Indicator lines reduced like the bars (Downsampler.envelope): first → low → high → last per mark instead of one vertex per source bar. A 50 000-bar SMA is now about 400 × 4 vertices, not 50 000; five overlays plus RSI and two MACD lines were the bulk of the 1.0.1 cost. The envelopes are cached per series and reduction factor.
  3. Time axis without a scan. 1.0.1 asked the calendar for the day or month of every visible bar on every frame. The unit boundaries are now computed once per series and found by binary search.
  4. Comparison series aligned once on the main series (an array indexed like the bars) instead of one dictionary lookup per visible bar and compared symbol per frame; they are reduced like any other line.
  5. Path batching: grid lines in one path, candle bodies and wicks appended to four paths without intermediate Path values, no per-bar array allocation in the MACD panel.

Nothing visual was traded for this: with fewer bars than marks (zoomed in), every bar is drawn as received, exactly as in 1.0.1.

# Decision

Metal is not adopted. The condition set for adopting it — not holding the 8.3 ms budget where it matters — is not met after the changes above: a pan or zoom of a 50 000-bar series (the server's maximum per request) with every indicator costs 4.8 ms of main-thread work at the median in a Debug build, and no frame is missed. A Metal renderer would still need the same reduction to one mark per ~2.5 pt (drawing 50 000 sub-pixel candles is wasted work on any backend), would have to re-implement text, dashes, anti-aliased hairlines, PDF export (vector today, through the same drawing code) and accessibility, and would add a second code path to keep visually identical in light and dark. The numbers do not justify that.

What would reopen the question: a measured miss on a 5K or 6K external display at 120 Hz with many overlays (not measurable here), or a feature that needs tens of thousands of individually visible marks (tick charts, depth heat-maps). The harness stays in the tree for that day.

# Reproducing

Scripts/build.sh debug, then the command at the top. Options: -VQXChartBenchSizes 1000x640,1728x1000, -VQXChartBenchFrames 240. The report is JSON (rows, off-screen, memory, machine, display). The two reports behind this page were kept outside the repository (~/Library/Developer/VQuant/shots/chart/bench-before.json and bench-after.json on the machine that ran them, 2026-11-20).

Files main
Menu

Theme