aboutsummaryrefslogtreecommitdiff
path: root/csharp
diff options
context:
space:
mode:
authorJon Skeet <skeet@pobox.com>2008-08-14 20:38:08 +0100
committerJon Skeet <skeet@pobox.com>2008-08-14 20:38:08 +0100
commit6d0cbe7200e5e20526e346b6db15525ee328850f (patch)
tree887ff3b5d8a40fb1051b13dda6955a2437c2bda8 /csharp
parent0980982095e5fc99a20b627c188d52a8b63248c7 (diff)
downloadprotobuf-6d0cbe7200e5e20526e346b6db15525ee328850f.tar.gz
protobuf-6d0cbe7200e5e20526e346b6db15525ee328850f.tar.bz2
protobuf-6d0cbe7200e5e20526e346b6db15525ee328850f.zip
Use a switch instead of a map for WireFormat.
Diffstat (limited to 'csharp')
-rw-r--r--csharp/ProtocolBuffers.Test/WireFormatTest.cs14
-rw-r--r--csharp/ProtocolBuffers/UnknownFieldSet.cs2
-rw-r--r--csharp/ProtocolBuffers/WireFormat.cs49
3 files changed, 48 insertions, 17 deletions
diff --git a/csharp/ProtocolBuffers.Test/WireFormatTest.cs b/csharp/ProtocolBuffers.Test/WireFormatTest.cs
index 6e4e506f..c5fc3e41 100644
--- a/csharp/ProtocolBuffers.Test/WireFormatTest.cs
+++ b/csharp/ProtocolBuffers.Test/WireFormatTest.cs
@@ -13,6 +13,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
+using System.Reflection;
using Google.ProtocolBuffers.Descriptors;
using Google.ProtocolBuffers.TestProtos;
using NUnit.Framework;
@@ -21,13 +22,16 @@ namespace Google.ProtocolBuffers {
[TestFixture]
public class WireFormatTest {
+ /// <summary>
+ /// Keeps the attributes on FieldType and the switch statement in WireFormat in sync.
+ /// </summary>
[Test]
public void FieldTypeToWireTypeMapping() {
-
- // Just test a few values
- Assert.AreEqual(WireFormat.WireType.Fixed64, WireFormat.FieldTypeToWireFormatMap[FieldType.SFixed64]);
- Assert.AreEqual(WireFormat.WireType.LengthDelimited, WireFormat.FieldTypeToWireFormatMap[FieldType.String]);
- Assert.AreEqual(WireFormat.WireType.LengthDelimited, WireFormat.FieldTypeToWireFormatMap[FieldType.Message]);
+ foreach (FieldInfo field in typeof(FieldType).GetFields(BindingFlags.Static | BindingFlags.Public)) {
+ FieldType fieldType = (FieldType)field.GetValue(null);
+ FieldMappingAttribute mapping = (FieldMappingAttribute)field.GetCustomAttributes(typeof(FieldMappingAttribute), false)[0];
+ Assert.AreEqual(mapping.WireType, WireFormat.GetWireType(fieldType));
+ }
}
[Test]
diff --git a/csharp/ProtocolBuffers/UnknownFieldSet.cs b/csharp/ProtocolBuffers/UnknownFieldSet.cs
index 468970e5..4e3e4e95 100644
--- a/csharp/ProtocolBuffers/UnknownFieldSet.cs
+++ b/csharp/ProtocolBuffers/UnknownFieldSet.cs
@@ -480,7 +480,7 @@ namespace Google.ProtocolBuffers {
}
// Unknown field or wrong wire type. Skip.
- if (field == null || wireType != WireFormat.FieldTypeToWireFormatMap[field.FieldType]) {
+ if (field == null || wireType != WireFormat.GetWireType(field.FieldType)) {
return MergeFieldFrom(tag, input);
}
diff --git a/csharp/ProtocolBuffers/WireFormat.cs b/csharp/ProtocolBuffers/WireFormat.cs
index 6def39e4..0ea93322 100644
--- a/csharp/ProtocolBuffers/WireFormat.cs
+++ b/csharp/ProtocolBuffers/WireFormat.cs
@@ -13,6 +13,7 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
+using System;
using System.Reflection;
using Google.ProtocolBuffers.Descriptors;
using System.Collections.Generic;
@@ -77,19 +78,45 @@ namespace Google.ProtocolBuffers {
}
/// <summary>
- /// Immutable mapping from field type to wire type. Built using the attributes on
- /// FieldType values.
+ /// Converts a field type to its wire type. Done with a switch for the sake
+ /// of speed - this is significantly faster than a dictionary lookup.
/// </summary>
- public static readonly IDictionary<FieldType, WireType> FieldTypeToWireFormatMap = MapFieldTypes();
-
- private static IDictionary<FieldType, WireType> MapFieldTypes() {
- var map = new Dictionary<FieldType, WireType>();
- foreach (FieldInfo field in typeof(FieldType).GetFields(BindingFlags.Static | BindingFlags.Public)) {
- FieldType fieldType = (FieldType) field.GetValue(null);
- FieldMappingAttribute mapping = (FieldMappingAttribute)field.GetCustomAttributes(typeof(FieldMappingAttribute), false)[0];
- map[fieldType] = mapping.WireType;
+ public static WireType GetWireType(FieldType fieldType) {
+ switch (fieldType) {
+ case FieldType.Double:
+ return WireType.Fixed64;
+ case FieldType.Float:
+ return WireType.Fixed32;
+ case FieldType.Int64:
+ case FieldType.UInt64:
+ case FieldType.Int32:
+ return WireType.Varint;
+ case FieldType.Fixed64:
+ return WireType.Fixed64;
+ case FieldType.Fixed32:
+ return WireType.Fixed32;
+ case FieldType.Bool:
+ return WireType.Varint;
+ case FieldType.String:
+ return WireType.LengthDelimited;
+ case FieldType.Group:
+ return WireType.StartGroup;
+ case FieldType.Message:
+ case FieldType.Bytes:
+ return WireType.LengthDelimited;
+ case FieldType.UInt32:
+ return WireType.Varint;
+ case FieldType.SFixed32:
+ return WireType.Fixed32;
+ case FieldType.SFixed64:
+ return WireType.Fixed64;
+ case FieldType.SInt32:
+ case FieldType.SInt64:
+ case FieldType.Enum:
+ return WireType.Varint;
+ default:
+ throw new ArgumentOutOfRangeException("No such field type");
}
- return Dictionaries.AsReadOnly(map);
}
}
}