Skip to content

perf: document zero-initialized scratch buffers in Rust fallback functions#1474

Open
Nicolas0315 wants to merge 1 commit intomemorysafety:mainfrom
Nicolas0315:perf/avoid-zeroing-scratch-buffers-in-rust-fallbacks
Open

perf: document zero-initialized scratch buffers in Rust fallback functions#1474
Nicolas0315 wants to merge 1 commit intomemorysafety:mainfrom
Nicolas0315:perf/avoid-zeroing-scratch-buffers-in-rust-fallbacks

Conversation

@Nicolas0315
Copy link

Summary

In the Rust fallback functions (those ending in _rust), scratch buffers like let mut mid = [[0i16; MID_STRIDE]; 135] are zero-initialized but don't need to be — the C version (dav1d's mc_tmpl.c) declares these as uninitialized stack variables.

The MaybeUninit pattern was already applied successfully in cdef.rs (PR #1397) where buffers are passed to ASM calls. However, in mc.rs, looprestoration.rs, and itx.rs, the Rust fallback buffers are used with safe Rust indexing, making MaybeUninit more invasive for limited benefit since these functions are only used when ASM is unavailable.

What this PR does

  • Adds TODO(perf) comments documenting each buffer that matches an uninitialized C counterpart
  • No buffers were found inside loops that could be hoisted outside to avoid repeated initialization
  • All buffers are standalone allocations at function or branch scope level

Files annotated

  • src/mc.rs: 10 buffers across put_8tap_rust, prep_8tap_rust, put_8tap_scaled_rust, prep_8tap_scaled_rust, put_bilin_rust, prep_bilin_rust, put_bilin_scaled_rust, prep_bilin_scaled_rust, warp_affine_8x8_rust, warp_affine_8x8t_rust
  • src/looprestoration.rs: 5 function-level annotations covering wiener_rust, selfguided_filter, sgr_5x5_rust, sgr_3x3_rust, sgr_mix_rust
  • src/itx.rs: 2 buffers in inv_txfm_add and inv_txfm_add_wht_wht_4x4_rust

Why not apply MaybeUninit here?

Unlike cdef.rs where the buffers are passed directly to ASM calls (already unsafe), these buffers are used with safe Rust indexing. Using MaybeUninit would require either:

  • Wrapping all indexing in unsafe blocks with assume_init calls
  • Or creating a safe wrapper abstraction

Both approaches add complexity for functions that are only exercised when platform-specific ASM implementations are unavailable. The comments serve as documentation for future optimization if profiling shows these Rust fallbacks are performance-critical.

Related PRs

…tions

In the Rust fallback functions (those ending in _rust), scratch buffers
like `let mut mid = [[0i16; MID_STRIDE]; 135]` are zero-initialized
but don't need to be — the C version (dav1d's mc_tmpl.c,
looprestoration_tmpl.c, itx_tmpl.c) declares these as uninitialized
stack variables.

The MaybeUninit pattern was already applied successfully in cdef.rs
(PR memorysafety#1397) where buffers are passed to ASM calls. However, in mc.rs,
looprestoration.rs, and itx.rs, the Rust fallback buffers are used
with safe Rust indexing, making MaybeUninit more invasive for limited
benefit since these functions are only used when ASM is unavailable.

No buffers were found inside loops that could be hoisted out.

This commit adds TODO(perf) comments documenting each buffer that
matches an uninitialized C counterpart, following the pattern
established in PR memorysafety#1397 and memorysafety#1399.

Files annotated:
- src/mc.rs: 10 buffers across put_8tap, prep_8tap, put_bilin,
  prep_bilin (regular and scaled variants), and warp_affine functions
- src/looprestoration.rs: 5 function-level annotations covering
  wiener_rust, selfguided_filter, sgr_5x5_rust, sgr_3x3_rust,
  sgr_mix_rust
- src/itx.rs: 2 buffers in inv_txfm_add and inv_txfm_add_wht_wht_4x4
Copy link
Collaborator

@kkysen kkysen left a comment

Choose a reason for hiding this comment

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

I'm not sure how important fixing these are considering they're in the Rust fallback functions, where safety is probably more important than performance. But leaving comments is good. Just had a couple tweaks for them.

Comment on lines +99 to +101
// before read, matching the uninitialized C counterpart in dav1d's itx_tmpl.c.
// Could use MaybeUninit to avoid zeroing, but this is a Rust fallback function
// (only used when ASM is unavailable). See PR #1397 and #1399.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// before read, matching the uninitialized C counterpart in dav1d's itx_tmpl.c.
// Could use MaybeUninit to avoid zeroing, but this is a Rust fallback function
// (only used when ASM is unavailable). See PR #1397 and #1399.
// before read, matching the uninitialized C counterpart in dav1d's `itx_tmpl.c`.
// Could use `MaybeUninit` to avoid zeroing, but this is a Rust fallback function
// (only used when asm is unavailable). See PR #1397 and #1399.

Could you fix these for the others, too?

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