Skip to content

Commit a906f7a

Browse files
committed
Add typed fast paths in ReadArray for common element types
Avoid Array.CreateInstance + Array.SetValue reflection overhead for object[], string[], and Type[] arrays in metadata deserialization.
1 parent d57243a commit a906f7a

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

src/Microsoft.VisualStudio.Composition/Configuration/SerializationContextBase.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,41 @@ protected Array ReadArray(BinaryReader reader, Func<object?> itemReader, Type el
756756
throw new NotSupportedException();
757757
}
758758

759+
// Use typed fast paths for common element types to avoid
760+
// the overhead of Array.CreateInstance + Array.SetValue (reflection + boxing).
761+
if (elementType == typeof(object))
762+
{
763+
var array = new object?[(int)count];
764+
for (int i = 0; i < array.Length; i++)
765+
{
766+
array[i] = itemReader();
767+
}
768+
769+
return array;
770+
}
771+
772+
if (elementType == typeof(string))
773+
{
774+
var array = new string?[(int)count];
775+
for (int i = 0; i < array.Length; i++)
776+
{
777+
array[i] = (string?)itemReader();
778+
}
779+
780+
return array;
781+
}
782+
783+
if (elementType == typeof(Type))
784+
{
785+
var array = new Type?[(int)count];
786+
for (int i = 0; i < array.Length; i++)
787+
{
788+
array[i] = (Type?)itemReader();
789+
}
790+
791+
return array;
792+
}
793+
759794
var list = Array.CreateInstance(elementType, (int)count);
760795
for (int i = 0; i < list.Length; i++)
761796
{

0 commit comments

Comments
 (0)