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

Make classes sealed, methods static & avoid array allocations where possible, dispose disposable, other optimizations & clean up usings #687

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace Castle.Components.DictionaryAdapter
using System;
using System.Linq;
using System.Collections;
using System.Reflection;

/// <summary>
/// Removes a property if matches value.
Expand Down Expand Up @@ -83,7 +82,7 @@ private static TBase Construct<TBase>(Type type, string paramName)
var constructor = type.GetConstructor(Type.EmptyTypes);
if (constructor != null)
{
return (TBase)constructor.Invoke(new object[0]);
return (TBase)constructor.Invoke(Array.Empty<object>());
}
}

Expand All @@ -94,7 +93,7 @@ private static TBase Construct<TBase>(Type type, string paramName)

#region Nested Class: ValueCondition

class ValueCondition : ICondition
sealed class ValueCondition : ICondition
{
private readonly object[] values;
private readonly IEqualityComparer comparer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public RemoveIfEmptyAttribute()

private new Type Condition { get; set; }

class RemoveIfEmptyCondition : ICondition
sealed class RemoveIfEmptyCondition : ICondition
{
public static readonly RemoveIfEmptyCondition Instance = new RemoveIfEmptyCondition();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ namespace Castle.Components.DictionaryAdapter
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Text;

/// <summary>
Expand Down Expand Up @@ -109,7 +108,7 @@ internal static string BuildString(IEnumerable enumerable, char separator)

#region Nested Class: StringList

class StringListWrapper<T> : IList<T>
sealed class StringListWrapper<T> : IList<T>
{
private readonly string key;
private readonly char separator;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected XPathFunctionAttribute() { }
public virtual int Minargs { get { return ArgTypes.Length; } }

public static readonly XPathResultType[]
NoArgs = new XPathResultType[0];
NoArgs = Array.Empty<XPathResultType>();

public abstract object Invoke(XsltContext context, object[] args, XPathNavigator node);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ namespace Castle.Components.DictionaryAdapter
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;

public abstract partial class DictionaryAdapterBase
{
Expand Down Expand Up @@ -223,7 +222,7 @@ protected void AddEditDependency(IEditableObject editDependency)

#region Nested Class: SuppressEditingScope

class SuppressEditingScope : IDisposable
sealed class SuppressEditingScope : IDisposable
{
private readonly DictionaryAdapterBase adapter;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected TrackPropertyChangeScope TrackReadonlyPropertyChanges()
return readOnlyTrackingScope = new TrackPropertyChangeScope(this);
}

private class NotificationSuppressionScope : IDisposable
private sealed class NotificationSuppressionScope : IDisposable
{
private readonly DictionaryAdapterBase adapter;

Expand All @@ -127,7 +127,7 @@ public class TrackPropertyChangeScope : IDisposable
private readonly DictionaryAdapterBase adapter;
private readonly PropertyDescriptor property;
private readonly object existingValue;
private Dictionary<PropertyDescriptor, object> readOnlyProperties;
private readonly Dictionary<PropertyDescriptor, object> readOnlyProperties;

public TrackPropertyChangeScope(DictionaryAdapterBase adapter)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ namespace Castle.Components.DictionaryAdapter
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;
using System.Diagnostics;

using Castle.Components.DictionaryAdapter.Xml;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,6 @@ private static IDictionary<string, PropertyDescriptor> MergeProperties(
}

private static readonly IDictionaryInitializer[]
NoInitializers = { };
NoInitializers = Array.Empty<IDictionaryInitializer>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public override bool IsReadOnly

public override bool Contains(object key)
{
return dictionary.Keys.Contains(GetKey(key));
return dictionary.ContainsKey(GetKey(key));
}

public override object this[object key]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Castle.Components.DictionaryAdapter
public class MemberwiseEqualityHashCodeStrategy : DictionaryBehaviorAttribute,
IDictionaryEqualityHashCodeStrategy, IDictionaryInitializer, IEqualityComparer<IDictionaryAdapter>
{
class HashCodeVisitor : AbstractDictionaryAdapterVisitor
sealed class HashCodeVisitor : AbstractDictionaryAdapterVisitor
{
private int hashCode;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class PropertyDescriptor : IDictionaryKeyBuilder, IDictionaryPropertyGett
private Dictionary<object, object> extendedProperties;
protected List<IDictionaryBehavior> dictionaryBehaviors;

private static readonly object[] NoAnnotations = new object[0];
private static readonly object[] NoAnnotations = Array.Empty<object>();

/// <summary>
/// Initializes an empty <see cref="PropertyDescriptor"/> class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void Initialize(IDictionaryAdapter dictionaryAdapter, object value)
};
}

class SuppressListChangedEvents : IDisposable
sealed class SuppressListChangedEvents : IDisposable
{
private readonly bool raiseEvents;
private readonly System.ComponentModel.BindingList<T> bindingList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -389,14 +389,14 @@ private void HandleNodeRealized(object sender, EventArgs e)
OnRealized();
}

private void AttachObservers(object value, IDictionaryAdapter dictionaryAdapter, PropertyDescriptor property)
private static void AttachObservers(object value, IDictionaryAdapter dictionaryAdapter, PropertyDescriptor property)
{
var bindingList = value as IBindingList;
if (bindingList != null)
bindingList.ListChanged += (s,e) => HandleListChanged(s, e, dictionaryAdapter, property);
}

private void HandleListChanged(object value, ListChangedEventArgs args, IDictionaryAdapter dictionaryAdapter, PropertyDescriptor property)
private static void HandleListChanged(object value, ListChangedEventArgs args, IDictionaryAdapter dictionaryAdapter, PropertyDescriptor property)
{
var change = args.ListChangedType;
var changed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ namespace Castle.Components.DictionaryAdapter.Xml
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Xml;
using System.Xml.Serialization;

Expand Down Expand Up @@ -350,7 +349,7 @@ private XmlMetadata GetXmlMetadata(Type clrType)
.GetXmlMeta();
}

private string GetDefaultTypeLocalName(Type clrType)
private static string GetDefaultTypeLocalName(Type clrType)
{
var name = clrType.Name;
return IsInterfaceName(name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ namespace Castle.Components.DictionaryAdapter.Xml
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

using Castle.Core;
using Castle.Core.Internal;
Expand Down Expand Up @@ -372,7 +370,7 @@ private bool TryGetEntry(IXmlNode node, out Entry entry, out bool reference)
return true;
}

private bool TryGetCompatibleValue(Entry entry, Type type, ref object value)
private static bool TryGetCompatibleValue(Entry entry, Type type, ref object value)
{
var values = entry.Values;
if (values == null)
Expand All @@ -391,8 +389,7 @@ private bool TryGetCompatibleValue(Entry entry, Type type, ref object value)
continue;

if (type.IsAssignableFrom(item.Type))
if (null != candidate)
return Try.Success(out value, candidate);
return Try.Success(out value, candidate);

if (dictionaryAdapter == null)
dictionaryAdapter = candidate as IDictionaryAdapter;
Expand Down Expand Up @@ -483,7 +480,7 @@ private static readonly Type
private static readonly object
CreateEntryToken = new object();

private class Entry
private sealed class Entry
{
public int Id;
public IXmlNode Node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ public bool TryGet(Type clrType, out IXmlIncludedType includedType)
: includedTypes.TryGet(clrType, out includedType);
}

private class DefaultAccessor : XPathBehaviorAccessor
private sealed class DefaultAccessor : XPathBehaviorAccessor
{
private readonly XPathBehaviorAccessor parent;

Expand All @@ -200,7 +200,7 @@ public override void Prepare()
}
}

private class ItemAccessor : XPathBehaviorAccessor
private sealed class ItemAccessor : XPathBehaviorAccessor
{
public ItemAccessor(XPathBehaviorAccessor parent)
: base(parent.ClrType.GetCollectionItemType(), parent.Context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public override IXmlCursor SelectPropertyNode(IXmlNode node, bool mutable)
return node.SelectChildren(this, Context, PropertyFlags.MutableIf(mutable));
}

private class ItemAccessor : XmlNodeAccessor,
private sealed class ItemAccessor : XmlNodeAccessor,
IConfigurable<XmlArrayItemAttribute>,
IXmlBehaviorSemantics <XmlArrayItemAttribute>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public Type GetClrType(XmlElementAttribute attribute)
return attribute.Type;
}

private class ItemAccessor : XmlNodeAccessor
private sealed class ItemAccessor : XmlNodeAccessor
{
public ItemAccessor(XmlNodeAccessor parent)
: base(parent.ClrType.GetCollectionItemType(), parent.Context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected XmlNodeAccessor(string name, Type type, IXmlContext context)
{
if (name == null)
throw Error.ArgumentNull(nameof(name));
if (name == string.Empty)
if (name.Length == 0)
throw Error.InvalidLocalName();

localName = XmlConvert.EncodeLocalName(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Castle.Components.DictionaryAdapter.Xml
using System;
using System.Collections.Generic;

internal class XmlCollectionAdapter<T> : ICollectionAdapter<T>, IXmlNodeSource
internal sealed class XmlCollectionAdapter<T> : ICollectionAdapter<T>, IXmlNodeSource
{
private List<XmlCollectionItem<T>> items;
private List<XmlCollectionItem<T>> snapshot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Castle.Components.DictionaryAdapter.Xml
{
internal class XmlNodeList<T> : ListProjection<T>, IXmlNodeSource
internal sealed class XmlNodeList<T> : ListProjection<T>, IXmlNodeSource
{
public XmlNodeList
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

namespace Castle.Components.DictionaryAdapter.Xml
{
internal class XmlNodeSet<T> : SetProjection<T>, IXmlNodeSource
internal sealed class XmlNodeSet<T> : SetProjection<T>, IXmlNodeSource
{
public XmlNodeSet
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
namespace Castle.Components.DictionaryAdapter.Xml
{
using System;
using System.Xml.XPath;
using System.Xml;

public class SysXmlCursor : SysXmlNode, IXmlCursor
Expand Down Expand Up @@ -478,7 +477,7 @@ private void RequireNoXsiType(IXmlKnownType knownType)
throw Error.CannotSetAttribute(this);
}

private XmlName GetEffectiveName(IXmlKnownType knownType, XmlNode parent)
private static XmlName GetEffectiveName(IXmlKnownType knownType, XmlNode parent)
{
var name = knownType.Name;

Expand Down Expand Up @@ -518,12 +517,12 @@ public void Remove()
RemoveAttribute(removedNode);
}

private void RemoveElement(XmlNode node)
private static void RemoveElement(XmlNode node)
{
node.ParentNode.RemoveChild(node);
}

private void RemoveAttribute(XmlNode node)
private static void RemoveAttribute(XmlNode node)
{
var attribute = (XmlAttribute) node;
attribute.OwnerElement.Attributes.Remove(attribute);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public IList<CompiledXPathNode> Dependencies
}

private static readonly IList<CompiledXPathNode>
NoDependencies = Array.AsReadOnly(new CompiledXPathNode[0]);
NoDependencies = Array.AsReadOnly(Array.Empty<CompiledXPathNode>());

private bool HasNoRealDependencies()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@

namespace Castle.Components.DictionaryAdapter.Xml
{
using System;
using System.Collections.Generic;
using System.Xml.XPath;

internal class XPathBufferedNodeIterator : XPathNodeIterator
internal sealed class XPathBufferedNodeIterator : XPathNodeIterator
{
private readonly IList<XPathNavigator> items;
private int index;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ private enum Token
Error
}

private class Tokenizer
private sealed class Tokenizer
{
private readonly string input;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Castle.Components.DictionaryAdapter.Xml
using System.Xml.XPath;
using System.Xml.Xsl;

internal class XPathContext : XsltContext
internal sealed class XPathContext : XsltContext
{
private readonly XsltContext context;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace Castle.Components.DictionaryAdapter.Xml
using System.Xml;
using System.Xml.XPath;

internal class XPathMutableCursor : XPathNode, IXmlCursor
internal sealed class XPathMutableCursor : XPathNode, IXmlCursor
{
private XPathBufferedNodeIterator iterator;
private CompiledXPathStep step;
Expand Down Expand Up @@ -214,7 +214,7 @@ public void MoveTo(IXmlNode position)
}

public override event EventHandler Realized;
protected virtual void OnRealized()
private void OnRealized()
{
if (Realized != null)
Realized(this, EventArgs.Empty);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ namespace Castle.Components.DictionaryAdapter.Xml
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Xml.Serialization;

internal class XmlTypeSerializerCache : SingletonDispenser<Type, XmlTypeSerializer>
internal sealed class XmlTypeSerializerCache : SingletonDispenser<Type, XmlTypeSerializer>
{
public static readonly XmlTypeSerializerCache
Instance = new XmlTypeSerializerCache();
Expand Down
Loading