Skip to content

Commit 33e4e97

Browse files
committed
[Proto] Extract into helpers for tag
1 parent c4819b6 commit 33e4e97

7 files changed

Lines changed: 31 additions & 26 deletions

File tree

Lagrange.Proto/Nodes/ProtoArray.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public override void WriteTo(int field, ProtoWriter writer)
3737
foreach (var node in _list)
3838
{
3939
if (first) first = false;
40-
else writer.EncodeVarInt(field << 3 | (int)node.WireType);
40+
else writer.EncodeVarInt(ProtoHelper.GetTag(field, node.WireType));
4141

4242
node.WriteTo(field, writer);
4343
}
@@ -47,7 +47,7 @@ public override int Measure(int field)
4747
{
4848
if (_list.Count == 0) return 0;
4949

50-
int size = ProtoHelper.GetVarIntLength(field << 3 | (int)WireType) * (_list.Count - 1);
50+
int size = ProtoHelper.GetVarIntLength(ProtoHelper.GetTag(field, WireType)) * (_list.Count - 1);
5151
foreach (var node in _list)
5252
{
5353
size += node.Measure(field);
@@ -66,4 +66,4 @@ private protected override void SetItem(int index, ProtoNode value)
6666
DetachParent(_list[index]);
6767
_list[index] = value;
6868
}
69-
}
69+
}

Lagrange.Proto/Nodes/ProtoObject.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public override void WriteTo(int field, ProtoWriter writer)
1414

1515
foreach (var (f, node) in _fields)
1616
{
17-
writer.EncodeVarInt((uint)f << 3 | (uint)node.WireType);
17+
writer.EncodeVarInt(ProtoHelper.GetTag(f, node.WireType));
1818
node.WriteTo(f, writer);
1919
}
2020
}
@@ -26,7 +26,7 @@ public override int Measure(int field)
2626
{
2727
if (node is ProtoArray { Count: 0 }) continue;
2828

29-
size += ProtoHelper.GetVarIntLength((uint)f << 3 | (uint)node.WireType);
29+
size += ProtoHelper.GetVarIntLength(ProtoHelper.GetTag(f, node.WireType));
3030
size += node.Measure(f);
3131
}
3232
return size;
@@ -89,7 +89,7 @@ public byte[] Serialize()
8989
{
9090
if (node is ProtoArray { Count: 0 }) continue;
9191

92-
writer.EncodeVarInt((uint)f << 3 | (uint)node.WireType);
92+
writer.EncodeVarInt(ProtoHelper.GetTag(f, node.WireType));
9393
node.WriteTo(f, writer);
9494
}
9595
writer.Flush();
@@ -109,7 +109,7 @@ public void Serialize(IBufferWriter<byte> buffer)
109109
{
110110
foreach (var (f, node) in _fields)
111111
{
112-
writer.EncodeVarInt((uint)f << 3 | (uint)node.WireType);
112+
writer.EncodeVarInt(ProtoHelper.GetTag(f, node.WireType));
113113
node.WriteTo(f, writer);
114114
}
115115
writer.Flush();
@@ -119,4 +119,4 @@ public void Serialize(IBufferWriter<byte> buffer)
119119
ProtoWriterCache.ReturnWriter(writer);
120120
}
121121
}
122-
}
122+
}

Lagrange.Proto/Serialization/Converter/Collection/ProtoRepeatedConverter.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public abstract class ProtoRepeatedConverter<TCollection, TElement> : ProtoConve
1212

