From 8242247436a17be2827f6fc5cf0f83e9676c46d7 Mon Sep 17 00:00:00 2001 From: mlemanczyk Date: Sat, 15 Jul 2023 16:42:33 +0200 Subject: [PATCH] RecyclableList.AddRange performance improvements. Local variable removed. --- Recyclable.Collections/RecyclableList.cs | 35 ++++++++++-------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/Recyclable.Collections/RecyclableList.cs b/Recyclable.Collections/RecyclableList.cs index 4cd596b..a4e1aa0 100644 --- a/Recyclable.Collections/RecyclableList.cs +++ b/Recyclable.Collections/RecyclableList.cs @@ -198,70 +198,65 @@ namespace Recyclable.Collections [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)] public void AddRange(in T[] items) { - int targetCapacity = _count + items.Length; - if (_capacity < targetCapacity) + if (_capacity < _count + items.Length) { - _ = RecyclableListHelpers.EnsureCapacity(this, targetCapacity); + _ = RecyclableListHelpers.EnsureCapacity(this, _count + items.Length); } new Span(items).CopyTo(new Span(_memoryBlock, _count, items.Length)); - _count = targetCapacity; + _count += items.Length; _version++; } [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)] public void AddRange(ReadOnlySpan items) { - int targetCapacity = _count + items.Length; - if (_capacity < targetCapacity) + if (_capacity < _count + items.Length) { - _ = RecyclableListHelpers.EnsureCapacity(this, targetCapacity); + _ = RecyclableListHelpers.EnsureCapacity(this, _count + items.Length); } items.CopyTo(new Span(_memoryBlock, _count, items.Length)); - _count = targetCapacity; + _count += items.Length; _version++; } [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)] public void AddRange(List items) { - var targetCapacity = _count + items.Count; - if (_capacity < targetCapacity) + if (_capacity < _count + items.Count) { - _ = RecyclableListHelpers.EnsureCapacity(this, targetCapacity); + _ = RecyclableListHelpers.EnsureCapacity(this, _count + items.Count); } items.CopyTo(_memoryBlock, _count); - _count = targetCapacity; + _count += items.Count; _version++; } [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)] public void AddRange(IList items) { - var targetCapacity = _count + items.Count; - if (_capacity < targetCapacity) + if (_capacity < _count + items.Count) { - _ = RecyclableListHelpers.EnsureCapacity(this, targetCapacity); + _ = RecyclableListHelpers.EnsureCapacity(this, _count + items.Count); } items.CopyTo(_memoryBlock, _count); - _count = targetCapacity; + _count += items.Count; _version++; } [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.AggressiveOptimization)] public void AddRange(RecyclableList items) { - var targetCapacity = _count + items._count; - if (_capacity < targetCapacity) + if (_capacity < _count + items._count) { - _ = RecyclableListHelpers.EnsureCapacity(this, targetCapacity); + _ = RecyclableListHelpers.EnsureCapacity(this, _count + items._count); } new Span(items._memoryBlock, 0, items._count).CopyTo(new Span(_memoryBlock, _count, items._count)); - _count = targetCapacity; + _count += items._count; _version++; }