Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
954e7208
Commit
954e7208
authored
Jun 09, 2015
by
Jon Skeet
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Use expression trees to avoid boxing when converting enums.
parent
e38294a6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
4 deletions
+12
-4
EnumHelper.cs
csharp/src/ProtocolBuffers/EnumHelper.cs
+12
-4
No files found.
csharp/src/ProtocolBuffers/EnumHelper.cs
View file @
954e7208
using
System
;
using
System.Collections.Generic
;
using
System.Linq
;
using
System.Linq.Expressions
;
using
System.Text
;
namespace
Google.Protobuf
...
...
@@ -10,6 +11,8 @@ namespace Google.Protobuf
// TODO(jonskeet): For snmall enums, use something lighter-weight than a dictionary?
private
static
readonly
Dictionary
<
int
,
T
>
_byNumber
;
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
;
...
...
@@ -36,6 +39,13 @@ namespace Google.Protobuf
{
_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>
...
...
@@ -71,14 +81,12 @@ namespace Google.Protobuf
private
static
long
GetRawValue
(
T
value
)
{
// TODO(jonskeet): Try using expression trees to get rid of the boxing here.
return
(
long
)(
object
)
value
;
return
toRawValue
(
value
);
}
private
static
T
FromRawValue
(
long
value
)
{
// TODO(jonskeet): Try using expression trees to get rid of the boxing here.
return
(
T
)(
object
)
value
;
return
fromRawValue
(
value
);
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment