-
Notifications
You must be signed in to change notification settings - Fork 762
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: Improve nullability checking in DependencyProperty
Move nullability checking to use `HashTableEx` to avoid JIT/Generics costs when initializing DependencyProperty nullability validation. The original implementation is in Uno.Core, but uses ConcurrentDictionary, which is particularly expensive to initialize in Jitted and Full AOT environments.
- Loading branch information
1 parent
fbf79fb
commit 5f50f1f
Showing
2 changed files
with
48 additions
and
1 deletion.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
src/Uno.UI/UI/Xaml/DependencyProperty.Dictionary.TypeNullableDictionary.cs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#nullable enable | ||
|
||
using System; | ||
using Uno.Collections; | ||
using Uno.UI.Helpers; | ||
using Uno.Extensions; | ||
|
||
namespace Windows.UI.Xaml | ||
{ | ||
public sealed partial class DependencyProperty | ||
{ | ||
private readonly static TypeNullableDictionary _isTypeNullableDictionary = new TypeNullableDictionary(); | ||
|
||
private bool GetIsTypeNullable(Type type) | ||
{ | ||
if(!_isTypeNullableDictionary.TryGetValue(type, out var isNullable)) | ||
{ | ||
_isTypeNullableDictionary.Add(type, isNullable = type.IsNullable()); | ||
} | ||
|
||
return isNullable; | ||
} | ||
|
||
private class TypeNullableDictionary | ||
{ | ||
private readonly HashtableEx _entries = new HashtableEx(FastTypeComparer.Default); | ||
|
||
internal bool TryGetValue(Type key, out bool result) | ||
{ | ||
if (_entries.TryGetValue(key, out var value)) | ||
{ | ||
result = (bool)value!; | ||
return true; | ||
} | ||
|
||
result = false; | ||
return false; | ||
} | ||
|
||
internal void Add(Type key, bool isNullable) | ||
=> _entries.Add(key, isNullable); | ||
|
||
internal void Clear() | ||
=> _entries.Clear(); | ||
} | ||
} | ||
} |
This file contains 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