lvzixun / sproto-Csharp

A pure C# implementation of sproto.
140 stars 53 forks source link

gc 比较高。很多byte[]一直在复制,流也是 #16

Open yingnierxiao opened 5 years ago

lvzixun commented 5 years ago

能否具体指出来哪里有重复的copy,以及优化的方法?

yingnierxiao commented 5 years ago

https://github.com/lvzixun/sproto-Csharp/blob/master/src/SprotoPack.cs#L133 https://github.com/lvzixun/sproto-Csharp/blob/master/src/SprotoPack.cs#L181 其他多处也有,不过这个2个地方比较大点,如果包频率,包比较大,包中还有byte类型,希望可以有个缓存迟来保存废弃的byte[] 数组。重复利用byte,需要改动到byte的实际长度。

lvzixun commented 5 years ago

this.buffer这个本身就是作为pack 和unpack 时可以复用的流来设计的。对应云风的sproto中 https://github.com/cloudwu/sproto/blob/master/lsproto.c#L507 之所以最后返回出来的 pack_bufferunpack_data 要从新copy出来是因为,如果返回this.buffer会导致的结果就是packunpack的实现是有副作用的, 你不知道别人拿到this.buffer还会做什么。同时我又不想传入一个out buffer来作为写入参数。 对应到云风sproto 中 https://github.com/cloudwu/sproto/blob/master/lsproto.c#L513 返回给lua使用的是lua_pushlstring(L, output, bytes); 此处也会重新copy出来一份string.