Skip to content

perf(frame): simd improvement & unpadded frames in one pass - #2484

Merged
richiemcilroy merged 1 commit into
CapSoftware:mainfrom
viralcodex:simd-improvement
Oct 13, 2026
Merged

richiemcilroy merged 1 commit into
CapSoftware:mainfrom
viralcodex:simd-improvement

Conversation

@viralcodex

@viralcodex viralcodex commented Oct 13, 2026 •

Copy link
Copy Markdown

Problem

GPUI's CPU-backed editor preview converts padded RGBA frames to tightly packed BGRA before uploading them to the sprite atlas. The same conversion runs in the portable camera-blur path.

The per-pixel copy_from_slice(&[...]) produces an expensive SIMD instruction sequence. At 4K, this conversion alone takes approximately 1.65 ms per frame.

Solution

In frame.rs, replace the temporary array and slice copy with direct channel writes:

target[0] = source[2];
target[1] = source[1];
target[2] = source[0];
target[3] = source[3];

This lets LLVM generate a shorter SIMD load/store sequence. Padding handling, input validation, allocation, and output bytes remain unchanged. No unsafe code or platform-specific implementation is added.

Benchmark

Actual production function versus the previous implementation, using synthetic RGBA frames with 256-byte-aligned strides. Apple M4 Pro, Rust 1.88.0, rustc -O, 15 interleaved measured batches after warmup. Medians per conversion, including allocation:

Frame size Before After Speedup
1248×702 169.6 µs 67.7 µs 2.50×
1920×1080 398.2 µs 161.8 µs 2.46×
3840×2160 1,648.4 µs 729.7 µs 2.26×

This saves approximately 0.92 ms per 4K conversion. These are kernel measurements, not end-to-end playback or export timings. The macOS IOSurface preview path bypasses this conversion.

RetriggerConfidence Score: 5/5

This PR appears safe to merge.

Summary

unpad_rgba_to_bgra now removes row padding and swaps red and blue in one pass.

  • Frame conversion now writes tight BGRA pixels directly.

Reviews (1) · Last reviewed commit: "simd improvement" · Reviewed by Greptile

@viralcodex

viralcodex commented Oct 13, 2026 •

Copy link
Copy Markdown
Author

this is built on top of my prev PR: #2478 , (closed it as both changes are combined here)

@viralcodex viralcodex changed the title perf(frame): simd improvement perf(frame): simd improvement & unpadded frames in one pass Oct 13, 2026

@richiemcilroy richiemcilroy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the conversion and both production callers. Local comparisons matched the previous output across 112 valid layouts and reproduced the speedup. Full native CI is still running.

@richiemcilroy
richiemcilroy merged commit 43dd4d1 into CapSoftware:main Oct 13, 2026
19 of 20 checks passed

Copy link
Copy Markdown
Member

Merged, thanks @viralcodex! Nice improvement to the frame conversion path, with clear before/after numbers. Appreciate you contributing to Cap 🫡

@viralcodex

Copy link
Copy Markdown
Author

Merged, thanks @viralcodex! Nice improvement to the frame conversion path, with clear before/after numbers. Appreciate you contributing to Cap 🫡

Hey my pleasure @richiemcilroy, I saw the post on X so though to take the gamble, installed Cap and recorded my first video too for my package i'm about to release, really love what u made here!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants