详解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(); 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()];这个也省掉
这俩问题就留给读者思考.
github:
到此这篇关于详解c# protobuf如何做到0分配内存的序列化的文章就介绍到这了,更多相关c# protobuf 序列化内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!