1
1
using System ;
2
+ using System . Runtime . CompilerServices ;
2
3
using System . Text ;
3
4
4
5
namespace ProtoZeroSharp ;
5
6
6
7
internal static class ProtobufFormat
7
8
{
9
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
8
10
private static ulong EncodeKey ( int fieldNumber , ProtoWireType wireType )
9
11
{
10
12
return ( ( ulong ) fieldNumber << 3 ) | ( ulong ) wireType ;
@@ -23,6 +25,7 @@ private static ulong EncodeKey(int fieldNumber, ProtoWireType wireType)
23
25
/// <summary>
24
26
/// Returns the upper bound of the length of a bytes field.
25
27
/// </summary>
28
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
26
29
internal static int BytesFieldLenUpperBound ( int payloadLength ) => VarInt . MaxBytesCount * 2 + payloadLength ; // message id + length + payload
27
30
28
31
/// <summary>
@@ -33,25 +36,35 @@ private static ulong EncodeKey(int fieldNumber, ProtoWireType wireType)
33
36
/// <param name="fieldNumber">Proto message id</param>
34
37
/// <param name="value">Value of the message</param>
35
38
/// <returns>Number of bytes written</returns>
39
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
36
40
internal static int WriteVarIntField ( Span < byte > output , int fieldNumber , ulong value )
37
41
{
38
42
int written = VarInt . WriteVarint ( output , EncodeKey ( fieldNumber , ProtoWireType . VarInt ) ) ;
39
43
written += VarInt . WriteVarint ( output . Slice ( written ) , value ) ;
40
44
return written ;
41
45
}
42
46
47
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
43
48
internal static int WriteLengthFieldHeader ( Span < byte > output , int messageId )
44
49
{
45
50
int written = VarInt . WriteVarint ( output , EncodeKey ( messageId , ProtoWireType . Length ) ) ;
46
51
return written ;
47
52
}
48
53
54
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
49
55
internal static int WriteLengthFieldLength ( Span < byte > output , int length )
50
56
{
51
57
int written = VarInt . WriteVarint ( output , length ) ;
52
58
return written ;
53
59
}
54
60
61
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
62
+ internal static int WriteLengthFieldLength ( Span < byte > output , int length , int fixedBytesSize )
63
+ {
64
+ int written = VarInt . WriteVarintFixedSize ( output , ( ulong ) length , fixedBytesSize ) ;
65
+ return written ;
66
+ }
67
+
55
68
/// <summary>
56
69
/// Writes a bytes buffer message to a buffer.
57
70
/// The buffer must be at least 20 bytes + payload length long to fit any buffer.
@@ -60,6 +73,7 @@ internal static int WriteLengthFieldLength(Span<byte> output, int length)
60
73
/// <param name="fieldNumber">Proto message id</param>
61
74
/// <param name="payload">Buffer to write</param>
62
75
/// <returns>Number of bytes written</returns>
76
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
63
77
internal static int WriteBytes ( Span < byte > output , int fieldNumber , ReadOnlySpan < byte > payload )
64
78
{
65
79
#if DEBUG
@@ -82,6 +96,7 @@ internal static int WriteBytes(Span<byte> output, int fieldNumber, ReadOnlySpan<
82
96
/// <param name="payloadBytesCount">Precalculatd number of bytes of utf-8 encoding of the given payload</param>
83
97
/// <param name="payload">String to write</param>
84
98
/// <returns>Number of bytes written</returns>
99
+ [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
85
100
internal static unsafe int WriteString ( Span < byte > output , int fieldNumber , int payloadBytesCount , string payload )
86
101
{
87
102
#if DEBUG
0 commit comments