Skip to content
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

perf(brush): Don't use reflection to invoke brush updates #18899

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

jeromelaban
Copy link
Member

No description provided.

@github-actions github-actions bot added platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform platform/android 🤖 Categorizes an issue or PR as relevant to the Android platform platform/macos 🍏 Categorizes an issue or PR as relevant to the macOS platform platform/ios 🍎 Categorizes an issue or PR as relevant to the iOS platform area/skia ✏️ Categorizes an issue or PR as relevant to Skia labels Nov 22, 2024
@unodevops
Copy link
Contributor

🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-18899/index.html

@unodevops
Copy link
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-18899/index.html

@unodevops
Copy link
Contributor

⚠️⚠️ The build 148209 has failed on Uno.UI - docs.

@unodevops
Copy link
Contributor

⚠️⚠️ The build 148210 has failed on Uno.UI - CI.

#endif

namespace Microsoft.UI.Xaml.Media
{
[TypeConverter(typeof(BrushConverter))]
public partial class Brush : DependencyObject
{
private readonly WeakEventManager _weakEventManager = new();
private List<WeakEventHelper.GenericEventHandler>? _invalidateRenderHandlers;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my previous testing, I recall this was leaking.

I never seen perf issues from the current approach when profiling, and even if there is a perf issue, I feel @dr1rrb's approach is safer.

Copy link
Member Author

@jeromelaban jeromelaban Nov 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either previous approaches have issues, the original one is using reflection and is very slow on wasm (it uses the interpreter), and the used by David uses CWT which is costly as well. I agree that it might on some instances, particularly if instances are not collected, but we can improve that with GCHandles and gen2 GC detection now that we're in net8+.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's problematic on Wasm specifically I think you might consider going with conditional code to use different approach on different platforms?

The main issue with WeakEventHelper is that the WeakReference instances are leaked (which is not detected by When_Add_Remove, still it's a memory leak).

On desktop, I never seen a perf issue for the existing approach, and it doesn't leak. I can't speak of other platforms though as I haven't profiled on them.

The real issue is that there is no good solution for weak events in C#. Everyone is inventing their own solution, and all the solutions have problems.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand you concerns, but yes, it's sufficient, and using reflection for such a critical path is not a good way to handle this for any platform. I agree that the origin of the problem has not been addressed, but every version of the runtime provides its features that provide a new way for handling it.

I'll try with the GCHandle approach, we'll see.

@unodevops
Copy link
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-18899/index.html

@unodevops
Copy link
Contributor

🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-18899/index.html

@github-actions github-actions bot added area/automation Categorizes an issue or PR as relevant to project automation kind/documentation labels Nov 23, 2024
@unodevops
Copy link
Contributor

⚠️⚠️ The build 148245 has failed on Uno.UI - docs.

@unodevops
Copy link
Contributor

⚠️⚠️ The build 148246 has failed on Uno.UI - CI.

@unodevops
Copy link
Contributor

🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-18899/index.html

@unodevops
Copy link
Contributor

🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-18899/index.html

@unodevops
Copy link
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-18899/index.html

@unodevops
Copy link
Contributor

⚠️⚠️ The build 148252 has failed on Uno.UI - CI.

@unodevops
Copy link
Contributor

🤖 Your WebAssembly Sample App stage site is ready! Visit it here: https://unowasmprstaging.z20.web.core.windows.net/pr-18899/index.html

@unodevops
Copy link
Contributor

🤖 Your Docs stage site is ready! Visit it here: https://unodocsprstaging.z13.web.core.windows.net/pr-18899/index.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/automation Categorizes an issue or PR as relevant to project automation area/skia ✏️ Categorizes an issue or PR as relevant to Skia kind/documentation platform/android 🤖 Categorizes an issue or PR as relevant to the Android platform platform/ios 🍎 Categorizes an issue or PR as relevant to the iOS platform platform/macos 🍏 Categorizes an issue or PR as relevant to the macOS platform platform/wasm 🌐 Categorizes an issue or PR as relevant to the WebAssembly platform
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants