Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CodedOutputStream :GC is generated when encoding #6814

Closed
sunrui19941128 opened this issue Oct 29, 2019 · 3 comments
Closed

CodedOutputStream :GC is generated when encoding #6814

sunrui19941128 opened this issue Oct 29, 2019 · 3 comments
Labels

Comments

@sunrui19941128
Copy link

public byte[] Encode(int id,IMessage msg)
{
using (MemoryStream rawOutput = new MemoryStream())
{
using (CodedOutputStream output = new CodedOutputStream(rawOutput))
{
output.WriteEnum(id);
output.WriteMessage(msg);
output.Flush();
return rawOutput.ToArray();
}
}
}

In Unity, every time this code is encoded, a considerable amount of GC will be generated, which is about 5k per call, or more than 200 k per frame

@ObsidianMinor
Copy link
Contributor

ObsidianMinor commented Oct 29, 2019

You can cut down on most of the GC overhead by calculating the size of the array ahead of time and writing directly to it like so:

public byte[] Encode(int id, IMessage msg)
{
    var len = CodedOutputStream.ComputeEnumSize(id) + CodedOutputStream.ComputeMessageSize(msg);
    var arr = new byte[len];
    using (CodedOutputStream output = new CodedOutputStream(arr))
    {
        output.WriteEnum(id);
        output.WriteMessage(msg);
    }
    return arr;
}

@sunrui19941128
Copy link
Author

Thanks,And that's what I've done since then. I've done dynamic computation of size and then I've optimized a lot of memory by opening up a little bit.

@jtattermusch
Copy link
Contributor

In addition to what @ObsidianMinor has said, we are also planning a new serialization/deserialization API that can avoid allocations completely (at least in some cases).
The WIP is here (but it's probably going to take a bit until it lands as the change is not trivial) #5888

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants