Skip to content

Commit

Permalink
revert capacity related changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Aug 10, 2023
1 parent e82b68d commit 5ac06b5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
62 changes: 44 additions & 18 deletions src/Esprima/ArrayList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,27 @@ public int Capacity
{
AssertUnchanged();

if (value < (_items?.Length ?? 0))
if (value < _count)
{
ThrowArgumentOutOfRangeException(nameof(value), value, null);
}

EnsureCapacity(value);
else if (value == (_items?.Length ?? 0))
{
return;
}
else if (value > 0)
{
T[] array = new T[value];
if (_count > 0)
{
Array.Copy(_items, 0, array, 0, _count);
}
_items = array;
}
else
{
_items = null;
}

OnChanged();
}
Expand Down Expand Up @@ -213,7 +228,10 @@ public void AddRange(ReadOnlySpan<T> items)
var oldCount = _count;
var newCount = oldCount + itemCount;

EnsureCapacity(newCount);
if (Capacity < newCount)
{
Array.Resize(ref _items, Math.Max(newCount, MinAllocatedCount));
}

Debug.Assert(_items is not null);
items.CopyTo(_items.AsSpan(oldCount, itemCount));
Expand All @@ -231,7 +249,12 @@ public void Add(T item)
{
AssertUnchanged();

EnsureCapacity(_count + 1);
var capacity = Capacity;

if (_count == capacity)
{
Array.Resize(ref _items, Math.Max(capacity * 2, MinAllocatedCount));
}

Debug.Assert(_items is not null);
_items![_count] = item;
Expand Down Expand Up @@ -262,7 +285,12 @@ public void Insert(int index, T item)
ThrowIndexOutOfRangeException<T>();
}

EnsureCapacity(_count + 1);
var capacity = Capacity;

if (_count == capacity)
{
Array.Resize(ref _items, Math.Max(capacity * 2, MinAllocatedCount));
}

Debug.Assert(_items is not null);
Array.Copy(_items, index, _items, index + 1, Count - index);
Expand Down Expand Up @@ -327,6 +355,16 @@ public void Yield(out T[]? items, out int count)
this = default;
}

public void TrimExcess(int threshold = MinAllocatedCount)
{
AssertUnchanged();

if (Capacity - _count > threshold)
{
Capacity = _count;
}
}

/// <remarks>
/// Items should not be added or removed from the <see cref="ArrayList{T}"/> while the returned <see cref="Span{T}"/> is in use!
/// </remarks>
Expand Down Expand Up @@ -367,18 +405,6 @@ IEnumerator IEnumerable.GetEnumerator()
return GetEnumerator();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void EnsureCapacity(int capacity)
{
var temp = _items;
if (temp is null || temp.Length < capacity)
{
var minAllocatedCount = temp is null || temp.Length < MinAllocatedCount ? MinAllocatedCount : temp.Length * 2;
var newSize = Math.Max(capacity, minAllocatedCount);
Array.Resize(ref _items, newSize);
}
}

/// <remarks>
/// This implementation does not detect changes to the list
/// during iteration and therefore the behaviour is undefined
Expand Down
2 changes: 1 addition & 1 deletion src/Esprima/JavascriptParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void ReleaseLargeBuffers()
Decorators.Clear();
if (Decorators.Capacity > 64)
{
Decorators = new ArrayList<Decorator>(64);
Decorators.Capacity = 64;
}

if (LabelSet.Count > 64)
Expand Down
1 change: 1 addition & 0 deletions src/Esprima/Scanner.RegExpParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ public RegExpParseResult Parse()
}

Debug.Assert(conversionError is null);
capturingGroups.TrimExcess();

var options = FlagsToOptions(_flags, compiled: _scanner._regExpParseMode == RegExpParseMode.AdaptToCompiled);
var matchTimeout = _scanner._regexTimeout;
Expand Down
3 changes: 2 additions & 1 deletion src/Esprima/Scanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ internal void ReleaseLargeBuffers()
_curlyStack.Clear();
if (_curlyStack.Capacity > 16)
{
_curlyStack = new ArrayList<string>(16);
_curlyStack.Capacity = 16;
}

_sb.Clear();
Expand Down Expand Up @@ -517,6 +517,7 @@ internal ArrayList<Comment> ScanCommentsInternal()
public ReadOnlySpan<Comment> ScanComments()
{
var comments = ScanCommentsInternal();
comments.TrimExcess();
return comments.AsSpan();
}

Expand Down

0 comments on commit 5ac06b5

Please sign in to comment.