bytebufferpool icon indicating copy to clipboard operation
bytebufferpool copied to clipboard

bytebuffer will be gc when reallocating memory

Open reacerland opened this issue 4 years ago • 1 comments

func (b *ByteBuffer) ReadFrom(r io.Reader) (int64, error) {
	p := b.B
	nStart := int64(len(p))
	nMax := int64(cap(p))
	n := nStart
	if nMax == 0 {
		nMax = 64
		p = make([]byte, nMax)
	} else {
		p = p[:nMax]
	}
	for {
		if n == nMax {
			nMax *= 2
			bNew := make([]byte, nMax)
			copy(bNew, p)
			p = bNew
		}
		nn, err := r.Read(p[n:])
		n += int64(nn)
		if err != nil {
			b.B = p[:n]
			n -= nStart
			if err == io.EOF {
				return n, nil
			}
			return n, err
		}
	}
}

if n == nMax, create a slice that is twice as long as cap(p.B), old p.B slice will be gc?

reacerland avatar Jul 27 '21 09:07 reacerland

sure, old p.B would not be used any more.

chaseSpace avatar Aug 08 '24 03:08 chaseSpace