-
Notifications
You must be signed in to change notification settings - Fork 242
Description
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 What am I talking about... Why not just write to sync.Pool to reduce the allocations from multiple calls?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.