You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is more of a minor feature request - happy to also do a PR for it.
As of now, msgp.Writer assumes that all writes are buffered.
There are many cases where writes need not be buffered, which could save an extra allocation and remove the need for the application to make use of the global writer sync.Pool when calling msgp.Encode.
An example case where buffered writes aren't necessary and aren't worth an extra allocation would be when you would like to compute the checksum of a MessagePack struct by writing its contents into a hash.Hasher.
This could be done without requiring any breaking changes by adding in a condition to all (*msgp.Writer).Write* methods, where if (*msgp.Writer).buf is nil, directly write to (*msgp.Writer).w instead of buffering writes into (*msgp.Writer).buf.
A more general breaking change that I'd also like to propose as an alternative to the above is to refactor the entire API to not assume writes are buffered at all. To do this, msgp.Writer could be removed and replaced in favor of io.Writer, and users may then choose to wrap bufio.Writer over their provided io.Writer should they wish for writes to be buffered.
The text was updated successfully, but these errors were encountered:
lithdew
changed the title
msgp: allow for unbuffered writer to prevent allocations
msgp: allow for unbuffered writes to prevent allocations
Jun 9, 2021
You can use msgp.NewWriterSize, though it we set it to minimum 18 so it has the buffer space it needs.
I don't see any benefit to completely unbuffered writes, and you end up spending an unreasonable amount of time calling the upstream "Write" function for every byte.
If the allocation is an issue just re-use the writer or use msg.Encode(...).
Hello,
This is more of a minor feature request - happy to also do a PR for it.
As of now,
msgp.Writer
assumes that all writes are buffered.There are many cases where writes need not be buffered, which could save an extra allocation and remove the need for the application to make use of the global writer
sync.Pool
when callingmsgp.Encode
.An example case where buffered writes aren't necessary and aren't worth an extra allocation would be when you would like to compute the checksum of a MessagePack struct by writing its contents into a
hash.Hasher
.This could be done without requiring any breaking changes by adding in a condition to all
(*msgp.Writer).Write*
methods, where if(*msgp.Writer).buf
is nil, directly write to(*msgp.Writer).w
instead of buffering writes into(*msgp.Writer).buf
.A more general breaking change that I'd also like to propose as an alternative to the above is to refactor the entire API to not assume writes are buffered at all. To do this,
msgp.Writer
could be removed and replaced in favor ofio.Writer
, and users may then choose to wrapbufio.Writer
over their providedio.Writer
should they wish for writes to be buffered.The text was updated successfully, but these errors were encountered: