This code is built on Json.NET and gets called hundreds of times per second. My data starts in an ArraySegment<byte>. I wrap that in a MemoryStream and pass it in to the code. It's generally small data: hundreds of bytes, not thousands. It just doesn't seem right that I have to construct multiple streams and readers for every single deserialization call. How do I optimize this? How do I reuse those two readers?
public object Deserialize(Stream stream, Type type)
{
using (var utfReader = new StreamReader(stream, Encoding.UTF8, true, 2048, true))
using (var reader = new JsonTextReader(utfReader) { CloseInput = false })
return _serializer.Deserialize(reader, type);
}
public T Deserialize<T>(Stream stream)
{
return (T)Deserialize(stream, typeof(T));
}