Cutting the CPU cost of scrolling the results grid
How we profiled SQLly's results grid on an M4 Max MacBook Pro and cut scroll CPU from about 36% of a core to 28%, or 22% with a new 60 per second repaint cap.
A user told me the CPU looked high when they scrolled the results grid. I watched it myself and agreed. Holding a trackpad scroll over a big result kept SQLly at about a third of a CPU core, for a grid that only has to slide some text up the screen. This post covers how I measured it, what was actually costing the time, what we changed, and what it bought.
The test machine and the test
Every number here comes from one machine:
- Computer: MacBook Pro (Mac16,5) with an Apple M4 Max: 16 CPU cores (12 performance, 4 efficiency), a 40-core GPU and 128 GB of memory.
- Display: the built-in Liquid Retina XDR panel with ProMotion, so the window was drawing at up to 120 frames per second.
- OS: macOS 26.6.
- Build: SQLly's optimized release profile, the same settings the shipping app uses, with symbols kept for profiling.
The data was a local SQLite table of 40,000 rows and eight columns: integers, decimals, dates,
short text, a note column with some NULLs, and a payload column holding
about 2,500 characters of JSON-ish text per row. That last column matters, as you’ll see. I
ran SELECT * FROM big, then a script posted synthetic trackpad scroll events at 120
per second, back and forth, while I read the process’s CPU from top and took
8-second sample profiles of the main thread at 1 ms intervals. CPU figures below are
percent of one core.
One lesson came early. The same build launched twice could read five points apart, depending on which cores macOS put it on. So every comparison in this post was measured by alternating builds back to back in one session: baseline, new, baseline, new.
My first guess was wrong
Before profiling I read the code and wrote down a ranking. I guessed that per-cell formatting
(dates, number separators) and the app’s view render functions were the big
costs. The profile disagreed. Per-cell formatting barely registered. The render functions were
about a tenth of the work.
The first surprise was that the main thread spent 60 to 75% of its wall time waiting for Metal to hand it the next drawable. That’s idle time, not CPU, and it’s just frame pacing. The real work came to about 1.6 to 1.8 ms per frame. At 120 frames a second that adds up to the third of a core we were seeing. The work split into two roughly equal halves.
Problem one: the grid shaped text it could never show
SQLly’s grid paints its own cells on a canvas. Every frame, for every visible cell, it asked the text system to shape the cell’s text so it could measure and draw it. The text system keeps a per-frame cache, but each lookup still hashes and compares the whole string. A cell holding 2,500 characters was hashed in full on every frame to draw the 30 or so characters that fit. When the text overflowed the column it was shaped up to three times: once in full, once for the ellipsis, and once for the fitted text.
We changed three things in the grid:
- Cut first, then shape. A value is trimmed to the prefix that could possibly fit its column, plus a few characters of slack for the ellipsis, before anything scans or shapes it. A cell holding a document now costs the same as a short one.
- Keep each cell’s shaped line. The grid now keeps the finished line for every cell across frames. Each entry is checked against a fingerprint of everything the shape depends on: the text, color, font face, size, column width and syntax colors. A steady scroll only shapes the rows coming into view, and a change to data, format, theme or width just misses the fingerprint and reshapes.
- Don’t repaint for nothing. Moving the pointer used to request a repaint on every mouse move. Now it only does when the hovered cell or header actually changes.
The first change alone took the long-column scroll from about 35% to 30%, with screenshots that were pixel-identical before and after.
Problem two: every frame rebuilt the whole window
The other half was outside the grid. GPUI, the UI framework SQLly is built on, runs every
view’s render on every frame unless the view is explicitly cached. Then it
lays out and prepaints the whole element tree. SQLly has about a hundred views, and none of
them were cached. So a scroll that only moved the grid also rebuilt the explorer, the editor,
the tab strip, the command bar, the menu bar and the status bar 120 times a second.
The profile also turned up a few small things that only showed because they ran every frame:
- The keyboard-shortcut hints in the tab bar rebuilt the whole default key-binding table from scratch each frame, just to print “⌘S”. They’re now resolved once and remembered.
- When no masking rules were loaded in memory, the results view read the rules file from disk on every frame. It now loads once.
- The selection stats in the results footer (count, sum, average and so on) were recomputed every frame. With a whole column selected that meant copying every value in it 120 times a second. They’re now computed when the selection changes.
The big fix was caching. The explorer sidebar, the editor and the in-window menu bar now draw from the previous frame unless they themselves change. That only works when the cache is trustworthy, and getting there took a few rules:
- A cached view only knows about its own changes. If it reads some other piece of state, it won’t notice when that state changes. That’s fine for the sidebar and editor, which own what they draw.
- Animations inside a cached view freeze. The pulsing dot on a connecting server, and the completion popup’s fade-in, ask for extra frames from inside the view. So each view reports when it’s animating, and it’s drawn live for those frames.
- A cached view has to state its size. The first time we cached the menu bar it shrank to the width of its contents and the window title slid to the left. A cached view’s root is laid out on its own, so it has to state its size.
The tab strip, command bar and status bar were harder. They draw from many sources: each tab’s dirty dot, the connection pickers, the transaction chip, the zoom control. An audit of every input would be easy to get wrong, and a stale toolbar is a worse bug than some CPU. So those rows refresh on two rules instead. Any change to the app itself refreshes them on the next frame. And no matter what, they never draw from a cache more than a fifth of a second old. During a scroll that means the chrome rebuilds five times a second instead of 120. We checked hovering a tab, the dirty dot after typing, opening a new tab, the server picker, the zoom popover, the command palette and the sidebar toggle with the caches on, and all of them behaved as before.
An optional cap for 120 Hz screens
After all that, every remaining frame does real work: it paints about 360 cells. On a 120 Hz display the cheapest frame is the one you skip. So there’s a new setting, off by default: Settings › Results › Performance › Scrolling › Limit scrolling repaints to 60 per second.
Scroll input still applies on every event, so the grid always ends up at the latest position. Only the painting waits. The first version paced only the trackpad events and barely helped, because the grid’s smooth-scroll and bounce animation was requesting its own repaints on a separate timer. Once both went through the same pacing, the grid dropped from about 113 frames a second to about 75 while scrolling. It doesn’t go all the way to 60 because the chrome refresh and timer jitter add a few frames. If you value the last bit of smoothness more than the CPU, leave it off.
The results
Same machine, same data, same scripted scroll. Builds alternated back to back in two rounds,
and each cell shows the two steady-state top readings from that round:
| Build | Round 1 | Round 2 |
|---|---|---|
| Before (v1.30.2) | 37.5% / 34.9% | 36.6% / 35.8% |
| After | 29.6% / 27.3% | 28.1% / 28.3% |
| After, with the 60 per second cap | 22.8% / 21.2% | 23.2% / 21.0% |
So scrolling went from about 36% of a core to about 28% with no setting to change, a cut of roughly a quarter. With the cap on it’s about 22%, close to 40% below where we started. The changes that got us there:
| Change | Where the time went |
|---|---|
| Cut cell text to what fits before shaping | Long values hashed and shaped in full, up to three times, every frame. |
| Keep each cell’s shaped line across frames | Every visible cell reshaped on every frame. |
| Repaint on hover only when the target changes | Every mouse move requested a frame. |
| Draw the sidebar, editor and menu bar from cache | Rebuilt and re-laid-out on every grid frame. |
| Draw the tab strip, command bar and status bar from cache, refreshed within 200 ms | The same, for the busiest chrome rows. |
| Remember shortcut hints, footer stats and masking rules | Rebuilt, recomputed or re-read from disk per frame. |
| Optional 60 per second scroll repaint cap | A 120 Hz display drawing twice as many frames as a scroll needs. |
What’s left
What’s left is mostly GPUI’s own floor: putting a few hundred glyph runs and grid lines into the scene and handing them to Metal each frame, plus layout for the results pane’s toolbar, which is still drawn live. There are smaller wins left, such as drawing column separators as one line per column instead of one per cell. But the cost that grew with the size of your data is gone. A result full of big JSON documents now scrolls for the same cost as one full of integers.
These changes ship in the next SQLly release. If scrolling still feels heavy on your machine, send us a note from Help › Send Feedback. Include your Mac model and display, since that’s exactly what the numbers above depend on.