Skip to content

Internally identify interceptions by content hash#76344

Merged
RikkiGibson merged 2 commits intodotnet:mainfrom
RikkiGibson:interceptors-dictionary
Dec 12, 2024
Merged

Internally identify interceptions by content hash#76344
RikkiGibson merged 2 commits intodotnet:mainfrom
RikkiGibson:interceptors-dictionary

Conversation

@RikkiGibson
Copy link
Member

Closes #76341

Tests that would be impacted by this change are indicated by ddc9b84 (i.e. the workaround of specifying path in that commit would no longer be necessary.)

@RikkiGibson RikkiGibson requested a review from a team as a code owner December 9, 2024 22:35
@ghost ghost added Area-Compilers untriaged Issues and PRs which have not yet been triaged by a lead labels Dec 9, 2024
@jcouv jcouv self-assigned this Dec 12, 2024
private sealed class InterceptorKeyComparer : IEqualityComparer<(ImmutableArray<byte> ContentHash, int Position)>
{
private InterceptorKeyComparer() { }
public static InterceptorKeyComparer Instance { get; } = new InterceptorKeyComparer();
Copy link
Member

Choose a reason for hiding this comment

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

{ get; }

Consider using a readonly field instead

public int GetHashCode((ImmutableArray<byte> ContentHash, int Position) obj)
{
return Hash.Combine(
BinaryPrimitives.ReadInt32LittleEndian(obj.ContentHash.AsSpan()),
Copy link
Member

@jcouv jcouv Dec 12, 2024

Choose a reason for hiding this comment

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

nit: FWIW, I was curious about this, so did some exploration. I noticed that we have another IEqualityComparer involving Immutable<byte> (see ByteSequenceBoolTupleComparer). It uses Hash.Combine(ByteSequenceComparer.GetHashCode(.... It's used to cache small method bodies.
In contrast, BinaryPrimitives.ReadInt32LittleEndian truncates most of the content hash. We also use that method in other places of the compiler (see ContentHashComparer)
Just wanted to mention it, but it feels like we're okay with this method for interceptors since the bytes are already a content hash. #Resolved

Copy link
Member Author

Choose a reason for hiding this comment

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

I should add a comment that similar implementation is used for InterceptableLocation (the type which holds this data on the "production"/source-generator side)

Copy link
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

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

LGTM Thanks (iteration 1) with a nit to consider

@RikkiGibson RikkiGibson merged commit ecc842b into dotnet:main Dec 12, 2024
@RikkiGibson RikkiGibson deleted the interceptors-dictionary branch December 12, 2024 22:42
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Dec 12, 2024
@dibarbet dibarbet modified the milestones: Next, 17.13 P3 Jan 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area-Compilers Feature - Interceptors untriaged Issues and PRs which have not yet been triaged by a lead

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Interceptors should not internally identify calls using paths

4 participants