Skip to content

(*Message).IsValid() performs lots of allocations #358

@DeedleFake

Description

@DeedleFake

I was tracking down a memory leak in an app of mine and, thanks to pprof, I found that calls to (*Message).IsValid() are both expensive and very allocation heavy. It's not documented, but the method performs an entire encoding of the message to a throwaway buffer and immediately just discards the result, only returning any errors that it encounters.

Could this be feasibly changed? Is the full encode to find errors really necessary? Couldn't the same code that finds those errors be refactored out so that only it can be called without needed to actually perform an encoding? Or, if that's not possible, could the buffer size be estimated and pre-allocated, and maybe even use a global sync.Pool to reduce the allocations from multiple calls? What am I talking about... Why not just write to io.Discard and avoid the problem completely? The data's being thrown away anyways.

As it stands, the path of the code is such that IsValid() is called followed immediately by a call to (*Conn).SendMessage(), meaning that every message is being encoded twice, with one of those being into a buffer that's just immediately thrown away.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions