Skip to content
Merged
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
10 changes: 4 additions & 6 deletions samples/Esprima.Benchmark/NodeListEnumerationBenchmark.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

namespace Esprima.Benchmark;

[MemoryDiagnoser]
[RankColumn]
[CsvExporter]
[MarkdownExporterAttribute.GitHub]
[MemoryDiagnoser]
public class NodeListEnumerationBenchmark
{
private const string FileName = "bundle";
Expand All @@ -30,7 +28,7 @@ public void Setup()
}

[Benchmark]
public object For_DirectIndexing()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn, nice oversight on my part... This pretty much messed up the benchmark results.

Nice catch! 👍

public int For_DirectIndexing()
{
var nodeList = _nodeLists[NodeListIndex];

Expand All @@ -43,7 +41,7 @@ public object For_DirectIndexing()
}

[Benchmark]
public object For_SpanIndexing()
public int For_SpanIndexing()
{
var nodeList = _nodeLists[NodeListIndex].AsSpan();

Expand All @@ -56,7 +54,7 @@ public object For_SpanIndexing()
}

[Benchmark]
public object ForEach_Span()
public int ForEach_Span()
{
var nodeList = _nodeLists[NodeListIndex].AsSpan();

Expand Down
10 changes: 1 addition & 9 deletions samples/Esprima.Benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
using System.Reflection;
using BenchmarkDotNet.Running;

namespace Esprima.Benchmark;

public class Program
{
public static void Main(string[] args)
{
BenchmarkSwitcher.FromAssembly(typeof(Program).GetTypeInfo().Assembly).Run(args);
}
}
BenchmarkSwitcher.FromAssembly(typeof(Program).GetTypeInfo().Assembly).Run(args);
19 changes: 13 additions & 6 deletions src/Esprima/ArrayList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public ArrayList(int initialCapacity)
{
if (initialCapacity < 0)
{
ThrowArgumentOutOfRangeException<T>(nameof(initialCapacity), initialCapacity, null);
ThrowInvalidInitialCapacity();
}

_items = initialCapacity > 0 ? new T[initialCapacity] : null;
Expand All @@ -90,6 +90,12 @@ public ArrayList(int initialCapacity)
_localVersion = 0;
_sharedVersion = null;
#endif

[MethodImpl(MethodImplOptions.NoInlining)]
static void ThrowInvalidInitialCapacity()
{
ThrowArgumentException("Invalid initial capacity", nameof(initialCapacity));
}
}

/// <remarks>
Expand All @@ -108,6 +114,7 @@ internal ArrayList(T[] items)

public int Capacity
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
AssertUnchanged();
Expand All @@ -119,7 +126,7 @@ public int Capacity

if (value < _count)
{
ThrowArgumentOutOfRangeException<T>(nameof(value), value, null);
ThrowArgumentOutOfRangeException(nameof(value), value, null);
}
else if (value == (_items?.Length ?? 0))
{
Expand Down Expand Up @@ -166,7 +173,7 @@ public T this[int index]
return _items![index];
}

return ThrowArgumentOutOfRangeException<T>(nameof(index), index, null);
return ThrowIndexOutOfRangeException<T>();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand All @@ -181,7 +188,7 @@ public T this[int index]
return;
}

ThrowArgumentOutOfRangeException<T>(nameof(index), index, null);
ThrowIndexOutOfRangeException<T>();
}
}

Expand Down Expand Up @@ -275,7 +282,7 @@ public void Insert(int index, T item)

if ((uint) index > (uint) _count)
{
ThrowArgumentOutOfRangeException<T>(nameof(index), index, null);
ThrowIndexOutOfRangeException<T>();
}

var capacity = Capacity;
Expand All @@ -299,7 +306,7 @@ public void RemoveAt(int index)

if ((uint) index >= (uint) _count)
{
ThrowArgumentOutOfRangeException<T>(nameof(index), index, null);
ThrowIndexOutOfRangeException<T>();
}

_count--;
Expand Down
11 changes: 8 additions & 3 deletions src/Esprima/Ast/NodeList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@ internal NodeList(ICollection<T> collection)
{
if (collection is null)
{
throw new ArgumentNullException(nameof(collection));
ThrowArgumentNullException();
}

_count = collection.Count;
_count = collection!.Count;
if (_count > 0)
{
_items = new T[_count];
collection.CopyTo(_items, 0);
}

static void ThrowArgumentNullException()
{
ThrowArgumentNullException<T>(nameof(collection));
}
}

/// <remarks>
Expand Down Expand Up @@ -53,7 +58,7 @@ public T this[int index]
return _items![index];
}

return ThrowArgumentOutOfRangeException<T>(nameof(index), index, null);
return ThrowIndexOutOfRangeException<T>();
}
}

Expand Down
18 changes: 15 additions & 3 deletions src/Esprima/EsprimaExceptionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,29 @@ namespace Esprima;
internal static class EsprimaExceptionHelper
{
[DoesNotReturn]
public static T ThrowArgumentNullException<T>(string message)
public static T ThrowArgumentNullException<T>(string paramName)
{
throw new ArgumentNullException(message);
throw new ArgumentNullException(paramName);
}

[DoesNotReturn]
public static T ThrowArgumentOutOfRangeException<T>(string paramName, object actualValue, string? message = null)
public static void ThrowArgumentOutOfRangeException<T>(string paramName, T actualValue, string? message = null)
{
throw new ArgumentOutOfRangeException(paramName, actualValue, message);
}

[DoesNotReturn]
public static void ThrowArgumentException(string message, string paramName)
{
throw new ArgumentException(message, paramName);
}

[DoesNotReturn]
public static T ThrowIndexOutOfRangeException<T>()
{
throw new ArgumentOutOfRangeException("index");
}

[DoesNotReturn]
public static T ThrowFormatException<T>(string message)
{
Expand Down