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
e305e56c
Commit
e305e56c
authored
Nov 04, 2019
by
Chris Bacon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
C#: Optimize parsing of some primitive and wrapper types
parent
4668a332
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
2 deletions
+51
-2
CodedInputStream.cs
csharp/src/Google.Protobuf/CodedInputStream.cs
+0
-0
FieldCodec.cs
csharp/src/Google.Protobuf/FieldCodec.cs
+34
-1
InvalidProtocolBufferException.cs
csharp/src/Google.Protobuf/InvalidProtocolBufferException.cs
+17
-1
No files found.
csharp/src/Google.Protobuf/CodedInputStream.cs
View file @
e305e56c
This diff is collapsed.
Click to expand it.
csharp/src/Google.Protobuf/FieldCodec.cs
View file @
e305e56c
...
...
@@ -507,7 +507,7 @@ namespace Google.Protobuf
{
var
nestedCodec
=
WrapperCodecs
.
GetCodec
<
T
>();
return
new
FieldCodec
<
T
?>(
input
=>
WrapperCodecs
.
Read
<
T
>(
input
,
nestedCodec
),
WrapperCodecs
.
GetReader
<
T
>(
),
(
output
,
value
)
=>
WrapperCodecs
.
Write
<
T
>(
output
,
value
.
Value
,
nestedCodec
),
(
CodedInputStream
i
,
ref
T
?
v
)
=>
v
=
WrapperCodecs
.
Read
<
T
>(
i
,
nestedCodec
),
(
ref
T
?
v
,
T
?
v2
)
=>
{
if
(
v2
.
HasValue
)
{
v
=
v2
;
}
return
v
.
HasValue
;
},
...
...
@@ -539,6 +539,22 @@ namespace Google.Protobuf
{
typeof
(
ByteString
),
ForBytes
(
WireFormat
.
MakeTag
(
WrappersReflection
.
WrapperValueFieldNumber
,
WireFormat
.
WireType
.
LengthDelimited
))
}
};
private
static
readonly
Dictionary
<
System
.
Type
,
Func
<
object
>>
Readers
=
new
Dictionary
<
System
.
Type
,
Func
<
object
>>
{
// TODO: Provide more optimized readers.
{
typeof
(
bool
),
null
},
{
typeof
(
int
),
null
},
{
typeof
(
long
),
()
=>
(
Func
<
CodedInputStream
,
long
?>)
CodedInputStream
.
ReadInt64Wrapper
},
{
typeof
(
uint
),
null
},
{
typeof
(
ulong
),
null
},
{
typeof
(
float
),
null
},
{
typeof
(
double
),
()
=>
BitConverter
.
IsLittleEndian
?
(
Func
<
CodedInputStream
,
double
?>)
CodedInputStream
.
ReadDoubleWrapperLittleEndian
:
(
Func
<
CodedInputStream
,
double
?>)
CodedInputStream
.
ReadDoubleWrapperBigEndian
},
{
typeof
(
string
),
null
},
{
typeof
(
ByteString
),
null
},
};
/// <summary>
/// Returns a field codec which effectively wraps a value of type T in a message.
///
...
...
@@ -553,6 +569,23 @@ namespace Google.Protobuf
return
(
FieldCodec
<
T
>)
value
;
}
internal
static
Func
<
CodedInputStream
,
T
?>
GetReader
<
T
>()
where
T
:
struct
{
Func
<
object
>
value
;
if
(!
Readers
.
TryGetValue
(
typeof
(
T
),
out
value
))
{
throw
new
InvalidOperationException
(
"Invalid type argument requested for wrapper reader: "
+
typeof
(
T
));
}
if
(
value
==
null
)
{
// Return default unoptimized reader for the wrapper type.
var
nestedCoded
=
GetCodec
<
T
>();
return
input
=>
Read
<
T
>(
input
,
nestedCoded
);
}
// Return optimized read for the wrapper type.
return
(
Func
<
CodedInputStream
,
T
?>)
value
();
}
internal
static
T
Read
<
T
>(
CodedInputStream
input
,
FieldCodec
<
T
>
codec
)
{
int
length
=
input
.
ReadLength
();
...
...
csharp/src/Google.Protobuf/InvalidProtocolBufferException.cs
View file @
e305e56c
...
...
@@ -136,5 +136,20 @@ namespace Google.Protobuf
{
return
new
InvalidProtocolBufferException
(
"Message was missing required fields"
);
}
}
internal
static
InvalidProtocolBufferException
InvalidWrapperMessageLength
()
{
return
new
InvalidProtocolBufferException
(
"Wrapper type message length is incorrect."
);
}
internal
static
InvalidProtocolBufferException
InvalidWrapperMessageTag
()
{
return
new
InvalidProtocolBufferException
(
"Wrapper type message tag is incorrect."
);
}
internal
static
InvalidProtocolBufferException
InvalidWrapperMessageExtraFields
()
{
return
new
InvalidProtocolBufferException
(
"Wrapper type message contains invalid extra field(s)."
);
}
}
}
\ No newline at end of file
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