1313
public override void Write(int field, WireType wireType, ProtoWriter writer, TCollection value)
1414
{
15-
int tag = (field << 3) | (byte)wireType;
15+
uint tag = ProtoHelper.GetTag(field, wireType);
1616
bool first = true;
1717

1818
foreach (var item in value)
@@ -25,7 +25,7 @@ public override void Write(int field, WireType wireType, ProtoWriter writer, TCo
2525

2626
public override void WriteWithNumberHandling(int field, WireType wireType, ProtoWriter writer, TCollection value, ProtoNumberHandling numberHandling)
2727
{
28-
int tag = (field << 3) | (byte)wireType;
28+
uint tag = ProtoHelper.GetTag(field, wireType);
2929
bool first = true;
3030

3131
foreach (var item in value)
@@ -38,7 +38,7 @@ public override void WriteWithNumberHandling(int field, WireType wireType, Proto
3838

3939
public override int Measure(int field, WireType wireType, TCollection value)
4040
{
41-
int tag = (field << 3) | (byte)wireType;
41+
uint tag = ProtoHelper.GetTag(field, wireType);
4242
int size = ProtoHelper.GetVarIntLength(tag) * (value.Count - 1); // the length of the first item is not counted as it would be added by the caller
4343

4444
foreach (var item in value)
@@ -63,7 +63,7 @@ public override TCollection Read(int field, WireType wireType, ref ProtoReader r
6363
if ((tag = reader.DecodeVarInt<int>() >> 3) != field) break;
6464
}
6565

66-
reader.Rewind(-ProtoHelper.GetVarIntLength(tag << 3));
66+
reader.Rewind(-ProtoHelper.GetVarIntLength(ProtoHelper.GetTag(tag, WireType.VarInt)));
6767

6868
return Finalize(collection, state);
6969
}
@@ -81,7 +81,7 @@ public override TCollection ReadWithNumberHandling(int field, WireType wireType,
8181
if ((tag = reader.DecodeVarInt<int>() >> 3) != field) break;
8282
}
8383

84-
reader.Rewind(-ProtoHelper.GetVarIntLength(tag << 3));
84+
reader.Rewind(-ProtoHelper.GetVarIntLength(ProtoHelper.GetTag(tag, WireType.VarInt)));
8585

8686
return Finalize(collection, state);
8787
}
@@ -93,4 +93,4 @@ public override TCollection ReadWithNumberHandling(int field, WireType wireType,
9393
private protected abstract void Add(TElement item, TCollection collection, object? state);
9494

9595
private protected virtual TCollection Finalize(TCollection collection, object? state) => collection;
96-
}
96+
}

Lagrange.Proto/Serialization/Converter/Nodes/ProtoArrayConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public override ProtoArray Read(int field, WireType wireType, ref ProtoReader re
3131
if ((tag = reader.DecodeVarInt<int>() >> 3) != field) break;
3232
}
3333

34-
reader.Rewind(-ProtoHelper.GetVarIntLength(tag << 3));
34+
reader.Rewind(-ProtoHelper.GetVarIntLength(ProtoHelper.GetTag(tag, WireType.VarInt)));
3535
return array;
3636
}
37-
}
37+
}

Lagrange.Proto/Serialization/Metadata/ProtoFieldInfo.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ public override void Read(ref ProtoReader reader, object target)
326326
if ((tag = reader.DecodeVarInt<int>() >> 3) != Field) break;
327327
}
328328

329-
reader.Rewind(-ProtoHelper.GetVarIntLength(tag << 3));
329+
reader.Rewind(-ProtoHelper.GetVarIntLength(ProtoHelper.GetTag(tag, WireType.VarInt)));
330330

331331
_typedSet(target, map);
332332
}
@@ -336,7 +336,7 @@ public override void Write(ProtoWriter writer, object target)
336336
Debug.Assert(_typedGet != null && KeyEffectiveConverter != null && ValueEffectiveConverter != null);
337337

338338
var map = _typedGet(target);
339-
int tag = (Field << 3) | (byte)WireType;
339+
uint tag = ProtoHelper.GetTag(Field, WireType);
340340
bool first = true;
341341

342342
foreach (var (key, value) in map)
@@ -346,11 +346,11 @@ public override void Write(ProtoWriter writer, object target)
346346

347347
writer.EncodeVarInt(KeyEffectiveConverter.Measure(1, KeyWireType, key) + ValueEffectiveConverter.Measure(2, ValueWireType, value) + 2); // 2 for the tag of key and value
348348

349-
writer.WriteRawByte((byte)(8 | (byte)KeyWireType));
349+
writer.WriteRawByte((byte)ProtoHelper.GetTag(1, KeyWireType));
350350
if (NumberHandling == ProtoNumberHandling.Default) KeyEffectiveConverter.Write(1, KeyWireType, writer, key);
351351
else KeyEffectiveConverter.WriteWithNumberHandling(1, KeyWireType, writer, key, NumberHandling);
352352

