Skip to content

Commit

Permalink
Use Array.Empty instead of zero-length allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
hyazinthh committed Jun 6, 2024
1 parent 52b8fe8 commit 6ced45e
Show file tree
Hide file tree
Showing 18 changed files with 110 additions and 109 deletions.
2 changes: 1 addition & 1 deletion src/Aardvark.Base.IO/Annotations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal readonly struct Annotated<T>
{
private readonly T m_value;
private readonly IEnumerable<Annotation> m_tags;
private static readonly IEnumerable<Annotation> s_empty = new Annotation[0];
private static readonly IEnumerable<Annotation> s_empty = System.Array.Empty<Annotation>();

public Annotated(T value)
{
Expand Down
30 changes: 15 additions & 15 deletions src/Aardvark.Base.IO/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public Convertible(string descriptor, object data)
#region Properties

public string Descriptor { get { return m_descriptor; } }
public object Data

public object Data
{
get { return m_data; }
set { m_data = value; }
Expand All @@ -76,7 +76,7 @@ public static Convertible FromFile(string fileName)
}

/// <summary>
/// Creates a file convertible given a file name and a preferrable target used to
/// Creates a file convertible given a file name and a preferrable target used to
/// determine the shortest possible path (a matching creator must be registered).
/// </summary>
public static Convertible FromFile(string fileName, Convertible preferrableTarget)
Expand All @@ -103,8 +103,8 @@ public static Convertible FindMatchingRaw(Convertible c)
}

/// <summary>
/// Creates a matching raw convertible from a given convertible and a preferrable target
/// used to the determine the shortest possible path (a matching raw convertible creator
/// Creates a matching raw convertible from a given convertible and a preferrable target
/// used to the determine the shortest possible path (a matching raw convertible creator
/// must be registered).
/// </summary>
public static Convertible FindMatchingRaw(Convertible c, Convertible preferrableTarget)
Expand All @@ -113,8 +113,8 @@ public static Convertible FindMatchingRaw(Convertible c, Convertible preferrable
}

/// <summary>
/// Creates a matching raw convertible from a given convertible and a preferrable target
/// used to the determine the shortest possible path (a matching raw convertible creator
/// Creates a matching raw convertible from a given convertible and a preferrable target
/// used to the determine the shortest possible path (a matching raw convertible creator
/// must be registered).
/// </summary>
public static Convertible FindMatchingRaw(Convertible c, string preferrableTargetDescriptor)
Expand Down Expand Up @@ -169,7 +169,7 @@ public Convertible ConvertedTo(Convertible target)
#endregion

#region Query Methods

/// <summary>
/// Returns whether a convertible is directly convertible into the target.
/// </summary>
Expand Down Expand Up @@ -398,14 +398,14 @@ public void Convert(Convertible source, Convertible target)
/// Private helper for public Convert method above.
/// </summary>
private void Convert(
Convertible source, Convertible target,
Convertible source, Convertible target,
Dictionary<string, Dictionary<string, RoutingEntry>> routingMap,
List<Convertible> tempConvertibles)
{
var sourceDescr = source.Descriptor;
var targetDescr = target.Descriptor;

if (sourceDescr == targetDescr)
if (sourceDescr == targetDescr)
{
DirectConvert(source, target);
return;
Expand Down Expand Up @@ -1029,7 +1029,7 @@ orderby distance ascending

/// <summary>
/// Sets whether a resource is available or not. By default, resources are
/// not available. If this is done after the routing table has been
/// not available. If this is done after the routing table has been
/// initialized once, the routing table will be rebuilt (therefore, use
/// SetResourceAvailability(List{string} resources, bool available) if you need
/// to set the availability of multiple resources).
Expand All @@ -1055,7 +1055,7 @@ public void SetResourceAvailability(string resource, bool available)

/// <summary>
/// Sets whether resources are available or not. By default, resources are
/// not available. If this is done after the routing table has been
/// not available. If this is done after the routing table has been
/// initialized once, the routing table will be rebuilt.
/// </summary>
public void SetResourceAvailability(List<string> resources, bool available)
Expand Down Expand Up @@ -1114,7 +1114,7 @@ private bool AreAllResourcesAvailable(string descriptor)
{
var reqResources = RequiredResources(descriptor);

if (reqResources == null)
if (reqResources == null)
return true;

foreach (var res in reqResources)
Expand Down Expand Up @@ -1168,7 +1168,7 @@ public Entry GetConversion(string sourceDescription, string targetDescription)
}
throw new ArgumentException();
}

/// <summary>
/// Enumerates all conversions in the specified conversion chain,
/// or returns null if no conversion exists.
Expand All @@ -1192,7 +1192,7 @@ public IEnumerable<Annotation> GetAnnotations(string source, string target)
{
var key = (source, target);
if (m_annotationsMap.ContainsKey(key)) return m_annotationsMap[key];
return new Annotation[0];
return Array.Empty<Annotation>();
}

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions src/Aardvark.Base.IO/FieldCoderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static FieldCoder CreateFieldCoder(this Type type, string name, string me
if (prop != null)
{
// template:
// (c, o) =>
// (c, o) =>
// {
// if (c.IsWriting) { var v = ((Foo)o).MyProperty; c.Code(ref v); }
// else { var v = 0; c.Code(ref v); ((Foo)o).MyProperty = v; }
Expand Down Expand Up @@ -105,7 +105,7 @@ private static void EmitPropertyCoder(ILGenerator gen, Type type, PropertyInfo p
var method_IsWriting = typeof(ICoder).GetMethod(
"get_IsWriting",
BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic,
null, new Type[] { }, null
null, Array.Empty<Type>(), null
);

var method_Code = GetCodeMethodOverloadFor(prop.PropertyType);
Expand Down Expand Up @@ -141,12 +141,12 @@ private static void EmitPropertyCoder(ILGenerator gen, Type type, PropertyInfo p
gen.Emit(OpCodes.Ldloca, 0); // load address of local variable v
gen.Emit(OpCodes.Initobj, prop.PropertyType); // initialize v to default value

// c.Code(ref v);
// c.Code(ref v);
gen.Emit(OpCodes.Ldarg_0); // c, used as this for following call
gen.Emit(OpCodes.Ldloca, 0); // ref v
gen.Emit(OpCodes.Callvirt, method_Code); // call ... c.code(ref v)

// ((T)o).MyProperty = v;
// ((T)o).MyProperty = v;
gen.Emit(OpCodes.Ldarg_1); // o
gen.Emit(OpCodes.Castclass, type); // cast to T
gen.Emit(OpCodes.Ldloc_0); // v
Expand Down
4 changes: 2 additions & 2 deletions src/Aardvark.Base.IO/SymMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public IEnumerable<FieldCoder> GetFieldCoders(int coderVersion)
if (s_lastMapCodedVersionMap.TryGetValue(m_typeName, out lastMapCodedVersion)
&& lastMapCodedVersion >= coderVersion)
{
if (m_typeName == this.GetType().GetCanonicalName()) return new FieldCoder[0];
if (m_typeName == this.GetType().GetCanonicalName()) return Array.Empty<FieldCoder>();
return new FieldCoder[]
{
new FieldCoder(0, "TypeName", (c, o) => c.CodePositiveSymbol(ref ((SymMap)o).m_typeName)),
Expand All @@ -119,7 +119,7 @@ public IEnumerable<FieldCoder> GetFieldCoders(int coderVersion)
}
}

if (m_typeName == this.GetType().GetCanonicalName()) return new FieldCoder[0];
if (m_typeName == this.GetType().GetCanonicalName()) return Array.Empty<FieldCoder>();

return new FieldCoder[]
{
Expand Down
12 changes: 6 additions & 6 deletions src/Aardvark.Base.IO/ZipFileContainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ private struct SubFileHeader
public long UncompressedSize;
}

private string[] m_ZipFileNames = new string[0];
private WeakReference[] m_ZipFileStreams = new WeakReference[0];
private string[] m_ZipFileNames = Array.Empty<string>();
private WeakReference[] m_ZipFileStreams = Array.Empty<WeakReference>();
private readonly SymbolDict<SubFileHeader> m_Files = new SymbolDict<SubFileHeader>();
private readonly SymbolSet m_Directories = new SymbolSet();
private readonly bool m_contentCaseSensitive = false;
Expand Down Expand Up @@ -97,7 +97,7 @@ public bool Init(string containerPath)
Report.BeginTimed("Init Zip container '" + containerPath + "'.");

FileStream mainFileStream = null;
FileStream[] zipStreams = new FileStream[0];
FileStream[] zipStreams = Array.Empty<FileStream>();

try
{
Expand All @@ -120,7 +120,7 @@ public bool Init(string containerPath)

zipStreams = new FileStream[zipEoCDR.cd_diskId + 1];
zipStreams[zipEoCDR.cd_diskId] = mainFileStream;

// Load all parts of a multi zip
if (zipEoCDR.cd_diskId > 0)
{
Expand All @@ -142,7 +142,7 @@ public bool Init(string containerPath)
zipStreams[i] = File.Open(m_ZipFileNames[i], FileMode.Open, FileAccess.Read, FileShare.None);
}
}

// Try to load Zip64 End Of Central Directory Locator
var zip64Locator = new TZip64EndOfCentralDirectoryLocator();
zip64Locator.Load(mainFileStream, zipEoCDR.Position);
Expand Down Expand Up @@ -399,6 +399,6 @@ private string SanitizeFilename(string fileName)

#endregion


}
}
3 changes: 2 additions & 1 deletion src/Aardvark.Base/AlgoDat/INode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Aardvark.Base;
using System;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -53,7 +54,7 @@ from n in sn.NodesAtDepth(depth - 1)
/// </summary>
public static IEnumerable<INode> DepthFirst(this INode self)
{
if (self == null) return new INode[0];
if (self == null) return Array.Empty<INode>();
return self.DepthFirst(n => n.SubNodes);
}

Expand Down
32 changes: 16 additions & 16 deletions src/Aardvark.Base/Extensions/IEnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static IEnumerable<Guid> Guids()
while (true) yield return Guid.NewGuid();
}
}

public static partial class EnumerableEx
{
#region Indexed Values
Expand Down Expand Up @@ -47,7 +47,7 @@ public static T[] SortedByDenseIndex<T>(
}

/// <summary>
/// Returns elements with index into 'other'.
/// Returns elements with index into 'other'.
/// </summary>
public static IEnumerable<IndexedValue<T>> IndexIntoOther<T>(
this IEnumerable<T> self, IList<T> other)
Expand All @@ -58,7 +58,7 @@ public static IEnumerable<IndexedValue<T>> IndexIntoOther<T>(
}

#endregion

#region Special Selects

public static IEnumerable<T> WhereNotNull<T>(this IEnumerable<T> self)
Expand Down Expand Up @@ -181,7 +181,7 @@ public static R[] SelectToArray<T, R>(this T[] self, Func<T, R> selector)
}
return result;
}

/// <summary>
/// Maps the elements of an array to a result list.
/// </summary>
Expand Down Expand Up @@ -690,7 +690,7 @@ public static int PairsCount<T>(this IEnumerable<T> self, bool excludeIdenticalP
}
}
}

