Replacing a memcpy with a Metal blit cost my iPad VNC client 15% more CPU

Blog · Last updated: December 3, 2026 · By Jérémie Aouate

In DualMac, a VNC client for iPad and iPhone, one memcpy was 12.4% of the app's CPU. Both ends of it are IOSurfaces, so I tried a patch that does the copy as a Metal blit, expecting about 10% back. On an M4 iPad Air the copy got 2.5 times slower instead, and the app's CPU went up by 13 to 16%. Through the GPU, even a 110 KB copy takes 1.8 ms on the receive thread. memcpy does it in 0.04 ms.

The copy

DualMac decodes screen updates into one surface and shows another, so the renderer never reads a half-drawn picture. After each update it copies the parts that changed (it tracks them as a grid of small cells) from the working surface to the display surface. That copy is a memcpy, and in a trace it was 12.4% of the app's CPU.

Both surfaces are IOSurfaces. Metal can wrap an IOSurface in a texture without copying its memory, and a blit between two textures runs on the GPU. On paper, that takes the copy off the CPU.

The patch

Trimmed of its checks, it looks like this:

// on every copy
let workingTexture = device.makeTexture(descriptor: descriptor,
                                        iosurface: workingSurface, plane: 0)

let commandBuffer = queue.makeCommandBuffer()
let blit = commandBuffer.makeBlitCommandEncoder()

cells.forEachRun { x, y, width, height in
    blit.copy(from: workingTexture, sourceSlice: 0, sourceLevel: 0,
              sourceOrigin: MTLOrigin(x: x, y: y, z: 0),
              sourceSize: MTLSize(width: width, height: height, depth: 1),
              to: displayTexture, destinationSlice: 0, destinationLevel: 0,
              destinationOrigin: MTLOrigin(x: x, y: y, z: 0))
}

blit.endEncoding()
commandBuffer.commit()
commandBuffer.waitUntilCompleted()

That's one command buffer per screen update and one copy per run of changed cells, and then the receive thread waits. It waits because the next update is decoded into the working surface, and the GPU has to be done reading it before that starts. If Metal refuses anything, the code falls back to memcpy. A blit copies the bytes as they are, and the scores that compare the iPad's picture with the Mac's didn't move.

The measurement

The app was a Release build on an 11-inch iPad Air (M4) on Wi-Fi, connected to an Apple silicon Mac over a direct Tailscale link. The patch sat behind a launch argument. A run is four passes in the order off, on, on, off, with no reinstall in between, so that whatever drifts during the run lands on both sides.

The Mac showed a page that keeps the whole screen changing, so every update repainted the whole surface. There were two screen sizes: 2360 × 1468 (13.9 MB per copy) and 2880 × 1800 (20.7 MB).

App CPU, % of one core memcpy Metal blit
2360 × 1468 35.7, 35.8, 36.6 35.9, 41.4, 41.7, 42.0
2880 × 1800 33.4, 33.4, 33.6, 33.8 37.9, 38.0, 38.0, 38.5
Every 30-second sample, sorted. One memcpy sample was lost.

The medians go from 35.8% to 41.5% and from 33.5% to 38.0%. Per megabyte received, so a busier pass can't decide it, that's 16% and 15% more. One sample with the blit came in at 35.9%, level with memcpy. The log shows the blit ran for every copy in that sample, so it wasn't the fallback. I don't know why, and I've left it in.

Frames reaching the screen, counted on a recording of the iPad: 50.1 and 52.9 per second with memcpy, 46.0 and 48.4 with the blit. At 2880 × 1800: 50.4 and 53.6, then 46.6 and 50.3. That leans the same way, but with two recordings a side I wouldn't swear to it. With memcpy the counts also sit at the recording's own rate, so the real gap could be larger or smaller. The median delay between the Mac drawing something and the iPad showing it went from 97 to 99.5 ms and from 96 to 98.5 ms, which is within the noise.

The log line

Every 10 seconds, the app logs how many copies it made, how many bytes they moved and how long the receive thread spent in the copy step. Dividing gives the time per copy.

Time per copy on the receive thread, median, memcpy against a Metal blit that is committed and waited for. Whole surface of 20.7 MB: 1.08 ms against 2.61 ms. Whole surface of 13.9 MB: 0.87 ms against 2.19 ms. Small update of 110 KB: 0.04 ms against 1.78 ms.
Medians of the 10-second log windows. Times vary by about 0.4 ms between test pages. The small updates come from a still screen, about 6 a second. At 2880 × 1800 they take 2.19 ms through the GPU.

The bottom pair is the interesting one. Moving 110 KB through the GPU takes 1.8 ms, and moving 13.9 MB takes 2.2 ms. So the size barely matters to the blit: the extra 13.8 MB added about 0.4 ms, where memcpy needs 0.8 ms. Nearly all of the 1.8 to 2.2 ms is paid whatever the size, about 50 times a second: wrapping the working surface in a texture, making a command buffer, encoding, committing, and blocking until the GPU reports back. I didn't time those parts separately.

As for memcpy, 13.9 MB in 0.87 ms is 16 GB/s. At that speed, copying a whole frame takes half as long as a trip to the GPU with almost nothing to copy.

If I stretch these two sizes into a line, which is a guess, the blit catches up at around 60 to 70 MB per copy. That's about a 5K screen (59 MB). I haven't tested one.

What I can't tell you

Before the run, I had decided that a change in app CPU only counts if it's over 25% and over 10 points of a core. This one is neither, so on CPU alone it's a tie. A patch meant to save 10% that adds a GPU round trip to the receive loop doesn't get merged for a tie, so the copy is still a memcpy.

I didn't try the versions that don't wait: a second working surface so the decoder can move on, or one command buffer for several updates. If you know the cheap way to do this on iOS, I'd like to hear it.

Three other patches measured the same way