353-
writer.WriteRawByte((byte)(16 | (byte)ValueWireType));
353+
writer.WriteRawByte((byte)ProtoHelper.GetTag(2, ValueWireType));
354354
if (ValueNumberHandling == ProtoNumberHandling.Default) ValueEffectiveConverter.Write(2, ValueWireType, writer, value);
355355
else ValueEffectiveConverter.WriteWithNumberHandling(2, ValueWireType, writer, value, ValueNumberHandling);
356356
}
@@ -361,7 +361,7 @@ public override int Measure(object target)
361361
Debug.Assert(_typedGet != null);
362362

363363
var map = _typedGet(target);
364-
int tag = (Field << 3) | (byte)WireType;
364+
uint tag = ProtoHelper.GetTag(Field, WireType);
365365

366366
int size = ProtoHelper.GetVarIntLength(tag) * (map.Count - 1) + 2 * map.Count; // the length of the first item is not counted as it would be added by the caller
367367
foreach (var (key, value) in map)
@@ -372,4 +372,4 @@ public override int Measure(object target)
372372

373373
return size;
374374
}
375-
}
375+
}

Lagrange.Proto/Serialization/Metadata/ProtoTypeResolver.Dynamic.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Reflection;
44
using System.Runtime.CompilerServices;
55
using Lagrange.Proto.Nodes;
6+
using Lagrange.Proto.Utility;
67

78
namespace Lagrange.Proto.Serialization.Metadata;
89

@@ -74,7 +75,7 @@ internal static ProtoObjectInfo<T> CreateObjectInfo<T>()
7475
var fieldInfo = CreateFieldInfo(typeof(T), field);
7576
if (fieldInfo == null) continue;
7677

77-
uint tag = ((uint)fieldInfo.Field << 3) | (byte)fieldInfo.WireType;
78+
uint tag = ProtoHelper.GetTag(fieldInfo.Field, fieldInfo.WireType);
7879
if (fields.ContainsKey(tag)) ThrowHelper.ThrowInvalidOperationException_DuplicateField(typeof(T), fieldInfo.Field);
7980
fields[tag] = fieldInfo;
8081
}
@@ -84,7 +85,7 @@ internal static ProtoObjectInfo<T> CreateObjectInfo<T>()
8485
var fieldInfo = CreateFieldInfo(typeof(T), field);
8586
if (fieldInfo == null) continue;
8687

87-
uint tag = ((uint)fieldInfo.Field << 3) | (byte)fieldInfo.WireType;
88+
uint tag = ProtoHelper.GetTag(fieldInfo.Field, fieldInfo.WireType);
8889
if (fields.ContainsKey(tag)) ThrowHelper.ThrowInvalidOperationException_DuplicateField(typeof(T), fieldInfo.Field);
8990
fields[tag] = fieldInfo;
9091
}
@@ -301,4 +302,4 @@ private static bool IsMapType(
301302

302303
return false;
303304
}
304-
}
305+
}

Lagrange.Proto/Utility/ProtoHelper.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Runtime.CompilerServices;
33
using System.Text;
44
using Lagrange.Proto.Primitives;
5+
using Lagrange.Proto.Serialization;
56

67
namespace Lagrange.Proto.Utility;
78

@@ -28,6 +29,9 @@ static ProtoHelper()
2829

2930
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3031
internal static int GetVarIntMax(int length) => VarIntBoundaries[length] - 1;
32+
33+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
34+
internal static uint GetTag(int field, WireType wireType) => ((uint)field << 3) | (byte)wireType;
3135

3236
[MethodImpl(MethodImplOptions.AggressiveInlining)]
3337
public static unsafe int GetVarIntLength<T>(T value) where T : unmanaged, INumberBase<T>
@@ -98,4 +102,4 @@ public static int CountBytes(ReadOnlySpan<byte> str)
98102
{
99103
return GetVarIntLength(str.Length) + str.Length;
100104
}
101-
}
105+
}

0 commit comments

Comments
 (0)