C# Protobuf如何做到0分配内存的序列化
题目很简单, 就是IMessage对象怎么变成Byte[]
答案1:
msg.ToByteArray()
这肯定不符合我们的要求
答案2:
using var memoryStream = new MemoryStream(); using var codedOutputStream = new CodedOutputStream(memoryStream); msg.WriteTo(codedOutputStream); codedOutputStream.Flush(); memoryStream.ToArray();
这里面memoryStream, codedOutputStream, 还有ToArray都产生了一个对象, MemoryStream内部还会多产生一个byte[]对象
不符合要求
答案3:
有人说你可以给MemoryStream传递一个byte[] slice, 让MemoryStream直接用byte[]
var bytes = new byte[msg.CalculateSize()]; using var memoryStream = new MemoryStream(bytes); using var codedOutputStream = new CodedOutputStream(memoryStream); msg.WriteTo(codedOutputStream); codedOutputStream.Flush();
这次消息直接被序列化到bytes里面去了, 但是memoryStream对象, codecOutputStream还有memoryStream内部的byte[]都还在, 我就序列化了一个对象, 却产生了3个垃圾对象
所以, 来仔细看看CodedOutputStream类:
/// <summary> /// Creates a new CodedOutputStream that writes directly to the given /// byte array. If more bytes are written than fit in the array, /// OutOfSpaceException will be thrown. /// </summary> public CodedOutputStream(byte[] flatArray) : this(flatArray, 0, flatArray.Length) { } /// <summary> /// Creates a new CodedOutputStream that writes directly to the given /// byte array slice. If more bytes are written than fit in the array, /// OutOfSpaceException will be thrown. /// </summary> private CodedOutputStream(byte[] buffer, int offset, int length) { this.output = null; this.buffer = buffer; this.position = offset; this.limit = offset + length; leaveOpen = true; // Simple way of avoiding trying to dispose of a null reference }
提供了一个byte[]的构造函数, 但是没提供slice的构造函数, 好在有一个私有的构造函数
答案4:
这边就不写代码了, 大概意思就是通过反射私有构造函数来构造一个CodedOutputStream对象, 来省掉MemoryStream和他内部的byte[]
现在离答案已经比较接近了
那我们的问题是, 能不能连CodedOutputStream也省掉呢?
答案5来了:
经过仔细观察, 发现这个类没有使用Stream的情况下, 就只需要修改buffer, limit, 和position几个成员就行了, 虽然是private成员, 但是C#还是能修改
下来立马实践
delegate void ClearCodedOutputStream(CodedOutputStream stream, byte[] buffer, int offset, int count); static ClearCodedOutputStream ResetCodedOutputStream; static CodedOutputStream codedOutputStream = new CodedOutputStream(new byte[10]); static unsafe void Encode(IMessage msg, byte[] buffer) { ResetCodedOutputStream(codedOutputStream, buffer, 0, buffer.Length); msg.WriteTo(codedOutputStream); codedOutputStream.Flush(); } static Action<T, TValue> MakeSetter<T, TValue>(FieldInfo field) { DynamicMethod m = new DynamicMethod( "setter", typeof(void), new Type[] { typeof(T), typeof(TValue) }, typeof(Program)); ILGenerator cg = m.GetILGenerator(); cg.Emit(OpCodes.Ldarg_0); cg.Emit(OpCodes.Ldarg_1); cg.Emit(OpCodes.Stfld, field); cg.Emit(OpCodes.Ret); return (Action<T, TValue>)m.CreateDelegate(typeof(Action<T, TValue>)); } static void Main(string[] args) { var bufferField = typeof(CodedOutputStream).GetField("buffer", BindingFlags.NonPublic | BindingFlags.Instance); var limitField = typeof(CodedOutputStream).GetField("limit", BindingFlags.NonPublic | BindingFlags.Instance); var positionField = typeof(CodedOutputStream).GetField("position", BindingFlags.NonPublic | BindingFlags.Instance); var setLimit = MakeSetter<CodedOutputStream, int>(limitField); var setPosition = MakeSetter<CodedOutputStream, int>(positionField); var setBuffer = MakeSetter<CodedOutputStream, byte[]>(bufferField); ResetCodedOutputStream = (stream, buffer, offset, length) => { //this.buffer = buffer; //this.position = offset; //this.limit = offset + length; setBuffer(stream, buffer); setPosition(stream, offset); setLimit(stream, offset + length); };
var buffer = new byte[msg.CalculateSize()]; Encode(msg, buffer); }
这个实例代码里面, 用了一个static的全局CodedOutputStream, 真正用的时候, 肯定要保证线程安全.
所以接下来的问题是:
1. 如何保证CodedOutputStream对象线程安全
2. 如何把var buffer = new byte[msg.CalculateSize()];这个也省掉
这俩问题就留给读者思考.