ax(node): replace prose comments with usage examples in bufpool.go
AX Principle 2 — comments must show HOW with real values, not describe WHAT the signature already says. Replaced three prose descriptions on getBuffer, putBuffer, and MarshalJSON with concrete call-site examples. Co-Authored-By: Charon <charon@lethean.io>
This commit is contained in:
parent
edcee49435
commit
adbf31e987
1 changed files with 5 additions and 4 deletions
|
|
@ -14,14 +14,15 @@ var bufferPool = sync.Pool{
|
|||
},
|
||||
}
|
||||
|
||||
// getBuffer retrieves a buffer from the pool.
|
||||
// buf := getBuffer()
|
||||
// defer putBuffer(buf)
|
||||
func getBuffer() *bytes.Buffer {
|
||||
buf := bufferPool.Get().(*bytes.Buffer)
|
||||
buf.Reset()
|
||||
return buf
|
||||
}
|
||||
|
||||
// putBuffer returns a buffer to the pool.
|
||||
// putBuffer(buf) // always called via defer after getBuffer()
|
||||
func putBuffer(buf *bytes.Buffer) {
|
||||
// Don't pool buffers that grew too large (>64KB)
|
||||
if buf.Cap() <= 65536 {
|
||||
|
|
@ -29,8 +30,8 @@ func putBuffer(buf *bytes.Buffer) {
|
|||
}
|
||||
}
|
||||
|
||||
// MarshalJSON encodes a value to JSON using a pooled buffer.
|
||||
// Returns a copy of the encoded bytes (safe to use after the function returns).
|
||||
// data, err := MarshalJSON(msg)
|
||||
// if err != nil { return nil, err }
|
||||
func MarshalJSON(v interface{}) ([]byte, error) {
|
||||
buf := getBuffer()
|
||||
defer putBuffer(buf)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue