From b28bcf28f3f48e9e7b37edfed928255cc4d613ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Sun, 18 Mar 2018 21:53:29 +0100 Subject: [PATCH 1/3] Add splitted in fast- and cold-path --- .../Immutable/ImmutableArray_1.Builder.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs index 482215723c4f..7d0dbf8f3942 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs @@ -247,7 +247,26 @@ public void Insert(int index, T item) /// Adds an item to the . /// /// The object to add to the . + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Add(T item) + { + int count = _count; + T[] elements = _elements; + + if ((uint)count < (uint)elements.Length) + { + elements[count] = item; + _count = count + 1; + } + else + { + AddWithResize(item); + } + } + + // Improve code quality as uncommon path + [MethodImpl(MethodImplOptions.NoInlining)] + private void AddWithResize(T item) { int newCount = _count + 1; this.EnsureCapacity(newCount); From 9e6b5463aa0d93324084750e6173faf8f26cdf17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Sun, 18 Mar 2018 23:13:07 +0100 Subject: [PATCH 2/3] Added missing namespace --- .../src/System/Collections/Immutable/ImmutableArray_1.Builder.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs index 7d0dbf8f3942..ffc3797396e4 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs @@ -5,6 +5,7 @@ using System.Collections.Generic; using System.Diagnostics; using System.Diagnostics.Contracts; +using System.Runtime.CompilerServices; namespace System.Collections.Immutable { From 74de90d3f8e25e1b286d44bbc280e4ccbc7393ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCnther=20Foidl?= Date: Thu, 21 Jun 2018 16:31:17 +0200 Subject: [PATCH 3/3] Addressed PR-feedback * https://github.com/dotnet/corefx/pull/28184#discussion_r197148884 * https://github.com/dotnet/corefx/pull/28184#discussion_r197150311 --- .../System/Collections/Immutable/ImmutableArray_1.Builder.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs index ffc3797396e4..a5ba7b2e9863 100644 --- a/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs +++ b/src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray_1.Builder.cs @@ -254,6 +254,8 @@ public void Add(T item) int count = _count; T[] elements = _elements; + // PERF: The uint-casts allow the JIT to eliminate bound-checks. + // https://github.com/dotnet/coreclr/pull/9773 if ((uint)count < (uint)elements.Length) { elements[count] = item; @@ -265,7 +267,7 @@ public void Add(T item) } } - // Improve code quality as uncommon path + // Specify NoInlining so that we are guaranteed an opportunity to service this method [MethodImpl(MethodImplOptions.NoInlining)] private void AddWithResize(T item) {