aboutsummaryrefslogtreecommitdiff
path: root/csharp/src
diff options
context:
space:
mode:
authorJon Skeet <skeet@pobox.com>2015-05-09 19:00:23 +0100
committerJon Skeet <skeet@pobox.com>2015-05-09 19:00:23 +0100
commitc58b2c66448f83c25da0251fe6cf5b12299fa581 (patch)
tree633eb209f649c76b7bbbb577ae6d583111db7080 /csharp/src
parent34fb6669555279f836f34ecc40a78f36d5e2ea10 (diff)
parent6f9da37b0dfa586b3167a9aa2f276059664953e8 (diff)
downloadprotobuf-c58b2c66448f83c25da0251fe6cf5b12299fa581.tar.gz
protobuf-c58b2c66448f83c25da0251fe6cf5b12299fa581.tar.bz2
protobuf-c58b2c66448f83c25da0251fe6cf5b12299fa581.zip
Merge pull request #352 from jtattermusch/csharp_performance_fix
Performance optimization for small messages without unknown fields
Diffstat (limited to 'csharp/src')
-rw-r--r--csharp/src/ProtocolBuffers/UnknownFieldSet.cs28
1 files changed, 24 insertions, 4 deletions
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
/// </summary>
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
{
+ // Avoid creating enumerator for the most common code path.
+ if (fields.Count == 0)
+ {
+ return 0;
+ }
+
int result = 0;
foreach (KeyValuePair<int, UnknownField> entry in fields)
{
@@ -209,9 +219,13 @@ namespace Google.ProtocolBuffers
/// </summary>
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
{
+ // Avoid creating enumerator for the most common code path.
+ if (fields.Count == 0)
+ {
+ return 0;
+ }
+
int result = 0;
foreach (KeyValuePair<int, UnknownField> entry in fields)
{