Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 27, 2026

Description

Replaces unsafe code and manual SIMD vectorization in ManagedWebSocket.ApplyMask with TensorPrimitives.Xor.

Changes

  • Added System.Numerics.Tensors project reference to System.Net.WebSockets.csproj
  • Removed unsafe keyword and all pointer operations from ApplyMask
  • Replaced manual Vector<byte> loop with TensorPrimitives.Xor<int>(toMaskInts, rolledMask, toMaskInts)
  • Simplified tail byte processing using foreach ref instead of pointer arithmetic

Before

private static unsafe int ApplyMask(Span<byte> toMask, int mask, int maskIndex)
{
    fixed (byte* toMaskBeg = &MemoryMarshal.GetReference(toMask))
    {
        // Manual Vector<byte> loop + pointer arithmetic (~45 lines)
    }
}

After

private static int ApplyMask(Span<byte> toMask, int mask, int maskIndex)
{
    Span<int> toMaskInts = MemoryMarshal.Cast<byte, int>(toMask);
    TensorPrimitives.Xor(toMaskInts, rolledMask, toMaskInts);
    // Tail bytes (~30 lines total)
}
Original prompt

ManagedWebSocket.cs has a ApplyMask(Span<byte> toMask, int mask, int maskIndex) method that uses unsafe code and manual vectorization. Try to simplify the implementation by calling the TensorPrimitives.Xor helper instead. Likely Xor<T>(ReadOnlySpan<T>, T, Span<T>)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @karelz, @dotnet/ncl
See info in area-owners.md if you want to be subscribed.

Copilot AI and others added 2 commits January 27, 2026 15:20
…orization

Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
<ProjectReference Include="$(LibrariesProjectRoot)System.Memory\src\System.Memory.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Net.Primitives\src\System.Net.Primitives.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Numerics.Vectors\src\System.Numerics.Vectors.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Numerics.Tensors\src\System.Numerics.Tensors.csproj" />
Copy link
Member

@jkotas jkotas Jan 27, 2026

Choose a reason for hiding this comment

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

System.Net.WebSockets is in the shared framework. This reference would require including System.Numerics.Tensors in shared framework as well. Is the extra baseline code bloat really worth it?

Copy link
Member

Choose a reason for hiding this comment

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

From just the WebSockets perspective I don't think we'd care, we can live with the current setup. I was curious what copilot would do in this case.

But if there are more places where we're duplicating such logic it might be worth considering? Linq's Min/Average/Sum implementations come to mind where we have some SIMD sprinkled around (I haven't looked if they could actually be replaced).

cc: @tannergooding @stephentoub In case you've already put any thought into whether it could make sense to use Tensor helpers in other libraries.

Copilot AI changed the title [WIP] Simplify ApplyMask method by using TensorPrimitives.Xor Simplify ManagedWebSocket.ApplyMask using TensorPrimitives.Xor Jan 27, 2026
Copilot AI requested a review from MihaZupan January 27, 2026 15:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants