question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Consider using reflection to optimize code paths in stock scenarios

See original GitHub issue

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:

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.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
RussKiecommented, Mar 15, 2022

As a disclaimer, this issue come up in our recent backlog triage, but this work is not high on our todo list, and we don’t expect any immediate actions here (at least form our side).

Thank you for your thoughts. I don’t think source generators would really work in this case - they are a “compile-time thing”, and at runtime descendant types may be dynamically loaded or generated (e.g., plugins). The other two approaches in theory sound plausible, though as you said, they carry own risks and limitations. But these may be a good start in the exploration of this problem space.

0reactions
kant2002commented, Mar 15, 2022

I don’t think source generators would really work in this case

We can come up with something which works like ComWrappers. Each library can be responsible for their own drawing. But so far, this is theoretical exercise. Again, seems to be overkill, given that source generators has a price.

Read more comments on GitHub >

github_iconTop Results From Across the Web

C#: Can someone explain the practicalities of reflection?
18 Answers. Reflection provides the ability to determine things and execute code at runtime. You don't have to use it if you don't...
Read more >
Reflection in Native Image
At this point, code can collect information about methods and fields and store them in their own data structures, which are then reflection-free...
Read more >
Reflection in Go: Use cases and tutorial
Reflection lets developers solve some problems by writing less code. However, reflection often affects the readability of your code, and it may ...
Read more >
Chapter 4. Query Performance Optimization
This chapter begins with general query design considerations—the things you should consider first when a query isn't performing well. We then dig much...
Read more >
Optimizing reflection in C# via dynamic code generation
A technical deep dive into C# techniques to make reflection-based code run faster using dynamic code generation and other optimizations.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found