码迷,mamicode.com
首页 > 其他好文 > 详细

golang bytes buffer代码剖析

时间:2015-10-25 16:31:39      阅读:365      评论:0      收藏:0      [点我收藏+]

标签:

//上数据结构,bytes Buffer
type Buffer struct {
	buf       []byte            // byte切片
	off       int               // 从&buf[off]地址读数据, 从&buf[len(buf)]地址写数据
	runeBytes [utf8.UTFMax]byte // avoid allocation of slice on each WriteByte or Rune
	bootstrap [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 不需要读的标志 等于0
	m := 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 []byte
		if 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 anywhere
			buf = makeSlice(2*cap(b.buf) + n)
			copy(buf, b.buf[b.off:])
		}
		b.buf = buf
		b.off = 0
	}
	b.buf = b.buf[0 : b.off+m+n] //赋值咯
	return b.off + m
}

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

func (b *Buffer) Truncate(n int) {
	b.lastRead = opInvalid
	switch {
	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 main

import (
	"bytes"
	"fmt"
	"strconv"
	"time"
)

func main() {

	var buffer bytes.Buffer

	ttime := 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的一个容器

golang bytes buffer代码剖析

标签:

原文地址:http://my.oschina.net/yang1992/blog/521814

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!