Skip to content

refactor(cuda): slice mutable views through one implementation - #10880

Merged
0ax1 merged 2 commits into
developfrom
ad/cuda-view-slice-mut
Nov 4, 2026
Merged

0ax1 merged 2 commits into
developfrom
ad/cuda-view-slice-mut

Conversation

@0ax1

@0ax1 0ax1 commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Removes duplicate mutable-view slicing logic by making
CudaViewMut::slice_mut delegate to into_slice_mut.

The temporary copies only view metadata, not device data. The return
lifetime keeps the original view mutably borrowed while the result lives.
Bounds checks, panic messages, pointer arithmetic, and access tracking
remain unchanged.

Changes

  • refactor(cuda): slice mutable views through one implementation
  • docs(cuda): clarify mutable view borrow lifetime

No performance impact is expected: this adds no allocation, CUDA calls,
synchronization, or device-data copies. Optimized builds should eliminate
the temporary view and inline the helper.

No builds, tests, or benchmarks were run for this change.

@0ax1 0ax1 added the changelog/chore A trivial change label Oct 27, 2026
@0ax1

0ax1 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

@claude review this pr in depth

@github-actions

github-actions Bot commented Oct 27, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @0ax1's task in 50s —— View job


Review of #10880: refactor(cuda): slice mutable views through one implementation

Verdict: The change is correct and behaves the same as before. I found no bugs. There is one optional suggestion about the comment.

Is it correct?

  • It compiles. CudaViewMut { ..*self } copies each field out of *self. Moving out of &mut self is only allowed because every field is Copy: CUdeviceptr, usize, &'a Access, &'a Arc<CudaStream> and PhantomData<&'a mut [T]>. CudaViewMut has no Drop impl, so there is no E0509 error either.
  • It is sound. The temporary has type CudaViewMut<'a, T>, and into_slice_mut returns CudaViewMut<'a, T>. That value then shrinks to CudaViewMut<'_, T> because PhantomData<&'a mut [T]> is covariant in 'a. The return type in the signature keeps *self mutably borrowed for as long as the result lives, so callers still can't create two live mutable views. The body doesn't touch self after the copy.
  • Behaviour is unchanged. into_slice_mut (memory.rs:640) does the same range(..).expect("CUDA view range out of bounds") check, offsets the pointer by start * size_of::<T>(), sets len = end - start, and carries access and stream over via ..self. The panic message, offset maths and fields all match the code that was removed.
  • It is safe against future changes. If someone later adds a non-Copy field (such as an owned guard) or a Drop impl, { ..*self } fails to compile. It cannot silently start duplicating ownership.
  • It matches existing code. CudaSlice::slice_mut (memory.rs:424) already uses the same pattern: self.as_view_mut().into_slice_mut(bounds).

Suggestion (optional)

memory.rs:636, comment wording. The comment says "A copy of this view, borrowed mutably for as long as the result." The copy itself isn't borrowed. The borrow checker sees it as a plain bitwise copy with lifetime 'a. What ties the result to &mut self is the CudaViewMut<'_, T> return type in the signature. A reader who takes the comment literally might think { ..*self } creates a reborrow. A more accurate wording:

// Copy the view; the `'_` return lifetime keeps `self` mutably borrowed while the result lives.
CudaViewMut { ..*self }.into_slice_mut(bounds)

Alternatively, add a private fn reborrow(&mut self) -> CudaViewMut<'_, T> helper, the view-side counterpart of CudaSlice::as_view_mut. The lifetime shrink would then sit in a signature instead of a comment, and the call would read self.reborrow().into_slice_mut(bounds), like CudaSlice::slice_mut. With only one caller today, this is a style choice. (transmute_mut couldn't use the helper, because changing the struct's type during a functional update isn't stable.)

Other checks

  • The PR description's performance note is right. The temporary is two words and two references, and it gets optimised away.
  • No new tests are needed for a pure refactor. Existing callers of slice_mut (for example device_buffer.rs:178, stream.rs, pinned.rs) will exercise it once CI builds the crate.
  • The diff stays within one file and needs no SPDX, typos or generated-file changes.

Checks run

  • Read the diff and the surrounding memory.rs code (struct definition, into_slice_mut, range, Drop impls, callers) and reasoned through it.
  • Not run: cargo clippy and tests. This workflow is read-only and CLAUDE.md leaves verification to the user. CI covers it.

@codspeed

codspeed Bot commented Oct 27, 2026 •

Copy link
Copy Markdown

Merging this PR will degrade performance by 12.5%

1 warning

⚠️ Walltime results may be unreliable

Some benchmarks used the Walltime instrument outside CodSpeed Macro Runners, which can make the results inconsistent.

Learn more about Macro Runners

See affected benchmarks

❌ 1 regressed benchmark
✅ 1764 untouched benchmarks
⏩ 130 skipped benchmarks1
🗄️ 47 archived benchmarks run2

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Walltime lt_i64_nullable_avx2 8.3 µs 9.4 µs -12.5%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing ad/cuda-view-slice-mut (570df9d) with develop (93ab9aa)

Open in CodSpeed

Footnotes

  1. 130 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

  2. 47 benchmarks were run, but are now archived. If they were deleted in another branch, consider rebasing to remove them from the report. Instead if they were added back, click here to restore them. ↩

0ax1 added 2 commits November 4, 2026 12:08
`CudaViewMut::slice_mut` repeated the body of `into_slice_mut`. Reslice
a copy of the view, borrowed for as long as the result, instead.

Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
Signed-off-by: Alexander Droste <alexander.droste@protonmail.com>
@0ax1
0ax1 force-pushed the ad/cuda-view-slice-mut branch from 7d93fcb to 570df9d Compare November 4, 2026 12:11
@0ax1
0ax1 marked this pull request as ready for review November 4, 2026 12:13
@0ax1
0ax1 requested review from mprammer and robert3005 November 4, 2026 12:13
@0ax1
0ax1 merged commit 0151998 into develop Nov 4, 2026
127 of 129 checks passed
@0ax1
0ax1 deleted the ad/cuda-view-slice-mut branch November 4, 2026 12:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog/chore A trivial change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants