-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Fast FieldInfo reflection #98199
Fast FieldInfo reflection #98199
Conversation
Tagging subscribers to this area: @dotnet/area-system-reflection Issue DetailsVerifying tests
|
6a3ffc9
to
081f6b3
Compare
src/libraries/System.Private.CoreLib/src/System/Reflection/FieldAccessor.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Reflection/FieldAccessor.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Runtime/tests/System.Reflection.Tests/FieldInfoTests.cs
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
JFYI there is an open question in the PR, otherwise LGTM.
src/mono/System.Private.CoreLib/src/System/RuntimeFieldHandle.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.Private.CoreLib/src/System/Reflection/FieldAccessor.cs
Outdated
Show resolved
Hide resolved
@jkotas I noticed that the runtime wraps exceptions thrown in Unlike invoking a static method, calling GetField\SetField doesn't run any code (fields don't have code) except for the possibility of indirectly calling the class initializer. So unless I hear otherwise, I'll create issue for Mono and NativeAOT to throw UPDATE: there doesn't appear to be hard rules when the class initializer is called other than "when first accessed", so either |
This makes
FieldInfo.SetValue
andGetValue
faster; the most improvements are:The cases for setting a value type in a static field are not improved since they follow the existing "slow path" to the runtime due to special logic including "box into" where we box the value type into an existing boxed object (non-primitive static value types are stored that way) and other special logic such as nullability. There are also other "slow paths" for cases where static variables are not fixed in memory, but these are somewhat rare cases.
This was done without using IL-emit; raw memory operations are used. This means there is no warm-up time and works on platforms that do not support emit.
This helps align field performance with fast property performance work (which is IL-emit based) that was done in v7 and v8. For example:
In the future, the "FieldAccessor" class added here can be exposed publicly and methods added that do not require boxing\unboxing.
New benchmarks were added in dotnet/performance#3863.
Click for benchmarks
<\details>