Skip to content

Avoid some defensive copies on readonly structs in System.Net.Quic - #101133

Merged
rzikm merged 9 commits into
dotnet:mainfrom
rzikm:quic-no-defensive-copy
Apr 25, 2024
Merged

Avoid some defensive copies on readonly structs in System.Net.Quic#101133
rzikm merged 9 commits into
dotnet:mainfrom
rzikm:quic-no-defensive-copy

Conversation

@rzikm

@rzikm rzikm commented Apr 16, 2024

Copy link
Copy Markdown
Member

This removes defensive copies on readonly structs in S.N.S uncovered by an analyzer from a community member (thanks @hamarb123!).

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@rzikm
rzikm requested a review from a team April 16, 2024 18:28
@MihaZupan

Copy link
Copy Markdown
Member

@MihaZupan MihaZupan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks

@rzikm

rzikm commented Apr 16, 2024

Copy link
Copy Markdown
Member Author

/azp run runtime

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).
Comment thread src/libraries/System.Net.Quic/src/System/Net/Quic/Interop/msquic.cs Outdated
internal unsafe partial struct QUIC_BUFFER
{
public Span<byte> Span => new(Buffer, (int)Length);
public readonly Span<byte> Span => new(Buffer, (int)Length);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't remember exactly, but if this is generated code from MsQuic, it should be fixed there as well.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

right, I thought this was added manually by us (since the QUIC_BUFFER definition is in another auto-generated file). I will follow up to make the change in their repo as well.

rzikm and others added 2 commits April 17, 2024 11:33
…uicConfiguration.Cache.cs

Co-authored-by: Marie Píchová <11718369+ManickaP@users.noreply.github.com>
@rzikm

rzikm commented Apr 22, 2024

Copy link
Copy Markdown
Member Author

/azp run runtime-libraries-coreclr outerloop

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).
Comment thread src/libraries/System.Net.Quic/src/System/Net/Quic/Internal/ReceiveBuffers.cs Outdated

namespace Microsoft.Quic
{
internal partial struct QUIC_SETTINGS : System.IEquatable<QUIC_SETTINGS>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why is this needed? Is QUIC_SETTINGS being put into a Dictionary somewhere or something?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

QUIC_SETTINGS is used as part of the cache key for MsQuicConfigurationHandle

/// It allows delegating MsQuic events to the managed object while it still can be collected and finalized.
/// </summary>
private readonly GCHandle _context;
private GCHandle _context;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Unintended leftover change?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This one should stay, otherwise the .Free() is executed on a defensive copy

It probably is not a big deal since ReleaseHandle runs exactly once, but we still make sure to cleanup dangling pointers.

@ManickaP ManickaP left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nits, otherwise LGTM.

@CarnaViire CarnaViire left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@rzikm

rzikm commented Apr 25, 2024

Copy link
Copy Markdown
Member Author

CI failures are unrelated.

@rzikm
rzikm merged commit aa07770 into dotnet:main Apr 25, 2024
matouskozak pushed a commit to matouskozak/runtime that referenced this pull request Apr 30, 2024
…otnet#101133)

* Avoid some defensive copies on readonly structs in System.Net.Quic

* Keep readonly-ness of CacheKey

* Apply suggestions from code review

* Remove ReadOnlySpan property

* Update src/libraries/System.Net.Quic/src/System/Net/Quic/Internal/MsQuicConfiguration.Cache.cs

Co-authored-by: Marie Píchová <11718369+ManickaP@users.noreply.github.com>

* Code review feedback

* Implement IEquattable for QUIC_SETTINGS

* More code review feedback

* Code review feedback

---------

Co-authored-by: Marie Píchová <11718369+ManickaP@users.noreply.github.com>
michaelgsharp pushed a commit to michaelgsharp/runtime that referenced this pull request May 9, 2024
…otnet#101133)

* Avoid some defensive copies on readonly structs in System.Net.Quic

* Keep readonly-ness of CacheKey

* Apply suggestions from code review

* Remove ReadOnlySpan property

* Update src/libraries/System.Net.Quic/src/System/Net/Quic/Internal/MsQuicConfiguration.Cache.cs

Co-authored-by: Marie Píchová <11718369+ManickaP@users.noreply.github.com>

* Code review feedback

* Implement IEquattable for QUIC_SETTINGS

* More code review feedback

* Code review feedback

---------

Co-authored-by: Marie Píchová <11718369+ManickaP@users.noreply.github.com>
@karelz karelz added this to the 9.0.0 milestone May 14, 2024
Ruihan-Yin pushed a commit to Ruihan-Yin/runtime that referenced this pull request May 30, 2024
…otnet#101133)

* Avoid some defensive copies on readonly structs in System.Net.Quic

* Keep readonly-ness of CacheKey

* Apply suggestions from code review

* Remove ReadOnlySpan property

* Update src/libraries/System.Net.Quic/src/System/Net/Quic/Internal/MsQuicConfiguration.Cache.cs

Co-authored-by: Marie Píchová <11718369+ManickaP@users.noreply.github.com>

* Code review feedback

* Implement IEquattable for QUIC_SETTINGS

* More code review feedback

* Code review feedback

---------

Co-authored-by: Marie Píchová <11718369+ManickaP@users.noreply.github.com>
@github-actions github-actions Bot locked and limited conversation to collaborators Jun 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.