Skip to content

Commit 3e98e1b

Browse files
Use byte as index for known type names
Uses a byte instead of an int to index known type names. We'll need to revisit this if the number of known types grows beyond 256. To make serialization a bit easier, I've added methods for reading/writing bytes to `JsonDataReader` and `JsonDataWriter`.
1 parent a6f804b commit 3e98e1b

File tree

6 files changed

+72
-12
lines changed

6 files changed

+72
-12
lines changed

src/Compiler/Microsoft.CodeAnalysis.Razor.Compiler/src/Language/TypeNameObject.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ namespace Microsoft.AspNetCore.Razor.Language;
1313
internal readonly struct TypeNameObject
1414
{
1515
private static readonly ImmutableArray<string> s_knownTypeNames;
16-
private static readonly FrozenDictionary<string, int> s_typeNameToIndex;
16+
private static readonly FrozenDictionary<string, byte> s_typeNameToIndex;
1717

1818
private static readonly int s_booleanIndex;
1919
private static readonly int s_stringIndex;
2020

2121
static TypeNameObject()
2222
{
2323
var knownTypeNames = ImmutableArray.CreateBuilder<string>();
24-
var typeNameToIndex = new Dictionary<string, int>(StringComparer.Ordinal);
24+
var typeNameToIndex = new Dictionary<string, byte>(StringComparer.Ordinal);
2525

2626
Add<object>("object");
2727
s_booleanIndex = Add<bool>("bool");
@@ -50,7 +50,8 @@ static TypeNameObject()
5050
int Add<T>(string? alias = null)
5151
{
5252
var fullName = typeof(T).FullName!;
53-
var index = knownTypeNames.Count;
53+
Debug.Assert(knownTypeNames.Count < byte.MaxValue, "Too many known type names to fit in a byte index.");
54+
var index = (byte)knownTypeNames.Count;
5455
knownTypeNames.Add(fullName);
5556
typeNameToIndex.Add(fullName, index);
5657

@@ -63,10 +64,10 @@ int Add<T>(string? alias = null)
6364
}
6465
}
6566

66-
private readonly int? _index;
67+
private readonly byte? _index;
6768
private readonly string? _stringValue;
6869

69-
public TypeNameObject(int index)
70+
public TypeNameObject(byte index)
7071
{
7172
Debug.Assert(index >= 0 && index < s_knownTypeNames.Length);
7273

@@ -84,7 +85,7 @@ public TypeNameObject(string? stringValue)
8485

8586
public bool IsNull => _index is null && _stringValue is null;
8687

87-
public int? Index => _index;
88+
public byte? Index => _index;
8889
public string? StringValue => _stringValue;
8990

9091
public static TypeNameObject From(string? typeName)
@@ -104,7 +105,7 @@ public static TypeNameObject From(string? typeName)
104105

105106
public readonly string? GetTypeName()
106107
{
107-
if (_index is int index)
108+
if (_index is byte index)
108109
{
109110
return s_knownTypeNames[index];
110111
}
@@ -114,7 +115,7 @@ public static TypeNameObject From(string? typeName)
114115

115116
public void AppendToChecksum(in Checksum.Builder builder)
116117
{
117-
if (_index is int index)
118+
if (_index is byte index)
118119
{
119120
builder.AppendData(index);
120121
}

src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Serialization/MessagePack/Formatters/TagHelpers/TypeNameObjectFormatter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public override TypeNameObject Deserialize(ref MessagePackReader reader, Seriali
3737
switch (typeNameKind)
3838
{
3939
case TypeNameKind.Index:
40-
var index = reader.ReadInt32();
40+
var index = reader.ReadByte();
4141
return new(index);
4242
case TypeNameKind.String:
4343
var fullName = CachedStringFormatter.Instance.Deserialize(ref reader, options).AssumeNotNull();
@@ -58,7 +58,7 @@ public override void Serialize(ref MessagePackWriter writer, TypeNameObject valu
5858

5959
writer.WriteArrayHeader(PropertyCount);
6060

61-
if (value.Index is int index)
61+
if (value.Index is byte index)
6262
{
6363
writer.Write((byte)TypeNameKind.Index);
6464
writer.Write(index);

src/Shared/Microsoft.AspNetCore.Razor.Serialization.Json/JsonDataReader.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,41 @@ public bool TryReadBoolean(string propertyName, out bool value)
139139
return false;
140140
}
141141

142+
public byte ReadByte()
143+
{
144+
_reader.CheckToken(JsonToken.Integer);
145+
146+
var result = Convert.ToByte(_reader.Value);
147+
_reader.Read();
148+
149+
return result;
150+
}
151+
152+
public byte ReadByteOrDefault(string propertyName, byte defaultValue = default)
153+
=> TryReadPropertyName(propertyName) ? ReadByte() : defaultValue;
154+
155+
public byte ReadByteOrZero(string propertyName)
156+
=> TryReadPropertyName(propertyName) ? ReadByte() : (byte)0;
157+
158+
public bool TryReadByte(string propertyName, out byte value)
159+
{
160+
if (TryReadPropertyName(propertyName))
161+
{
162+
value = ReadByte();
163+
return true;
164+
}
165+
166+
value = default;
167+
return false;
168+
}
169+
170+
public byte ReadByte(string propertyName)
171+
{
172+
ReadPropertyName(propertyName);
173+
174+
return ReadByte();
175+
}
176+
142177
public int ReadInt32()
143178
{
144179
_reader.CheckToken(JsonToken.Integer);

src/Shared/Microsoft.AspNetCore.Razor.Serialization.Json/JsonDataWriter.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,30 @@ public void WriteIfNotFalse(string propertyName, bool value)
6868
}
6969
}
7070

71+
public void Write(byte value)
72+
{
73+
_writer.WriteValue(value);
74+
}
75+
76+
public void Write(string propertyName, byte value)
77+
{
78+
_writer.WritePropertyName(propertyName);
79+
_writer.WriteValue(value);
80+
}
81+
82+
public void WriteIfNotZero(string propertyName, byte value)
83+
{
84+
WriteIfNotDefault(propertyName, value, defaultValue: (byte)0);
85+
}
86+
87+
public void WriteIfNotDefault(string propertyName, byte value, byte defaultValue)
88+
{
89+
if (value != defaultValue)
90+
{
91+
Write(propertyName, value);
92+
}
93+
}
94+
7195
public void Write(int value)
7296
{
7397
_writer.WriteValue(value);

src/Shared/Microsoft.AspNetCore.Razor.Serialization.Json/ObjectReaders_TagHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ static TypeNameObject ReadTypeNameObject(JsonDataReader reader, string propertyN
219219

220220
if (reader.IsInteger)
221221
{
222-
var index = reader.ReadInt32();
222+
var index = reader.ReadByte();
223223
return new(index);
224224
}
225225

src/Shared/Microsoft.AspNetCore.Razor.Serialization.Json/ObjectWriters_TagHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ static void WriteDocumentationObject(JsonDataWriter writer, string propertyName,
6262

6363
static void WriteTypeNameObject(JsonDataWriter writer, string propertyName, TypeNameObject typeNameObject)
6464
{
65-
if (typeNameObject.Index is int index)
65+
if (typeNameObject.Index is byte index)
6666
{
6767
writer.Write(propertyName, index);
6868
}

0 commit comments

Comments
 (0)