aboutsummaryrefslogtreecommitdiff
path: root/src/ProtocolBuffers/Collections
diff options
context:
space:
mode:
authorcsharptest <roger@csharptest.net>2011-06-09 19:47:56 -0500
committerrogerk <devnull@localhost>2011-06-09 19:47:56 -0500
commitced18e10ae9ca41f338c9e788642d705dd17f9d4 (patch)
tree334ca02758aca596df2b71fb4ada50db71aa4913 /src/ProtocolBuffers/Collections
parent367e02261c6bee9bce37cb6942bd6cbf743fb67c (diff)
downloadprotobuf-ced18e10ae9ca41f338c9e788642d705dd17f9d4.tar.gz
protobuf-ced18e10ae9ca41f338c9e788642d705dd17f9d4.tar.bz2
protobuf-ced18e10ae9ca41f338c9e788642d705dd17f9d4.zip
Several performance tweaks
- Removed default value assingment when default is equal to default(T) - Added Benchmarks for most types and repeated/packed arrays - Left PopsicleList's list fields uninitialized util needed - Changed CodedInputStream's repated/packed reader - Changed Enum writers to simply cast to int - Changed the WriteEnum to use object rawValue that provides .ToString() if needed - Should be fully on par with original library for performance, gaining 2x-3x in some cases
Diffstat (limited to 'src/ProtocolBuffers/Collections')
-rw-r--r--src/ProtocolBuffers/Collections/IPopsicleList.cs8
-rw-r--r--src/ProtocolBuffers/Collections/PopsicleList.cs50
2 files changed, 40 insertions, 18 deletions
diff --git a/src/ProtocolBuffers/Collections/IPopsicleList.cs b/src/ProtocolBuffers/Collections/IPopsicleList.cs
index 7f6fd8bc..a1a75815 100644
--- a/src/ProtocolBuffers/Collections/IPopsicleList.cs
+++ b/src/ProtocolBuffers/Collections/IPopsicleList.cs
@@ -47,4 +47,12 @@ namespace Google.ProtocolBuffers.Collections
{
void Add(IEnumerable<T> collection);
}
+
+ /// <summary>
+ /// Used to efficiently cast the elements of enumerations
+ /// </summary>
+ internal interface ICastArray
+ {
+ IEnumerable<TItemType> CastArray<TItemType>();
+ }
} \ No newline at end of file
diff --git a/src/ProtocolBuffers/Collections/PopsicleList.cs b/src/ProtocolBuffers/Collections/PopsicleList.cs
index 33324303..48161e82 100644
--- a/src/ProtocolBuffers/Collections/PopsicleList.cs
+++ b/src/ProtocolBuffers/Collections/PopsicleList.cs
@@ -40,10 +40,12 @@ namespace Google.ProtocolBuffers.Collections
/// to be made read-only (with the <see cref="MakeReadOnly" /> method),
/// after which any modifying methods throw <see cref="NotSupportedException" />.
/// </summary>
- public sealed class PopsicleList<T> : IPopsicleList<T>
+ public sealed class PopsicleList<T> : IPopsicleList<T>, ICastArray
{
- private readonly List<T> items = new List<T>();
- private bool readOnly = false;
+ private static readonly IEnumerable<T> EmptySet = new T[0];
+
+ private List<T> items;
+ private bool readOnly;
/// <summary>
/// Makes this list read-only ("freezes the popsicle"). From this
@@ -57,7 +59,7 @@ namespace Google.ProtocolBuffers.Collections
public int IndexOf(T item)
{
- return items.IndexOf(item);
+ return items == null ? -1 : items.IndexOf(item);
}
public void Insert(int index, T item)
@@ -74,7 +76,7 @@ namespace Google.ProtocolBuffers.Collections
public T this[int index]
{
- get { return items[index]; }
+ get { if (items == null) throw new ArgumentOutOfRangeException(); return items[index]; }
set
{
ValidateModification();
@@ -96,17 +98,18 @@ namespace Google.ProtocolBuffers.Collections
public bool Contains(T item)
{
- return items.Contains(item);
+ return items == null ? false : items.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
- items.CopyTo(array, arrayIndex);
+ if (items != null)
+ items.CopyTo(array, arrayIndex);
}
public int Count
{
- get { return items.Count; }
+ get { return items == null ? 0 : items.Count; }
}
public bool IsReadOnly
@@ -120,18 +123,9 @@ namespace Google.ProtocolBuffers.Collections
return items.Remove(item);
}
- public void Add(IEnumerable<T> collection)
- {
- if (readOnly)
- {
- throw new NotSupportedException("List is read-only");
- }
- items.AddRange(collection);
- }
-
public IEnumerator<T> GetEnumerator()
{
- return items.GetEnumerator();
+ return items == null ? EmptySet.GetEnumerator() : items.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
@@ -139,12 +133,32 @@ namespace Google.ProtocolBuffers.Collections
return GetEnumerator();
}
+ public void Add(IEnumerable<T> collection)
+ {
+ if (readOnly)
+ {
+ throw new NotSupportedException("List is read-only");
+ }
+ if (items == null)
+ items = new List<T>();
+ items.AddRange(collection);
+ }
+
private void ValidateModification()
{
if (readOnly)
{
throw new NotSupportedException("List is read-only");
}
+ if (items == null)
+ items = new List<T>();
+ }
+
+ IEnumerable<TItemType> ICastArray.CastArray<TItemType>()
+ {
+ if (items == null)
+ return new TItemType[0];
+ return (TItemType[])(object)items.ToArray();
}
}
} \ No newline at end of file