Merge pull request #352 from jtattermusch/csharp_performance_fix

Performance optimization for small messages without unknown fields
pull/366/head
Jon Skeet 10 years ago
commit c58b2c6644
  1. 28
      csharp/src/ProtocolBuffers/UnknownFieldSet.cs

@ -125,9 +125,13 @@ namespace Google.ProtocolBuffers
/// </summary> /// </summary>
public void WriteTo(ICodedOutputStream output) public void WriteTo(ICodedOutputStream output)
{ {
foreach (KeyValuePair<int, UnknownField> entry in fields) // Avoid creating enumerator for the most common code path.
if (fields.Count > 0)
{ {
entry.Value.WriteTo(entry.Key, output); foreach (KeyValuePair<int, UnknownField> entry in fields)
{
entry.Value.WriteTo(entry.Key, output);
}
} }
} }
@ -138,6 +142,12 @@ namespace Google.ProtocolBuffers
{ {
get get
{ {
// Avoid creating enumerator for the most common code path.
if (fields.Count == 0)
{
return 0;
}
int result = 0; int result = 0;
foreach (KeyValuePair<int, UnknownField> entry in fields) foreach (KeyValuePair<int, UnknownField> entry in fields)
{ {
@ -209,9 +219,13 @@ namespace Google.ProtocolBuffers
/// </summary> /// </summary>
public void WriteAsMessageSetTo(ICodedOutputStream output) public void WriteAsMessageSetTo(ICodedOutputStream output)
{ {
foreach (KeyValuePair<int, UnknownField> entry in fields) // Avoid creating enumerator for the most common code path.
if (fields.Count > 0)
{ {
entry.Value.WriteAsMessageSetExtensionTo(entry.Key, output); foreach (KeyValuePair<int, UnknownField> entry in fields)
{
entry.Value.WriteAsMessageSetExtensionTo(entry.Key, output);
}
} }
} }
@ -223,6 +237,12 @@ namespace Google.ProtocolBuffers
{ {
get get
{ {
// Avoid creating enumerator for the most common code path.
if (fields.Count == 0)
{
return 0;
}
int result = 0; int result = 0;
foreach (KeyValuePair<int, UnknownField> entry in fields) foreach (KeyValuePair<int, UnknownField> entry in fields)
{ {

Loading…
Cancel
Save