I was exploring both the LLAPI and the HLAPI, and I noticed something pretty unfortunate: there’s no way of using the built-in NetworkReader without generating constant garbage. The only way to give a NetworkReader its data buffer is through its constructor.
It seems to me like it should be an obvious company-wide policy at Unity that absolutely nothing should generate constant runtime garbage if used the way it’s meant to be used.
To the best of my knowledge, this isn’t a problem with NetworkWriter. You don’t have to create a ‘new’ one every time, because you can simply SeekZero on the same old NetworkWriter again and again and start writing from there. Couldn’t we just do the same with NetworkReader if we had a way to set its bytes array?
My original goal was to build my own HLAPI on top of the LLAPI, and I figured I could’ve easily salvaged NetworkReader/Writer if it wasn’t for that little flaw.
Yeah, that’s a filthy habit they need to do something about. I’d be worried about leaving a server running with problems like that all over the place. You’ve got leaks all over the place or GC stuttering. Either way it’s not going to be a smooth experience.
yeah, there’ are some weird bits in there for sure. i think to make this work you just need to make m_buf public and you’re basically done though. and possibly the m_Buffer property in NetworkBuffer
I originally also planned to just use LLAPI stuff and maybe use NetworkWriter and NetworkReader. I even had a system in place to use pooling for NetworkReaders/Writers, but it turned out a bit messy and just felt wrong …
I eventually abandoned NetworkReader/Writer (and at some point even the entire LLAPI due to several issues), and just wrote all this stuff myself.
I now have one nice and clean class which can be used for both reading and writing, completely free of garbage. I also have another class which does the same thing, but at the bit-level instead of byte-level.
I was planning to eventually release much of my code for free at some point, but haven’t set up a github account or anything yet. If anyone is interested, just let me know!
I wouldn’t have a clue about networking. But chiming in to support the fact that Unity needs to be told off whenever they reach for a the GC cookie jar.