Skip to content

perf(gpui): SIMD vectorize rgba_to_rgb and eliminate image conversion overhead - #2672

Open
nikolas-sapa wants to merge 2 commits into
CapSoftware:mainfrom
nikolas-sapa:perf/gpui-simd-rgba-to-rgb
Open

nikolas-sapa wants to merge 2 commits into
CapSoftware:mainfrom
nikolas-sapa:perf/gpui-simd-rgba-to-rgb

Conversation

@nikolas-sapa

@nikolas-sapa nikolas-sapa commented Oct 17, 2026 •

Copy link
Copy Markdown

Summary

  • Vectorizes rgba_to_rgb in apps/desktop-gpui/kernels/src/codec.rs using aarch64 NEON SIMD (vld4q_u8 + vst3q_u8), processing 16 pixels (64 bytes in -> 48 bytes out) per iteration.
  • Adds an unrolled 4-pixel chunk portable fallback for non-aarch64 targets or trailing buffers.
  • Eliminates iterator and intermediate vector reallocation overhead from chunks_exact(4).flat_map(...).collect().
  • Updates encode_jpeg_rgba_as_rgb to call rgba_to_rgb directly instead of rgba.convert(), saving memory allocation and redundant pixel traversal during JPEG thumbnail and export encoding.
  • Includes bit-exact tests across all pixel boundaries and a benchmark harness in benches/rgba_to_rgb.rs.

Benchmark Results (Apple Silicon M-series)

Workload Pixels Baseline Median Optimized Median Speedup Correctness
thumbnail-320x180 57,600 81.8 µs 5.8 µs 14.1x Bit-exact
preset-216x122 26,352 36.5 µs 3.1 µs 11.8x Bit-exact
screen-720p 921,600 1,157 µs 130 µs 8.9x Bit-exact
screen-1080p 2,073,600 2,576 µs 417 µs 6.2x Bit-exact
screen-retina 5,939,136 7,293 µs 934 µs 7.8x Bit-exact
screen-4k 8,294,400 10,087 µs 1,145 µs 8.8x Bit-exact

Test Plan

  • Run cargo test --manifest-path apps/desktop-gpui/kernels/Cargo.toml
  • Run cargo bench --manifest-path apps/desktop-gpui/kernels/Cargo.toml --bench rgba_to_rgb

RetriggerView in GreptileConfidence Score: 4/5

Remove the narrating comment before merging to meet the repository guide; the independent JPEG reference test is a non-blocking improvement.

Findings

  1. P2 Comment repeats the code ▶
  2. P2 JPEG test compares identical paths ▶
Fix with agent prompt
### Issue 1
apps/desktop-gpui/kernels/src/codec.rs:45
The new `// Portable 4-pixel chunk unrolled fallback` comment only describes the loop below it. The repository guide explicitly bans comments that narrate what the code does. Remove it before merging to meet that requirement.

```suggestion

```

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

### Issue 2
apps/desktop-gpui/kernels/src/codec.rs:127-128
Routing `encode_jpeg_rgba_as_rgb` through `encode_jpeg_rgb` makes `jpeg_paths_agree_on_the_same_pixels` compare the same conversion and encoder calls with themselves. It no longer checks that this replacement preserves the old JPEG output. Keep an independent reference in the test using `ConvertBuffer` and `encode_image`, then compare it with the new path.

---

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

Summary

Speeds up rgba_to_rgb with NEON on ARM and an unrolled fallback elsewhere.

  • RGBA images reach JPEG encoding through a faster direct pixel converter.

Review was based on code inspection; tests and benchmarks were not run.

Reviews (1) · Last reviewed commit: "perf(gpui): SIMD vectorize rgba_to_rgb a..." · Reviewed by Greptile

@vercel

vercel Bot commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

@nikolas-sapa 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 thread apps/desktop-gpui/kernels/src/codec.rs Outdated
}
}

// Portable 4-pixel chunk unrolled fallback

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 Comment repeats the code

The new // Portable 4-pixel chunk unrolled fallback comment only describes the loop below it. The repository guide explicitly bans comments that narrate what the code does. Remove it before merging to meet that requirement.

Suggested change
// Portable 4-pixel chunk unrolled fallback

Context Used: AGENTS.md (source)

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/desktop-gpui/kernels/src/codec.rs
Line: 45

Comment:
**Comment repeats the code**

The new `// Portable 4-pixel chunk unrolled fallback` comment only describes the loop below it. The repository guide explicitly bans comments that narrate what the code does. Remove it before merging to meet that requirement.

```suggestion

```

**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.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Comment on lines +127 to +128
let rgb = rgba_to_rgb(rgba.as_raw());
encode_jpeg_rgb(&rgb, rgba.width(), rgba.height(), quality)

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 JPEG test compares identical paths

Routing encode_jpeg_rgba_as_rgb through encode_jpeg_rgb makes jpeg_paths_agree_on_the_same_pixels compare the same conversion and encoder calls with themselves. It no longer checks that this replacement preserves the old JPEG output. Keep an independent reference in the test using ConvertBuffer and encode_image, then compare it with the new path.

Prompt To Fix With AI
This is a comment left during a code review.
Path: apps/desktop-gpui/kernels/src/codec.rs
Line: 127-128

Comment:
**JPEG test compares identical paths**

Routing `encode_jpeg_rgba_as_rgb` through `encode_jpeg_rgb` makes `jpeg_paths_agree_on_the_same_pixels` compare the same conversion and encoder calls with themselves. It no longer checks that this replacement preserves the old JPEG output. Keep an independent reference in the test using `ConvertBuffer` and `encode_image`, then compare it with the new path.

---

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

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