as these are generally easier to work with than Put-like APIs where you need to know the size of the buffer beforehand, prepare a buffer of the right size, and then call the Put-like operation.
Such APIs are missing for the encoding/hex, encoding/base32, and encoding/base64 packages. I propose we add the following:
This would simplify many callers of these packages, where the only reason to call the Len method is to obtain the size of the expected output buffer, do some manual memory management, and then call the Encode or Decode equivalent.
This assumes we had some way to unify bytes.Buffer with append-like APIs; see #53685 (comment).
The resulting code is both simpler and more performant since we can append directly into the underlying bytes.Buffer without going through an intermediate encodeState.scratch buffer. Also, the caller doesn't have to do manual memory management checking whether the encoded output fits within the encodeState.scratch.
The text was updated successfully, but these errors were encountered:
As far as names go, we could also consider EncodeAppend and DecodeAppend, even if they aren't as consistent with the usual "verb then object" method names like MarshalJSON or AppendUint.
There's been a shift in recent years to provide
Append
-like variants of APIs:as these are generally easier to work with than
Put
-like APIs where you need to know the size of the buffer beforehand, prepare a buffer of the right size, and then call thePut
-like operation.Such APIs are missing for the
encoding/hex
,encoding/base32
, andencoding/base64
packages. I propose we add the following:This would simplify many callers of these packages, where the only reason to call the
Len
method is to obtain the size of the expected output buffer, do some manual memory management, and then call theEncode
orDecode
equivalent.For example, this logic in the
json
package:go/src/encoding/json/encode.go
Lines 839 to 860 in c111091
could be simplified as:
This assumes we had some way to unify
bytes.Buffer
withappend
-like APIs; see #53685 (comment).The resulting code is both simpler and more performant since we can append directly into the underlying
bytes.Buffer
without going through an intermediateencodeState.scratch
buffer. Also, the caller doesn't have to do manual memory management checking whether the encoded output fits within theencodeState.scratch
.The text was updated successfully, but these errors were encountered: