Repository navigation
Improve performance of Enum*Windows() interop functions - #2639
Merged
Merged
Conversation
kpreisser
commented
Jan 6, 2020
Contributor
|
MC, please rebase |
…tions. This is done by passing a static delegate to the native function and supplying the callback as reference data. Otherwise, when we would supply the callback directly, the CLR would have to generate a native code block (for storing the closure pointer) for each callback delegate instance.
kpreisser
force-pushed
the
improveEnumWindowsPerformance
branch
from
January 14, 2020 18:22
016b7df to
6fe940e
Compare
Codecov Report
@@ Coverage Diff @@
## master #2639 +/- ##
==================================================
- Coverage 58.15075% 57.58726% -0.5635%
==================================================
Files 1240 1233 -7
Lines 423004 417542 -5462
Branches 38852 38753 -99
==================================================
- Hits 245980 240451 -5529
- Misses 171650 171714 +64
- Partials 5374 5377 +3
|
RussKie
approved these changes
Jan 14, 2020
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Proposed changes
In
EnumWindows,EnumChildWindows, andEnumThreadWindows, instead of passing the supplied callback delegate to the native function, use a (fixed) static delegate, and specify a handle pointer of the supplied callback in thelParamparameter (that is available for storing application-provided data).That way, the CLR no longer needs to allocate memory for each delegate instance in order to store a native code block (that contains the closure pointer), thus improving performance in many cases (see comparison below). (On the other hand, the invoked callback delegate now has a little bit more work to do, by dereferencing a
GCHandleand calling another method.)This can especially improve speed when running x86 applications on ARM64 systems, as it avoids the need for the emulation layer to generate new ARM64 code from the x86 code every time the method is invoked with a new delegate instance.
Note: In
Form.RecreateHandleCorethere was a call toEnumThreadWindowswhere the form handle was passed aslParamdata, but I think it makes more sense to store it in the callback closure since a new instance of the delegate is created anyway.Note: I'm not sure about the naming/placement of the delegate types. In the native declarations, all functions use
WNDENUMPROCas callback type.Customer Impact
EnumWindows,EnumChildWindowsorEnumThreadWindows.In specific cases (when the callback is called very often), performance might be slower due to the additional work the callback has to do.
Regression?
Risk
Test methodology
Performance
EnumChildWindows):Average time before/after this PR:
Note: When the callback is called more often (e.g. about 200 times when calling
EnumWindows), the timing improvement vanishes with a x64 app, probably because the callback delegate has now a bit more work to do.Test environment(s)
.NET Core SDK (reflecting any global.json):
Version: 5.0.100-alpha1-016073
Commit: bd7bf8c6d2
Runtime Environment:
OS Name: Windows
OS Version: 10.0.18363
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\5.0.100-alpha1-016073\
Host (useful for support):
Version: 5.0.0-alpha.1.19564.1
Commit: c77948d92a
.NET Core SDKs installed:
3.1.100 [C:\Program Files\dotnet\sdk]
5.0.100-alpha1-016073 [C:\Program Files\dotnet\sdk]
Microsoft Reviewers: Open in CodeFlow