golang bytes buffer代码剖析

来源:转载


//上数据结构,bytes Buffertype Buffer struct {buf []byte // byte切片off int // 从&buf[off]地址读数据, 从&buf[len(buf)]地址写数据runeBytes [utf8.UTFMax]byte // avoid allocation of slice on each WriteByte or Runebootstrap [64]byte // memory to hold first slice; helps small buffers (Printf) avoid allocation.lastRead readOp // last read operation, so that Unread* can work correctly.}

再来看看我们bytes Buffer里面write是怎么实现的

func (b *Buffer) WriteString(s string) (n int, err error) {b.lastRead = opInvalid /// Non-read operation 不需要读的标志 等于0m := b.grow(len(s)) // 增长大小(准确来说是调整数据在buf中位置,也不一定增长),m当然是老数据末尾return copy(b.buf[m:], s), nil //copy一下数据,从m开始}

最最重要的看过来

func (b *Buffer) grow(n int) int {m := b.Len() //func (b *Buffer) Len() int { return len(b.buf) - b.off }// m就是buf的len 减去(-) b.off(读开始位置)if m == 0 && b.off != 0 {b.Truncate(0) //下面给予显示} //调整大小和位置if len(b.buf)+n > cap(b.buf) {var buf []byteif b.buf == nil && n <= len(b.bootstrap) {buf = b.bootstrap[0:] //这个bootstrap缓存了buf的切片,说是防止重allocation} else if m+n <= cap(b.buf)/2 { // 二倍申请新的slice的原则copy(b.buf[:], b.buf[b.off:])buf = b.buf[:m]} else {// not enough space anywherebuf = makeSlice(2*cap(b.buf) + n)copy(buf, b.buf[b.off:])}b.buf = bufb.off = 0}b.buf = b.buf[0 : b.off+m+n] //赋值咯return b.off + m}

再看看它用到过的函数 Truncate函数,缩减切片

func (b *Buffer) Truncate(n int) {b.lastRead = opInvalidswitch {case n < 0 || n > b.Len():panic("bytes.Buffer: truncation out of range")case n == 0:// Reuse buffer space.b.off = 0}b.buf = b.buf[0 : b.off+n]}

makeSlice函数

func makeSlice(n int) []byte {// If the make fails, give a known error.defer func() {if recover() != nil {panic(ErrTooLarge)}}()return make([]byte, n) //其实还是调用这个make}

最后看看一个使用示例咯

package mainimport ("bytes""fmt""strconv""time")func main() {var buffer bytes.Bufferttime := time.Now().UnixNano()for i := 0; i < 10000000; i++ {buffer.WriteString(strconv.Itoa(i))}ttime1 := time.Now().UnixNano()//取内容buffer.Bytes() 或者 buffer.String()fmt.Printf("time cal %f %d/n", float64(ttime1-ttime)/float64(1*time.Second), len(buffer.String()))}

总结:bytes buffer:就是写byte或者字符串string的一个容器



分享给朋友:
您可能感兴趣的文章:
随机阅读: