Commit 9b66768e authored by Jon Skeet's avatar Jon Skeet

Optimization to avoid foreach over empty lists.

parent 43527448
...@@ -543,19 +543,14 @@ namespace Google.Protobuf ...@@ -543,19 +543,14 @@ namespace Google.Protobuf
#endregion #endregion
#region Write array members #region Write array members
public void WriteGroupArray<T>(int fieldNumber, RepeatedField<T> list) public void WriteMessageArray<T>(int fieldNumber, RepeatedField<T> list)
where T : IMessage where T : IMessage
{ {
foreach (IMessage value in list) if (list.Count == 0)
{ {
WriteGroup(fieldNumber, value); return;
} }
} foreach (T value in list)
public void WriteMessageArray<T>(int fieldNumber, RepeatedField<T> list)
where T : IMessage
{
foreach (IMessage value in list)
{ {
WriteMessage(fieldNumber, value); WriteMessage(fieldNumber, value);
} }
...@@ -563,6 +558,10 @@ namespace Google.Protobuf ...@@ -563,6 +558,10 @@ namespace Google.Protobuf
public void WriteStringArray(int fieldNumber, RepeatedField<string> list) public void WriteStringArray(int fieldNumber, RepeatedField<string> list)
{ {
if (list.Count == 0)
{
return;
}
foreach (var value in list) foreach (var value in list)
{ {
WriteString(fieldNumber, value); WriteString(fieldNumber, value);
...@@ -571,6 +570,10 @@ namespace Google.Protobuf ...@@ -571,6 +570,10 @@ namespace Google.Protobuf
public void WriteBytesArray(int fieldNumber, RepeatedField<ByteString> list) public void WriteBytesArray(int fieldNumber, RepeatedField<ByteString> list)
{ {
if (list.Count == 0)
{
return;
}
foreach (var value in list) foreach (var value in list)
{ {
WriteBytes(fieldNumber, value); WriteBytes(fieldNumber, value);
...@@ -579,6 +582,10 @@ namespace Google.Protobuf ...@@ -579,6 +582,10 @@ namespace Google.Protobuf
public void WriteBoolArray(int fieldNumber, RepeatedField<bool> list) public void WriteBoolArray(int fieldNumber, RepeatedField<bool> list)
{ {
if (list.Count == 0)
{
return;
}
foreach (var value in list) foreach (var value in list)
{ {
WriteBool(fieldNumber, value); WriteBool(fieldNumber, value);
...@@ -587,6 +594,10 @@ namespace Google.Protobuf ...@@ -587,6 +594,10 @@ namespace Google.Protobuf
public void WriteInt32Array(int fieldNumber, RepeatedField<int> list) public void WriteInt32Array(int fieldNumber, RepeatedField<int> list)
{ {
if (list.Count == 0)
{
return;
}
foreach (var value in list) foreach (var value in list)
{ {
WriteInt32(fieldNumber, value); WriteInt32(fieldNumber, value);
...@@ -595,6 +606,10 @@ namespace Google.Protobuf ...@@ -595,6 +606,10 @@ namespace Google.Protobuf
public void WriteSInt32Array(int fieldNumber, RepeatedField<int> list) public void WriteSInt32Array(int fieldNumber, RepeatedField<int> list)
{ {
if (list.Count == 0)
{
return;
}
foreach (var value in list) foreach (var value in list)
{ {
WriteSInt32(fieldNumber, value); WriteSInt32(fieldNumber, value);
...@@ -603,6 +618,10 @@ namespace Google.Protobuf ...@@ -603,6 +618,10 @@ namespace Google.Protobuf
public void WriteUInt32Array(int fieldNumber, RepeatedField<uint> list) public void WriteUInt32Array(int fieldNumber, RepeatedField<uint> list)
{ {
if (list.Count == 0)
{
return;
}
foreach (var value in list) foreach (var value in list)
{ {
WriteUInt32(fieldNumber, value); WriteUInt32(fieldNumber, value);
...@@ -611,6 +630,10 @@ namespace Google.Protobuf ...@@ -611,6 +630,10 @@ namespace Google.Protobuf
public void WriteFixed32Array(int fieldNumber, RepeatedField<uint> list) public void WriteFixed32Array(int fieldNumber, RepeatedField<uint> list)
{ {
if (list.Count == 0)
{
return;
}
foreach (var value in list) foreach (var value in list)
{ {
WriteFixed32(fieldNumber, value); WriteFixed32(fieldNumber, value);
...@@ -619,6 +642,10 @@ namespace Google.Protobuf ...@@ -619,6 +642,10 @@ namespace Google.Protobuf
public void WriteSFixed32Array(int fieldNumber, RepeatedField<int> list) public void WriteSFixed32Array(int fieldNumber, RepeatedField<int> list)
{ {
if (list.Count == 0)
{
return;
}
foreach (var value in list) foreach (var value in list)
{ {
WriteSFixed32(fieldNumber, value); WriteSFixed32(fieldNumber, value);
...@@ -627,6 +654,10 @@ namespace Google.Protobuf ...@@ -627,6 +654,10 @@ namespace Google.Protobuf
public void WriteInt64Array(int fieldNumber, RepeatedField<long> list) public void WriteInt64Array(int fieldNumber, RepeatedField<long> list)
{ {
if (list.Count == 0)
{
return;
}
foreach (var value in list) foreach (var value in list)
{ {
WriteInt64(fieldNumber, value); WriteInt64(fieldNumber, value);
...@@ -635,6 +666,10 @@ namespace Google.Protobuf ...@@ -635,6 +666,10 @@ namespace Google.Protobuf
public void WriteSInt64Array(int fieldNumber, RepeatedField<long> list) public void WriteSInt64Array(int fieldNumber, RepeatedField<long> list)
{ {
if (list.Count == 0)
{
return;
}
foreach (var value in list) foreach (var value in list)
{ {
WriteSInt64(fieldNumber, value); WriteSInt64(fieldNumber, value);
...@@ -643,6 +678,10 @@ namespace Google.Protobuf ...@@ -643,6 +678,10 @@ namespace Google.Protobuf
public void WriteUInt64Array(int fieldNumber, RepeatedField<ulong> list) public void WriteUInt64Array(int fieldNumber, RepeatedField<ulong> list)
{ {
if (list.Count == 0)
{
return;
}
foreach (var value in list) foreach (var value in list)
{ {
WriteUInt64(fieldNumber, value); WriteUInt64(fieldNumber, value);
...@@ -651,6 +690,10 @@ namespace Google.Protobuf ...@@ -651,6 +690,10 @@ namespace Google.Protobuf
public void WriteFixed64Array(int fieldNumber, RepeatedField<ulong> list) public void WriteFixed64Array(int fieldNumber, RepeatedField<ulong> list)
{ {
if (list.Count == 0)
{
return;
}
foreach (var value in list) foreach (var value in list)
{ {
WriteFixed64(fieldNumber, value); WriteFixed64(fieldNumber, value);
...@@ -659,6 +702,10 @@ namespace Google.Protobuf ...@@ -659,6 +702,10 @@ namespace Google.Protobuf
public void WriteSFixed64Array(int fieldNumber, RepeatedField<long> list) public void WriteSFixed64Array(int fieldNumber, RepeatedField<long> list)
{ {
if (list.Count == 0)
{
return;
}
foreach (var value in list) foreach (var value in list)
{ {
WriteSFixed64(fieldNumber, value); WriteSFixed64(fieldNumber, value);
...@@ -667,6 +714,10 @@ namespace Google.Protobuf ...@@ -667,6 +714,10 @@ namespace Google.Protobuf
public void WriteDoubleArray(int fieldNumber, RepeatedField<double> list) public void WriteDoubleArray(int fieldNumber, RepeatedField<double> list)
{ {
if (list.Count == 0)
{
return;
}
foreach (var value in list) foreach (var value in list)
{ {
WriteDouble(fieldNumber, value); WriteDouble(fieldNumber, value);
...@@ -675,6 +726,10 @@ namespace Google.Protobuf ...@@ -675,6 +726,10 @@ namespace Google.Protobuf
public void WriteFloatArray(int fieldNumber, RepeatedField<float> list) public void WriteFloatArray(int fieldNumber, RepeatedField<float> list)
{ {
if (list.Count == 0)
{
return;
}
foreach (var value in list) foreach (var value in list)
{ {
WriteFloat(fieldNumber, value); WriteFloat(fieldNumber, value);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment