Skip to content

Commit

Permalink
Inline GrowBuffer. Gains another 1%.
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveSandersonMS committed Jul 31, 2020
1 parent c846f72 commit 5b77481
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ internal class RenderTreeFrameArrayBuilder : ArrayBuilder<RenderTreeFrame>
{
public void AppendElement(int sequence, string elementName)
{
GrowBufferIfFull();
if (_itemsInUse == _items.Length)
{
GrowBuffer(_items.Length * 2);
}

ref var item = ref _items[_itemsInUse++];
Debug.Assert(item.FrameType == default);

Expand All @@ -26,7 +30,11 @@ public void AppendElement(int sequence, string elementName)

public void AppendText(int sequence, string textContent)
{
GrowBufferIfFull();
if (_itemsInUse == _items.Length)
{
GrowBuffer(_items.Length * 2);
}

ref var item = ref _items[_itemsInUse++];
Debug.Assert(item.FrameType == default);

Expand All @@ -37,7 +45,11 @@ public void AppendText(int sequence, string textContent)

public void AppendMarkup(int sequence, string markupContent)
{
GrowBufferIfFull();
if (_itemsInUse == _items.Length)
{
GrowBuffer(_items.Length * 2);
}

ref var item = ref _items[_itemsInUse++];
Debug.Assert(item.FrameType == default);

Expand All @@ -48,7 +60,11 @@ public void AppendMarkup(int sequence, string markupContent)

public void AppendAttribute(int sequence, string attributeName, object? attributeValue)
{
GrowBufferIfFull();
if (_itemsInUse == _items.Length)
{
GrowBuffer(_items.Length * 2);
}

ref var item = ref _items[_itemsInUse++];
Debug.Assert(item.FrameType == default);

Expand All @@ -60,7 +76,11 @@ public void AppendAttribute(int sequence, string attributeName, object? attribut

public void AppendComponent(int sequence, Type componentType)
{
GrowBufferIfFull();
if (_itemsInUse == _items.Length)
{
GrowBuffer(_items.Length * 2);
}

ref var item = ref _items[_itemsInUse++];
Debug.Assert(item.FrameType == default);

Expand All @@ -71,7 +91,11 @@ public void AppendComponent(int sequence, Type componentType)

public void AppendElementReferenceCapture(int sequence, Action<ElementReference> elementReferenceCaptureAction)
{
GrowBufferIfFull();
if (_itemsInUse == _items.Length)
{
GrowBuffer(_items.Length * 2);
}

ref var item = ref _items[_itemsInUse++];
Debug.Assert(item.FrameType == default);

Expand All @@ -82,7 +106,11 @@ public void AppendElementReferenceCapture(int sequence, Action<ElementReference>

public void AppendComponentReferenceCapture(int sequence, Action<object?> componentReferenceCaptureAction, int parentFrameIndexValue)
{
GrowBufferIfFull();
if (_itemsInUse == _items.Length)
{
GrowBuffer(_items.Length * 2);
}

ref var item = ref _items[_itemsInUse++];
Debug.Assert(item.FrameType == default);

Expand All @@ -94,7 +122,11 @@ public void AppendComponentReferenceCapture(int sequence, Action<object?> compon

public void AppendRegion(int sequence)
{
GrowBufferIfFull();
if (_itemsInUse == _items.Length)
{
GrowBuffer(_items.Length * 2);
}

ref var item = ref _items[_itemsInUse++];
Debug.Assert(item.FrameType == default);

Expand Down
19 changes: 8 additions & 11 deletions src/Components/Shared/src/ArrayBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,15 @@ public ArrayBuilder(int minCapacity = 32, ArrayPool<T> arrayPool = null)
/// <returns>The index of the appended item.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)] // Just like System.Collections.Generic.List<T>
public int Append(in T item)
{
GrowBufferIfFull();

var indexOfAppendedItem = _itemsInUse++;
_items[indexOfAppendedItem] = item;
return indexOfAppendedItem;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
protected void GrowBufferIfFull()
{
if (_itemsInUse == _items.Length)
{
GrowBuffer(_items.Length * 2);
}

var indexOfAppendedItem = _itemsInUse++;
_items[indexOfAppendedItem] = item;
return indexOfAppendedItem;
}

internal int Append(T[] source, int startIndex, int length)
Expand Down Expand Up @@ -145,7 +139,10 @@ public void InsertExpensive(int index, T value)
ThrowIndexOutOfBoundsException();
}

GrowBufferIfFull();
if (_itemsInUse == _items.Length)
{
GrowBuffer(_items.Length * 2);
}

Array.Copy(_items, index, _items, index + 1, _itemsInUse - index);
_itemsInUse++;
Expand Down

0 comments on commit 5b77481

Please sign in to comment.