Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Move DefaultBinder.CanConvert.cs to shared #23931

Merged
merged 4 commits into from
Apr 30, 2019

Conversation

EgorBo
Copy link
Member

@EgorBo EgorBo commented Apr 12, 2019

Basically it's CoreRT implementation: https://github.com/dotnet/corert/blob/master/src/System.Private.CoreLib/src/System/DefaultBinder.CanConvert.cs

I can remove Primitives enum and fill s_primitiveConversions with raw values like here.

I wrote some relfection-based benchmark, it shows that managed impl is 1-2% faster.

cc @jkotas @marek-safar

@@ -1204,6 +1204,63 @@ private static bool CreateParamOrder(int[] paramOrder, ParameterInfo[] pars, str
return true;
}

// CanChangePrimitive
// This will determine if the source can be converted to the target type
private static bool CanChangePrimitive(Type source, Type target)
Copy link
Member

Choose a reason for hiding this comment

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

I think in Mono it's internal and used from Array code. Can you please check it and make it internal if that's the case?

Copy link
Member Author

Choose a reason for hiding this comment

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

Indeed, thanks! I hope CoreCLR guys don't mind if use internal here

Copy link
Member

Choose a reason for hiding this comment

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

used from Array code. Can you please check it and make it internal if that's the case?

Is it a good idea to have a dependency from Array to DefaultBinder, TypeCode and rest of reflection? It may be a good idea to implement this using ElementTypes to make this more tree-shaking friendly.

Copy link
Member

@filipnavara filipnavara Apr 15, 2019

Choose a reason for hiding this comment

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

It's not necessarily a good idea. It was the only code that implemented primitive type widening check in managed code though (AFAIK). I'd prefer to have the functionality somewhere else and have DefaultBinder call it.

Copy link
Member

Choose a reason for hiding this comment

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

This comment still applies, but it does not prevent merging this.

(source == typeof(UIntPtr) && target == typeof(UIntPtr)))
return true;

Primitives widerCodes = s_primitiveConversions[(int)(Type.GetTypeCode(source))];
Copy link
Member

Choose a reason for hiding this comment

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

This new implementation will have different behavior for enums. The C++ code in DBCanConvertPrimitive called GetSignatureCorElementType that returns VALUETYPE for enums, but GetTypeCode will return the underlying type.

E.g. CanChangePrimitive(typeof(MyEnum), typeof(MyEnum)) returns false today, but it is going to return true with this change.

I think that this should be ok because of none of the callers depend on this, but it would be good if you can double check it too.

Copy link
Member Author

Choose a reason for hiding this comment

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

@jkotas everywhere where this method is used the second argument (Type) is checked for IsPrimitive (which is false for Enum) so this case is not possible.

@EgorBo EgorBo force-pushed the move-default-binder-to-shared branch 2 times, most recently from b0bb4f1 to 07528df Compare April 23, 2019 09:16
@jkotas jkotas merged commit b169f91 into dotnet:master Apr 30, 2019
Dotnet-GitSync-Bot pushed a commit to Dotnet-GitSync-Bot/mono that referenced this pull request Apr 30, 2019
Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
Dotnet-GitSync-Bot pushed a commit to Dotnet-GitSync-Bot/corert that referenced this pull request Apr 30, 2019
Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
Dotnet-GitSync-Bot pushed a commit to Dotnet-GitSync-Bot/corefx that referenced this pull request Apr 30, 2019
Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
marek-safar pushed a commit to mono/mono that referenced this pull request Apr 30, 2019
Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
jkotas pushed a commit to dotnet/corert that referenced this pull request Apr 30, 2019
Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
GrabYourPitchforks pushed a commit to Dotnet-GitSync-Bot/corefx that referenced this pull request May 1, 2019
Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
stephentoub pushed a commit to dotnet/corefx that referenced this pull request May 1, 2019
Signed-off-by: dotnet-bot <dotnet-bot@microsoft.com>
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants