Split the slow path into a separate function, so that the fast path in EnsureSpace becomes inlineable.
This allows code in jwriter to inline the fast path.
See: https://github.com/golang/go/issues/19186
When the body isn't fully read and Close is called multiple times we put
the same buffer in the pool multiple times causing it to be reused by
multiple goroutines at the same time.
When marshalling something as body for a http.Request we can only use
BuildBytes() which always allocates a buffer for our whole body. This
pull request adds a ReadCloser() method which returns an io.ReadCloser
that can be passed as body into http.NewRequest. This ReadCloser will
read from the existing buffs and putBuf them when they are not needed
anymore.
This adds an optional parameter to BuildBytes() which allows for reusing
of a byte buffer. Because it's an optional parameter it will not break
backwards compatibility.
This allows people to use a sync.Pool for the buffers used by
BuildBytes() to reduce garbage generation.