Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#53285 switch to use MemberInfo.MetadataToken #55801

Merged
merged 5 commits into from
Jul 20, 2021
Merged

#53285 switch to use MemberInfo.MetadataToken #55801

merged 5 commits into from
Jul 20, 2021

Conversation

FatTigerWang
Copy link
Contributor

Switch to use MemberInfo.MetadataToken

With #53283, we won't build a netstandard1.x configuration of this library anymore and the code path can be optimized.

Fix #53285

fix code style
remove whitespace to fix SA1028
return false;

if (left.Name != right.Name)
if (left.MetadataToken != right.MetadataToken)
Copy link
Member

@krwq krwq Jul 16, 2021

Choose a reason for hiding this comment

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

Is it possible that left is MethodRef and right is MethodDef? (not sure if those are always resolved) that could potentially produce false where it shouldn't.

Also if the token is equal should it just return true? Could arguments differ for same token?

Copy link
Contributor Author

@FatTigerWang FatTigerWang Jul 16, 2021

Choose a reason for hiding this comment

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

As far as I know, MetadataToken is unique in the same Module, so when MetadataToken is equal, it can return true. In addition, according to documentation, I am not sure whether it is necessary to judge the unexpected situation. Do you have any suggestions? I will change the code later.

delete redundant judgments, only use MetadataToken and Module for judgment
}

return true;
return left.Module.Name == right.Module.Name && left.MetadataToken == right.MetadataToken;
Copy link
Member

@jkotas jkotas Jul 16, 2021

Choose a reason for hiding this comment

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

The modules can have same name, but different identity. This condition would not work correctly in that case.

Should the whole MethodInfoEqualityComparer type be deleted and the dictionary switched to use the default comparer? It is not clear what this implementation is trying to achieve that the default EqualityComparer would not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The default comparator will compare the reference of the object. It cannot be judged correctly when used here, so need to use MethodInfoEqualityComparer

Copy link
Member

@jkotas jkotas Jul 16, 2021

Choose a reason for hiding this comment

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

The default EqualityComparer will call Equals method to compare the objects:

MethodInfo.Equals should do exactly what the existing code is trying to achieve.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did not find information about MethodInfo.Equals override from Object.Equals

@steveharter steveharter requested a review from krwq July 16, 2021 15:38
@FatTigerWang
Copy link
Contributor Author

Any feedback?

@jkotas
Copy link
Member

jkotas commented Jul 19, 2021

It would be useful to understand why a simple MethodInfo.Equals does not work. If it does not work, add comment explaining it. Hopefully, it will also help us figure out how the code can be simplified.

@FatTigerWang
Copy link
Contributor Author

It would be useful to understand why a simple MethodInfo.Equals does not work. If it does not work, add comment explaining it. Hopefully, it will also help us figure out how the code can be simplified.

You are right, MethodInfo.Equals can be judged correctly, so the default EqualityComparer should be used.

use Methodinfo.Equals to judge equality

use Methodinfo.Equals to judge equality and remove special comparator.

Fix #453285
@steveharter
Copy link
Member

You are right, MethodInfo.Equals can be judged correctly, so the default EqualityComparer should be used.

Are there automated tests that verify the original issue and\or that the Equals overload works as expected?

@terrajobst terrajobst added the community-contribution Indicates that the PR has been added by a community member label Jul 19, 2021
@FatTigerWang
Copy link
Contributor Author

You are right, MethodInfo.Equals can be judged correctly, so the default EqualityComparer should be used.

Are there automated tests that verify the original issue and\or that the Equals overload works as expected?

I will check automated tests and add new tests if necessary.

@FatTigerWang
Copy link
Contributor Author

FatTigerWang commented Jul 20, 2021

I have reviewed the existing automated test cases, and I don’t think there is a need to add new test cases for this change.
If MethodInfo.Equals does not work, then the Proxy_Declares_Interface_Events and Invoke_Indexer_Setter_And_Getter_Invokes_Correct_Methods test cases will fail.

@FatTigerWang
Copy link
Contributor Author

I read the code again, MethodInfo.Equals was supported in .NET Standard 1.0, here is just to judge the equality of the methods of a single interface, why was the special EqualityComparer implemented at that time?

@jkotas
Copy link
Member

jkotas commented Jul 20, 2021

The special equality comparer was added in dotnet/corefx#9546

dotnet/corefx#9546 (comment) has more discussion about. I do not think that the arguments in that discussion are valid. In particular, I do not agree that "The default Equality test for MethodInfo is reference equality, not "semantic equality" is true.

@jkotas jkotas merged commit b835c8c into dotnet:main Jul 20, 2021
@jkotas
Copy link
Member

jkotas commented Jul 20, 2021

@FatTigerWang Thank you!

@FatTigerWang FatTigerWang deleted the issue-53285-use-metadatatoken branch July 21, 2021 01:48
@ghost ghost locked as resolved and limited conversation to collaborators Aug 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Reflection community-contribution Indicates that the PR has been added by a community member
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

Optimize DispatchProxyGenerator code path by using MetdataToken
5 participants