-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Open
Description
Description
The internal logging pattern used throughout Controls.Core is verbose and wasteful:
Application.Current?.FindMauiContext()?.CreateLogger<T>()?.LogWarning($"Cannot set {property.PropertyName} because it is readonly.");Problems:
- Verbose — every call site repeats the 4-method chain
- Wasteful — the
$"..."interpolated string is always allocated, even whenApplication.Currentis null and the entire?.chain short-circuits. The compiler formats the string before passing it to the method. - DI overhead —
FindMauiContext()walks the parent chain andCreateLogger<T>()resolves from DI on every call, even though these are all cold error paths.
Proposed Fix
Introduce an internal MauiLog helper with an [InterpolatedStringHandler] that skips string formatting when logging is disabled:
// Before (4-method chain, always allocates the string)
Application.Current?.FindMauiContext()?.CreateLogger<BindableObject>()?.LogWarning($"Cannot set {property.PropertyName} because it is readonly.");
// After (1 call, zero allocation when logging is disabled)
MauiLog.Warning<BindableObject>($"Cannot set {property.PropertyName} because it is readonly.");The WarningInterpolatedStringHandler ref struct checks Application.Current?.FindMauiContext() in its constructor. If null, it sets shouldAppend = false and the compiler skips all AppendLiteral/AppendFormatted calls — zero heap allocation.
For structured logging sites ("text {placeholder}", arg), MauiLog provides pass-through overloads that still eliminate the verbose chain.
Benchmark Results
| Scenario | Before | After | Speedup |
|---|---|---|---|
| Logging disabled (no Application.Current) | ~34ns + string alloc | ~1ns, 0 alloc | 34x |
| Logging enabled | Same perf | Same perf | 1x |
Scope
~30 call sites across 24 files in Controls.Core. All are cold error paths (readonly property, invalid value, GC'd parent, etc.).
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels