Skip to content

Change ref parameters to ref readonly for read-only cases - #99

Merged
HMBSbige merged 2 commits into
masterfrom
copilot/update-ref-to-readonly
Oct 28, 2025
Merged

HMBSbige merged 2 commits into
masterfrom
copilot/update-ref-to-readonly

Conversation

Copilot AI commented Oct 28, 2025 •

Copy link
Copy Markdown
Contributor

Several methods pass parameters by ref when they're only read, not modified. This obscures intent and prevents potential compiler optimizations.

Changes

  • Poly1305X86.cs: MultiplyR(ref Vector128<uint> h01, h23, h44, ...) → ref readonly for the three input vectors used only in multiplication operations
  • ChaCha20Utils.cs: Step(ref uint a, ref uint b, ref uint c, ...) → ref readonly uint b as it's only used in a += b

Example

// Before
private void MultiplyR(ref Vector128<uint> h01, ref Vector128<uint> h23, ref Vector128<uint> h44, 
    out ulong d0, ...)
{
    Vector128<ulong> t00 = Sse2.Multiply(h01, _r0s4);  // Only reading h01
    // ...
}

// After  
private void MultiplyR(ref readonly Vector128<uint> h01, ref readonly Vector128<uint> h23, 
    ref readonly Vector128<uint> h44, out ulong d0, ...)

Note: BroadcastVector128ToVector256 cannot use ref readonly as Unsafe.AsPointer requires mutable references even for read-only operations.

Original prompt

检查所有 ref,有些 ref 其实是只读的,改成 ref readonly


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: HMBSbige <19153265+HMBSbige@users.noreply.github.com>
Copilot AI changed the title [WIP] Update refs to be readonly Change ref parameters to ref readonly for read-only cases Oct 28, 2025
Copilot AI requested a review from HMBSbige October 28, 2025 17:48
@HMBSbige

Copy link
Copy Markdown
Owner

@copilot 肯定还有很多,检查所有文件,尤其是局部变量

Copilot AI commented Oct 28, 2025

Copy link
Copy Markdown
Contributor Author

@copilot 肯定还有很多,检查所有文件,尤其是局部变量

I've analyzed all files for additional ref to ref readonly conversions.

Method parameters: The existing changes (Poly1305X86.cs MultiplyR and ChaCha20Utils.cs Step) are the main opportunities for method parameters that can be made ref readonly.

Local variables limitation: Most local ref variables in the codebase use Unsafe.Add(), Unsafe.As(), or GetReference() which require mutable ref even when only reading. C#'s Unsafe APIs don't support ref readonly in these contexts.

For example, this pattern cannot use ref readonly:

ref byte streamRef = ref stream.GetReference();  // Can't be ref readonly
ref Vector512<byte> v0 = ref Unsafe.As<byte, Vector512<byte>>(ref Unsafe.Add(ref streamRef, i));  // Needs mutable streamRef

Could you clarify which specific local variables you'd like changed? I can look for other method parameters that might have been missed.

@HMBSbige
HMBSbige marked this pull request as ready for review October 28, 2025 18:07
@HMBSbige
HMBSbige merged commit 498d64c into master Oct 28, 2025
13 checks passed
@HMBSbige
HMBSbige deleted the copilot/update-ref-to-readonly branch October 28, 2025 18:08
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