Browse Source

Avoid unneeded allocations in CompiledBindingPath

pull/10478/head
Julien Lebosquain 4 years ago
parent
commit
765bce78d7
No known key found for this signature in database GPG Key ID: 1833CAD10ACC46FD
  1. 16
      src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/CompiledBindings/CompiledBindingPath.cs

16
src/Markup/Avalonia.Markup.Xaml/MarkupExtensions/CompiledBindings/CompiledBindingPath.cs

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Reflection; using System.Reflection;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Data.Core; using Avalonia.Data.Core;
@ -13,13 +12,14 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions.CompiledBindings
{ {
public class CompiledBindingPath public class CompiledBindingPath
{ {
private readonly List<ICompiledBindingPathElement> _elements = new List<ICompiledBindingPathElement>(); private readonly ICompiledBindingPathElement[] _elements;
public CompiledBindingPath() { } public CompiledBindingPath()
=> _elements = Array.Empty<ICompiledBindingPathElement>();
internal CompiledBindingPath(IEnumerable<ICompiledBindingPathElement> bindingPath, object rawSource) internal CompiledBindingPath(ICompiledBindingPathElement[] elements, object rawSource)
{ {
_elements = new List<ICompiledBindingPathElement>(bindingPath); _elements = elements;
RawSource = rawSource; RawSource = rawSource;
} }
@ -78,13 +78,13 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions.CompiledBindings
internal IEnumerable<ICompiledBindingPathElement> Elements => _elements; internal IEnumerable<ICompiledBindingPathElement> Elements => _elements;
internal SourceMode SourceMode => _elements.OfType<IControlSourceBindingPathElement>().Any() internal SourceMode SourceMode => Array.Exists(_elements, e => e is IControlSourceBindingPathElement)
? SourceMode.Control : SourceMode.Data; ? SourceMode.Control : SourceMode.Data;
internal object RawSource { get; } internal object RawSource { get; }
public override string ToString() public override string ToString()
=> string.Concat(_elements); => string.Concat((IEnumerable<ICompiledBindingPathElement>) _elements);
} }
public class CompiledBindingPathBuilder public class CompiledBindingPathBuilder
@ -169,7 +169,7 @@ namespace Avalonia.Markup.Xaml.MarkupExtensions.CompiledBindings
return this; return this;
} }
public CompiledBindingPath Build() => new CompiledBindingPath(_elements, _rawSource); public CompiledBindingPath Build() => new CompiledBindingPath(_elements.ToArray(), _rawSource);
} }
public interface ICompiledBindingPathElement public interface ICompiledBindingPathElement

Loading…
Cancel
Save