Commit 954e7208 authored by Jon Skeet's avatar Jon Skeet

Use expression trees to avoid boxing when converting enums.

parent e38294a6
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions;
using System.Text; using System.Text;
namespace Google.Protobuf namespace Google.Protobuf
...@@ -10,6 +11,8 @@ namespace Google.Protobuf ...@@ -10,6 +11,8 @@ namespace Google.Protobuf
// TODO(jonskeet): For snmall enums, use something lighter-weight than a dictionary? // TODO(jonskeet): For snmall enums, use something lighter-weight than a dictionary?
private static readonly Dictionary<int, T> _byNumber; private static readonly Dictionary<int, T> _byNumber;
private static readonly Dictionary<string, T> _byName; private static readonly Dictionary<string, T> _byName;
private static readonly Func<T, long> toRawValue;
private static readonly Func<long, T> fromRawValue;
private const long UnknownValueBase = 0x100000000L; private const long UnknownValueBase = 0x100000000L;
...@@ -36,6 +39,13 @@ namespace Google.Protobuf ...@@ -36,6 +39,13 @@ namespace Google.Protobuf
{ {
_byName[name] = (T) Enum.Parse(typeof(T), name, false); _byName[name] = (T) Enum.Parse(typeof(T), name, false);
} }
ParameterExpression param1 = Expression.Parameter(typeof(T), "x");
ParameterExpression param2 = Expression.Parameter(typeof(long), "x");
Expression convertedParam1 = Expression.Convert(param1, typeof(long));
Expression convertedParam2 = Expression.Convert(param2, typeof(T));
toRawValue = Expression.Lambda<Func<T, long>>(convertedParam1, param1).Compile();
fromRawValue = Expression.Lambda<Func<long, T>>(convertedParam2, param2).Compile();
} }
/// <summary> /// <summary>
...@@ -71,14 +81,12 @@ namespace Google.Protobuf ...@@ -71,14 +81,12 @@ namespace Google.Protobuf
private static long GetRawValue(T value) private static long GetRawValue(T value)
{ {
// TODO(jonskeet): Try using expression trees to get rid of the boxing here. return toRawValue(value);
return (long)(object)value;
} }
private static T FromRawValue(long value) private static T FromRawValue(long value)
{ {
// TODO(jonskeet): Try using expression trees to get rid of the boxing here. return fromRawValue(value);
return (T)(object)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