/// <summary>
/// [A B].Triples([x y], [X Y]) -> (A, x, X) (A, x, Y) (A, y, X) (A, y, Y) (B, x, X) (B, x, Y) (B, y, X) (B, y, Y) (C, x, X) (C, x, Y) (C, y, X) (C, y, Y)
/// </summary>
Expand Down Expand Up @@ -759,7 +759,7 @@ public static IEnumerable<T> Dup<T>(this IEnumerable<T> self)

foreach (var x in self) { yield return x; yield return x; }
}

/// <summary>
/// Duplicates each element of the sequence n times.
/// [A B C].DupElements(4) -> [A A A A B B B B C C C C]
Expand Down Expand Up @@ -891,7 +891,7 @@ public static IEnumerable<T> NonNull<T>(IEnumerable<T> elements)
if (elements == null) return Enumerable.Empty<T>();
return elements;
}

/// <summary>
/// Compares both enumerables using SequenceEqual and additionally checks
/// whether both enumerables are null.
Expand Down Expand Up @@ -964,7 +964,7 @@ public static T Max<T>(this IEnumerable<T> seq, Func<T, T, bool> greaterThan, T

/// <summary>
/// Aardvark.Runtime.IEnumerableExtensions:
/// Invokes a transform function on each element of a sequence and
/// Invokes a transform function on each element of a sequence and
/// returns the element that yielded the maximum value (and the maximum value in rv_maxVal).
/// Only elements that yield larger values than minVal are considered.
/// If multiple elements yield the maximum value, the first in the sequence is chosen.
Expand Down Expand Up @@ -1002,7 +1002,7 @@ public static TSrc MaxElement<TSrc, TDst>(this IEnumerable<TSrc> source, Func<TS

/// <summary>
/// Aardvark.Runtime.IEnumerableExtensions:
/// Invokes a transform function on each element of a sequence and
/// Invokes a transform function on each element of a sequence and
/// returns the element that yielded the minimum value (and the minimum value in rv_minVal).
/// Only elements that yield smaller values than maxVal are considered.
/// If multiple elements yield the minimum value, the first in the sequence is chosen.
Expand Down Expand Up @@ -2012,7 +2012,7 @@ public static bool TrueForAll<T>(
}

#endregion

#region Conversions

public static T[] ToArrayDebug<T>(this IEnumerable<T> self)
Expand Down Expand Up @@ -2193,7 +2193,7 @@ public static TElement[] ToArray<TElement>(this IEnumerable<TElement> source, in
{
if (count == 0)
{
return new TElement[0];
return Array.Empty<TElement>();
}
else
{
Expand Down Expand Up @@ -2302,9 +2302,9 @@ public static int IndexOf<T>(this IEnumerable<T> list, T elementToFind)
}
return -1;
}

/// <summary>
/// Concats single item to sequence.
/// Concats single item to sequence.
/// </summary>
public static IEnumerable<T> Concat<T>(this IEnumerable<T> sequence, T item)
{
Expand All @@ -2313,7 +2313,7 @@ public static IEnumerable<T> Concat<T>(this IEnumerable<T> sequence, T item)
}

/// <summary>
/// Concats sequence to single item.
/// Concats sequence to single item.
/// </summary>
public static IEnumerable<T> Concat<T>(this T item, IEnumerable<T> sequence)
{
Expand Down Expand Up @@ -2400,7 +2400,7 @@ public static IEnumerable<R> SelectMany<T, R>(this IEnumerable<T> self, Func<T,
/// <summary>
/// Converts a sequence that contains numbers of elements
/// into a sequence that represents the indices of the
/// first element if the elements are stored in consecutive
/// first element if the elements are stored in consecutive
/// order and is closed by the total count elements.
/// The length of the integrated sequence is +1 of the input.
/// </summary>
Expand All @@ -2415,7 +2415,7 @@ public static IEnumerable<int> Integrated(this IEnumerable<int> counts, int sum
}

/// <summary>
/// Integrates a sequence of values.
/// Integrates a sequence of values.
/// Returns a sequence of N+1 values where the last is the sum off all value.
/// </summary>
public static IEnumerable<double> Integrated(this IEnumerable<double> values, double sum = 0)
Expand Down
Loading

0 comments on commit 6ced45e

Please sign in to comment.