Skip to content

improve(desktop-gpui): write BGRA unpad through a raw pointer - #2619

Open
kyzer1023 wants to merge 1 commit into
CapSoftware:mainfrom
kyzer1023:cursor/perf-gpui-unpad-v2-squashed
Open

kyzer1023 wants to merge 1 commit into
CapSoftware:mainfrom
kyzer1023:cursor/perf-gpui-unpad-v2-squashed

Conversation

@kyzer1023

@kyzer1023 kyzer1023 commented Oct 15, 2026 •

Copy link
Copy Markdown

Summary

unpad_rgba_to_bgra (Linux/Windows editor playback via frame_image, and the portable camera-blur readback) allocated with vec![0; len] and then overwrote every byte. This writes through Vec::with_capacity + *mut u8 + set_len instead: same skip-zero allocation, tight nested loop, no iterator zip.

An earlier spare_capacity_mut / MaybeUninit::write version was faster on rustc 1.99 but ~0.78x slower on rustc 1.88 (the pinned toolchain), because 1.88 doesn't inline Iterator::zip over the MaybeUninit chunks. The raw-pointer loop is a win on both.

Change

  • apps/desktop-gpui/kernels/src/frame.rs: raw-pointer unpad, no zero fill. Only unpad_rgba_to_bgra changes; main's reorder_32bit_row / convert_32bit_rows work is untouched.
  • apps/desktop-gpui/kernels/benches/frame_unpad.rs: before/after harness with the previous implementation inlined (results consumed with let _ = black_box(...) per AGENTS.md)
  • apps/desktop-gpui/kernels/Cargo.toml: keeps all of main's benches (including overlay_image_inspection, frame_export, screenshot_alpha) and appends frame_unpad
  • Regression test for wgpu 256-byte aligned stride

Rebased onto main (0bc36e0d). Single commit.

Relationship to #2513

#2513 edits the same file (kernels/src/frame.rs) but different functions (swap_red_blue_into, convert_32bit_rows). The changes are complementary; landing both is a same-file merge, not overlapping edits.

Benchmarks

On the rebased commit. Intel Xeon, 4 cores, KVM; Ubuntu 24.04.5 LTS, Linux 6.12.94+. GPUI-like release profile (opt-level=3, lto=thin, codegen-units=1), 5 warmup, median of 30. Baseline is main's vec![0] path.

Toolchain 1248×702 1080p 4K
rustc 1.88.0 0.440 → 0.341 ms (1.29×) 1.094 → 0.841 ms (1.30×) 4.268 → 3.289 ms (1.30×)
rustc 1.99.1 0.437 → 0.339 ms (1.29×) 1.080 → 0.837 ms (1.29×) 4.373 → 3.386 ms (1.29×)

Earlier runs on previous bases matched (1.29–1.32× on both toolchains), with byte-identical output to the previous implementation and Miri clean on 11 tests.

Tests

Full cap-gpui-kernels suite: 40 passed, 0 failed, 1 ignored (benchmark_waveform_peaks) on both rustc 1.88.0 and 1.99.1.

The full GPUI app was not built: the [patch."https://github.com/wingleeio/zed"] entry points at /zed-cap, which isn't available in the test VM, so the kernels crate was extracted into a standalone workspace.

Repro

With /zed-cap checked out:

cd apps/desktop-gpui
cargo test -p cap-gpui-kernels --lib
cargo bench -p cap-gpui-kernels --bench frame_unpad -- --nocapture

Without it, extract apps/desktop-gpui/kernels into a one-crate workspace and run:

export FFMPEG_DIR=$PWD/target/native-deps
export LD_LIBRARY_PATH=$FFMPEG_DIR/lib
cargo test -p cap-gpui-kernels --lib
cargo bench -p cap-gpui-kernels --bench frame_unpad -- --nocapture

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge; no blocking issues remain.

Summary

Rewrites unpad_rgba_to_bgra to fill allocated space through a raw pointer instead of zeroing it first.

  • Frame conversion now skips a zero-fill pass.

Reviews (3) · Last reviewed commit: "improve(desktop-gpui): write BGRA unpad ..." · Reviewed by Greptile

@vercel

vercel Bot commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

@kyzer1023 is attempting to deploy a commit to the Cap Software Inc Team on Vercel.

A member of the Team first needs to authorize it.

Comment on lines +21 to +26
black_box(f(
black_box(&src),
black_box(width),
black_box(stride),
black_box(height),
));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

P2 Benchmark silently discards its result

black_box(f(...)) returns Option<Vec<u8>>, but the benchmark silently discards it. AGENTS.md requires every Option result to be explicitly handled. Use let _ = black_box(f(...)); to mark the discard as intentional and avoid the unused_must_use warning. This repository requirement must be satisfied before merging.

Suggested change
black_box(f(
black_box(&src),
black_box(width),
black_box(stride),
black_box(height),
));
let _ = black_box(f(
black_box(&src),
black_box(width),
black_box(stride),
black_box(height),
));

Context Used: AGENTS.md (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/desktop-gpui/kernels/benches/frame_unpad.rs
Line: 21-26

Comment:
**Benchmark silently discards its result**

`black_box(f(...))` returns `Option<Vec<u8>>`, but the benchmark silently discards it. `AGENTS.md` requires every `Option` result to be explicitly handled. Use `let _ = black_box(f(...));` to mark the discard as intentional and avoid the `unused_must_use` warning. This repository requirement must be satisfied before merging.

```suggestion
                let _ = black_box(f(
                    black_box(&src),
                    black_box(width),
                    black_box(stride),
                    black_box(height),
                ));
```

**Context Used:** AGENTS.md ([source](https://github.com/capsoftware/cap/blob/main/AGENTS.md))

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Fixed in cb5d9d7: the benchmark now uses let _ = black_box(f(...)); to make the discard explicit, per AGENTS.md.

@cursor
cursor Bot force-pushed the cursor/perf-gpui-unpad-v2-squashed branch from 10cab80 to cb5d9d7 Compare October 15, 2026 08:00

Copy link
Copy Markdown
Author

@greptileai review

unpad_rgba_to_bgra was vec![0; len] then overwriting every pixel on the
Linux/Windows playback path. Write through with_capacity + *mut u8 +
set_len instead. spare_capacity_mut/MaybeUninit::write skips calloc but
on rustc 1.88 (the Cap pin) the inner stores stay four byte moves and
regress vs the zero-filled baseline.
@cursor
cursor Bot force-pushed the cursor/perf-gpui-unpad-v2-squashed branch from 5368e51 to 69287d9 Compare October 19, 2026 18:06

Copy link
Copy Markdown
Author

@greptileai review

This branch has not been deployed

No deployments
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.

1 participant