dotnet / dotnet/winforms

Consider using reflection to optimize code paths in stock scenarios

Open
#1,441 5 comments 2 reactions 0 assignees View on GitHub
enhancement tenet-performance
Dominant language
C#
Stars
4.9k
Forks
1.1k
Avg merge
1d 13m
Merged PRs (30d)
85

Description

WinForms is almost completely configurable but that comes at a performance cost. When we get windows messages (`WM_*`) we create event classes and jump through a lot of hoops that would not be necessary in a closed system.

For one example, getting a `WM_ERASEBKGND` often ends up being a `FillRect` on the passed in `HDC`. We could potentially avoid a lot of hoop jumping some allocations if we knew the `OnPaintBackground` was completely under our control.

We would pay up front for this sort of check, but could potentially earn it back quite quickly. The following code takes a couple of milliseconds on class creation:

``` C#
Type type = GetType();
_isTypeInAssembly = type.Assembly == typeof(Control).Assembly;

_isOnPaintBackgroundLocal = _isTypeInAssembly
|| type.GetMethod(
"OnPaintBackground",
BindingFlags.NonPublic | BindingFlags.Instance,
null,
new Type[] { typeof(PaintEventArgs) }, null
).DeclaringType.Assembly == typeof(Control).Assembly;
```

If we find measurable situations where we go "if only we knew this wasn't customized" this may be a strategy to attempt.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.