From 5e0de1ebee532dc2ff4ce88ec2d7bee90817825c Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Sat, 9 Jul 2016 08:02:14 +0100 Subject: Remove the overload for Add(RepeatedField) We now just perform the optimization within AddRange itself. This is a breaking change in terms of "drop in the DLL", but is source compatible, which should be fine. --- .../Collections/RepeatedFieldTest.cs | 12 +++++++-- .../Google.Protobuf/Collections/RepeatedField.cs | 30 ++++++++++------------ 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs b/csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs index f8e3b177..6852f75f 100644 --- a/csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs +++ b/csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs @@ -153,10 +153,18 @@ namespace Google.Protobuf.Collections } [Test] - public void Add_RepeatedField() + public void AddRange_AlreadyNotEmpty() + { + var list = new RepeatedField { 1, 2, 3 }; + list.AddRange(new List { 4, 5, 6 }); + CollectionAssert.AreEqual(new[] { 1, 2, 3, 4, 5, 6 }, list); + } + + [Test] + public void AddRange_RepeatedField() { var list = new RepeatedField { "original" }; - list.Add(new RepeatedField { "foo", "bar" }); + list.AddRange(new RepeatedField { "foo", "bar" }); Assert.AreEqual(3, list.Count); Assert.AreEqual("original", list[0]); Assert.AreEqual("foo", list[1]); diff --git a/csharp/src/Google.Protobuf/Collections/RepeatedField.cs b/csharp/src/Google.Protobuf/Collections/RepeatedField.cs index dcc6e9bf..7bb56448 100644 --- a/csharp/src/Google.Protobuf/Collections/RepeatedField.cs +++ b/csharp/src/Google.Protobuf/Collections/RepeatedField.cs @@ -288,21 +288,6 @@ namespace Google.Protobuf.Collections /// public bool IsReadOnly => false; - // TODO: Remove this overload and just handle it in the one below, at execution time? - - /// - /// Adds all of the specified values into this collection. - /// - /// The values to add to this collection. - public void Add(RepeatedField values) - { - ProtoPreconditions.CheckNotNull(values, nameof(values)); - EnsureSize(count + values.count); - // We know that all the values will be valid, because it's a RepeatedField. - Array.Copy(values.array, 0, array, count, values.count); - count += values.count; - } - /// /// Adds all of the specified values into this collection. /// @@ -311,8 +296,19 @@ namespace Google.Protobuf.Collections { ProtoPreconditions.CheckNotNull(values, nameof(values)); - // Optimize the case where we know the size and can ask the collection to - // copy itself. + // Optimization 1: If the collection we're adding is already a RepeatedField, + // we know the values are valid. + var otherRepeatedField = values as RepeatedField; + if (otherRepeatedField != null) + { + EnsureSize(count + otherRepeatedField.count); + Array.Copy(otherRepeatedField.array, 0, array, count, otherRepeatedField.count); + count += otherRepeatedField.count; + return; + } + + // Optimization 2: The collection is an ICollection, so we can expand + // just once and ask the collection to copy itself into the array. var collection = values as ICollection; if (collection != null) { -- cgit v1.2.3