From 6f9da37b0dfa586b3167a9aa2f276059664953e8 Mon Sep 17 00:00:00 2001 From: Jan Tattermusch Date: Thu, 7 May 2015 08:40:36 -0700 Subject: Performance optimization for small messages without unknown fields --- csharp/src/ProtocolBuffers/UnknownFieldSet.cs | 28 +++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'csharp') diff --git a/csharp/src/ProtocolBuffers/UnknownFieldSet.cs b/csharp/src/ProtocolBuffers/UnknownFieldSet.cs index 98aa04c7..d5d0675d 100644 --- a/csharp/src/ProtocolBuffers/UnknownFieldSet.cs +++ b/csharp/src/ProtocolBuffers/UnknownFieldSet.cs @@ -125,9 +125,13 @@ namespace Google.ProtocolBuffers /// public void WriteTo(ICodedOutputStream output) { - foreach (KeyValuePair entry in fields) + // Avoid creating enumerator for the most common code path. + if (fields.Count > 0) { - entry.Value.WriteTo(entry.Key, output); + foreach (KeyValuePair entry in fields) + { + entry.Value.WriteTo(entry.Key, output); + } } } @@ -138,6 +142,12 @@ namespace Google.ProtocolBuffers { get { + // Avoid creating enumerator for the most common code path. + if (fields.Count == 0) + { + return 0; + } + int result = 0; foreach (KeyValuePair entry in fields) { @@ -209,9 +219,13 @@ namespace Google.ProtocolBuffers /// public void WriteAsMessageSetTo(ICodedOutputStream output) { - foreach (KeyValuePair entry in fields) + // Avoid creating enumerator for the most common code path. + if (fields.Count > 0) { - entry.Value.WriteAsMessageSetExtensionTo(entry.Key, output); + foreach (KeyValuePair entry in fields) + { + entry.Value.WriteAsMessageSetExtensionTo(entry.Key, output); + } } } @@ -223,6 +237,12 @@ namespace Google.ProtocolBuffers { get { + // Avoid creating enumerator for the most common code path. + if (fields.Count == 0) + { + return 0; + } + int result = 0; foreach (KeyValuePair entry in fields) { -- cgit v1.2.3