Repository navigation
Description
Summary of the new feature/enhancement
When assigning a struct to a variable, some checks are done to determine if the struct is mutable. The current code path checks if there are:
- Any public instance fields
- Any public instance properties with a set accessor
- Any public instance methods
This can be simplified by checking if the struct is decorated with the IsReadOnlyAttribute. This would allow PS to assume any struct that wasn't declared explicitly as read only with the C# readonly modifier (SharpLab example) to be mutable. This would also probably result in less false positives because most structs have a public field (even if initonly) or a public method, even these explicitly readonly structs.
Proposed technical implementation details (optional)
This method:
PowerShell/src/System.Management.Automation/engine/runtime/Binding/Binders.cs
Lines 2030 to 2063 in 947bddf
| internal static bool IsValueTypeMutable(Type type) | |
| { | |
| if (type.IsPrimitive || type.IsEnum) | |
| { | |
| return false; | |
| } | |
| // If there are any fields, the type is mutable. | |
| if (type.GetFields(BindingFlags.Public | BindingFlags.Instance).Any()) | |
| { | |
| return true; | |
| } | |
| // If there are any properties with setters, the type is mutable. | |
| var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance); | |
| for (int index = 0; index < properties.Length; index++) | |
| { | |
| var property = properties[index]; | |
| if (property.CanWrite) | |
| { | |
| return true; | |
| } | |
| } | |
| // If there are any methods other than the property getters, the type might | |
| // be mutable, so assume the type is mutable. | |
| var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance); | |
| if (methods.Length != properties.Length) | |
| { | |
| return true; | |
| } | |
| return false; | |
| } |
Should be changed like this:
diff --git a/src/System.Management.Automation/engine/runtime/Binding/Binders.cs b/src/System.Management.Automation/engine/runtime/Binding/Binders.cs
index b54fbdfd5..7a0254ccb 100644
--- a/src/System.Management.Automation/engine/runtime/Binding/Binders.cs
+++ b/src/System.Management.Automation/engine/runtime/Binding/Binders.cs
@@ -2034,32 +2034,7 @@ namespace System.Management.Automation.Language
return false;
}
- // If there are any fields, the type is mutable.
- if (type.GetFields(BindingFlags.Public | BindingFlags.Instance).Any())
- {
- return true;
- }
-
- // If there are any properties with setters, the type is mutable.
- var properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
- for (int index = 0; index < properties.Length; index++)
- {
- var property = properties[index];
- if (property.CanWrite)
- {
- return true;
- }
- }
-
- // If there are any methods other than the property getters, the type might
- // be mutable, so assume the type is mutable.
- var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance);
- if (methods.Length != properties.Length)
- {
- return true;
- }
-
- return false;
+ return !type.IsDefined<System.Runtime.CompilerServices.IsReadOnlyAttribute>();
}
private static readonly ConcurrentDictionary<Type, bool> s_mutableValueTypesWithInstanceMembers =Related issue #12411
Activity
iSazonov commentedon Apr 23, 2020
We need to compare performance - IsDefined() method may be too costly.
SeeminglyScience commentedon Apr 23, 2020
True, though I would be very surprised if it was slower than all of the other reflection calls it would replace.
iSazonov commentedon Apr 23, 2020
I don't see using IsReadOnlyAttribute in .Net Runtime repo https://github.com/dotnet/runtime/search?l=C%23&q=IsReadOnlyAttribute
SeeminglyScience commentedon Apr 23, 2020
Check out the right hand side of this SharpLab example. When a struct has the
readonlymodifier applied to it, it is decorated with that attribute.SeeminglyScience commentedon Apr 23, 2020
Eh complicating it a bit is that the attribute isn't in
netstandard2.0. Libraries that target netstandard2.0 will turn this:into
That makes it a little bit more tricky, and may need to match based on type full name:
That makes performance more of a concern, but it still replaces a lot of reflection calls so hopefully performance is very similar if not better. Either way, it's cached statically per type so I believe it's worth it.
iSazonov commentedon Apr 23, 2020
.Net runtime does not address the issue in IsDefine()?
SeeminglyScience commentedon Apr 23, 2020
So because the attribute isn't in netstandard2.0, roslyn adds the code to define it as an internal type within the library declaring a readonly struct.
So if my module declares a readonly struct while targeting netstandard2.0, it'll be decorated with
System.Runtime.CompilerServices.IsReadOnlyAttribute, MyModuleAssemblynotSystem.Runtime.CompilerServices.IsReadOnlyAttribute, System.Private.CoreLibiSazonov commentedon Apr 23, 2020
I guess IsDefine() knows about this?
SeeminglyScience commentedon Apr 23, 2020
Hmm not sure what you mean. Can't use that method because it only takes a
Typeobject, which has assembly identity built into it.iSazonov commentedon Apr 23, 2020
I guess IsDefined() understand custom IsReadOnlyAttribute() and I hope we have no need a workaround.
iSazonov commentedon Apr 23, 2020
SeeminglyScience commentedon Apr 23, 2020
That's a different attribute from
IsReadOnlyAttribute.If
IsDefinedaccounted for this scenario it would be lying, and would lose a ton of performance for the typical case. If I pass it atyperefofSystem.Runtime.CompilerServices.IsReadOnlyAttribute, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798eand it returns true forSystem.Runtime.CompilerServices.IsReadOnlyAttribute, MyRandomAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=then that would be pretty confusing for any attribute outside of these edge cases. Plus, it would have to just do a full name compare like we're doing here.iSazonov commentedon Apr 23, 2020
In the case I wonder why the attribute is automatically defined for <netstandard2.0.
4 remaining items
SeeminglyScience commentedon Apr 24, 2020
Nah you can mark it up for grabs.
🔧 Improve detection of mutable value types
(#12430) Improve detection of mutable value types (#12495)
(PowerShell#12430) Improve detection of mutable value types (PowerShe…