diff options
author | Jon Skeet <skeet@pobox.com> | 2015-06-12 13:07:51 +0100 |
---|---|---|
committer | Jon Skeet <skeet@pobox.com> | 2015-06-12 13:07:51 +0100 |
commit | 7532f0256f58e0d11711da4e159534bccbf266f0 (patch) | |
tree | 246de5832a971caff8dcf85db1f971aed2c48f3c /csharp/src/ProtocolBuffers.Test | |
parent | 5a33827eec75b980fb152c531e4c6b75ce5af015 (diff) | |
download | protobuf-7532f0256f58e0d11711da4e159534bccbf266f0.tar.gz protobuf-7532f0256f58e0d11711da4e159534bccbf266f0.tar.bz2 protobuf-7532f0256f58e0d11711da4e159534bccbf266f0.zip |
Reimplement RepeatedField<T> using an array as the backing store.
This is effectively reimplementing List<T>, but with a few advantages:
- We know that an empty repeated field is common, so don't allocate an array until we need to
- With direct access to the array, we can easily convert enum values to int without boxing
- We can relax the restrictions over what happens if the repeated field is modified while iterating, avoiding so much checking
This is somewhat risky, in that reimplementing a building block like this is *always* risky, but hey...
(The performance benefits are significant...)
Diffstat (limited to 'csharp/src/ProtocolBuffers.Test')
-rw-r--r-- | csharp/src/ProtocolBuffers.Test/CodedInputStreamTest.cs | 4 | ||||
-rw-r--r-- | csharp/src/ProtocolBuffers.Test/RepeatedFieldTest.cs | 9 |
2 files changed, 7 insertions, 6 deletions
diff --git a/csharp/src/ProtocolBuffers.Test/CodedInputStreamTest.cs b/csharp/src/ProtocolBuffers.Test/CodedInputStreamTest.cs index 450662a6..07f54e94 100644 --- a/csharp/src/ProtocolBuffers.Test/CodedInputStreamTest.cs +++ b/csharp/src/ProtocolBuffers.Test/CodedInputStreamTest.cs @@ -487,7 +487,7 @@ namespace Google.Protobuf uint tag;
Assert.IsTrue(input.ReadTag(out tag));
- List<TestNegEnum> values = new List<TestNegEnum>();
+ RepeatedField<TestNegEnum> values = new RepeatedField<TestNegEnum>();
input.ReadEnumArray(tag, values);
Assert.AreEqual(6, values.Count);
@@ -511,7 +511,7 @@ namespace Google.Protobuf uint tag;
Assert.IsTrue(input.ReadTag(out tag));
- List<TestNegEnum> values = new List<TestNegEnum>();
+ RepeatedField<TestNegEnum> values = new RepeatedField<TestNegEnum>();
input.ReadEnumArray(tag, values);
Assert.AreEqual(6, values.Count);
diff --git a/csharp/src/ProtocolBuffers.Test/RepeatedFieldTest.cs b/csharp/src/ProtocolBuffers.Test/RepeatedFieldTest.cs index 94e30189..cbe79294 100644 --- a/csharp/src/ProtocolBuffers.Test/RepeatedFieldTest.cs +++ b/csharp/src/ProtocolBuffers.Test/RepeatedFieldTest.cs @@ -40,11 +40,12 @@ namespace Google.Protobuf [Test] public void Add_RepeatedField() { - var list = new RepeatedField<string>(); + var list = new RepeatedField<string> { "original" }; list.Add(new RepeatedField<string> { "foo", "bar" }); - Assert.AreEqual(2, list.Count); - Assert.AreEqual("foo", list[0]); - Assert.AreEqual("bar", list[1]); + Assert.AreEqual(3, list.Count); + Assert.AreEqual("original", list[0]); + Assert.AreEqual("foo", list[1]); + Assert.AreEqual("bar", list[2]); } } } |