Skip to content

Commit

Permalink
perf: Add the ability to disable SetTargetValue exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Sep 14, 2021
1 parent dc0bc5f commit 9eb4efa
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 31 deletions.
92 changes: 61 additions & 31 deletions src/Uno.UI/DataBinding/BindingExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,44 +539,56 @@ void IValueChangedListener.OnValueChanged(object o)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void SetTargetValueForXBindSelector()
{
try
void SetTargetValue()
{
var canSetTarget = _updateSources?.None(s => s.ValueType == null) ?? true;

if (canSetTarget)
{
SetTargetValueSafe(ParentBinding.XBindSelector(DataContext));
}
else
{
// x:Bind failed bindings don't change the target value
// if no FallbackValue was specified.
ApplyFallbackValue(useTypeDefaultValue: false);

if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
{
this.Log().Debug($"Binding path does not provide a value [{TargetPropertyDetails}] on [{_targetOwnerType}], using fallback value");
}
}
}

if (FeatureConfiguration.BindingExpression.HandleSetTargetValueExceptions)
{
/// <remarks>
/// This method contains or is called by a try/catch containing method and
/// can be significantly slower than other methods as a result on WebAssembly.
/// See https://github.com/dotnet/runtime/issues/56309
/// </remarks>
void SetTargetValue()
void SetTargetValueWithTry()
{
var canSetTarget = _updateSources?.None(s => s.ValueType == null) ?? true;

if (canSetTarget)
try
{
SetTargetValueSafe(ParentBinding.XBindSelector(DataContext));
SetTargetValue();
}
else
catch (Exception e)
{
// x:Bind failed bindings don't change the target value
// if no FallbackValue was specified.
ApplyFallbackValue(useTypeDefaultValue: false);
ApplyFallbackValue();

if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Error))
{
this.Log().Debug($"Binding path does not provide a value [{TargetPropertyDetails}] on [{_targetOwnerType}], using fallback value");
this.Log().Error("Failed to apply binding to property [{0}] on [{1}] ({2})".InvariantCultureFormat(TargetPropertyDetails, _targetOwnerType, e.Message), e);
}
}
}

SetTargetValue();
SetTargetValueWithTry();
}
catch (Exception e)
else
{
ApplyFallbackValue();

if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Error))
{
this.Log().Error("Failed to apply binding to property [{0}] on [{1}] ({2})".InvariantCultureFormat(TargetPropertyDetails, _targetOwnerType, e.Message), e);
}
SetTargetValue();
}
}

Expand All @@ -603,23 +615,41 @@ private void SetTargetValueSafe(object v, bool useTargetNullValue)
);
}

try
{
InnerSetTargetValueSafe(v, useTargetNullValue);
}
catch (Exception e)
if (FeatureConfiguration.BindingExpression.HandleSetTargetValueExceptions)
{
if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Error))
SetTargetValueSafeWithTry(v, useTargetNullValue);

/// <remarks>
/// This method contains or is called by a try/catch containing method and
/// can be significantly slower than other methods as a result on WebAssembly.
/// See https://github.com/dotnet/runtime/issues/56309
/// </remarks>
void SetTargetValueSafeWithTry(object v, bool useTargetNullValue)
{
this.Log().Error("Failed to apply binding to property [{0}] on [{1}] ({2})".InvariantCultureFormat(TargetPropertyDetails, _targetOwnerType, e.Message), e);
}
try
{
InnerSetTargetValueSafe(v, useTargetNullValue);
}
catch (Exception e)
{
if (this.Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Error))
{
this.Log().Error("Failed to apply binding to property [{0}] on [{1}] ({2})".InvariantCultureFormat(TargetPropertyDetails, _targetOwnerType, e.Message), e);
}

ApplyFallbackValue();
}
ApplyFallbackValue();
}
#if !HAS_EXPENSIVE_TRYFINALLY // Try/finally incurs a very large performance hit in mono-wasm - https://github.com/dotnet/runtime/issues/50783
finally
finally
#endif
{
_IsCurrentlyPushing = false;
}
}
}
else
{
InnerSetTargetValueSafe(v, useTargetNullValue);
_IsCurrentlyPushing = false;
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/Uno.UI/FeatureConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,15 @@ public static class Binding
public static bool IgnoreINPCSameReferences { get; set; } = false;
}

public static class BindingExpression
{
/// <summary>
/// Skips the BindingExpression.SetTargetValue exception handling. Can be disabled to
/// improve application performance on WebAssembly. See See #7005 for additional details.
/// </summary>
public static bool HandleSetTargetValueExceptions { get; set; } = true;
}

public static class Popup
{
#if __ANDROID__
Expand Down

0 comments on commit 9eb4efa

Please sign in to comment.