Kaynağa Gözat

remove grow case differentiation

benchmarks:
http://play.golang.org/p/xMWTHiu9JY

https://groups.google.com/forum/?fromgroups=#!topic/golang-nuts/ETbw1ECDgRs
Julien Schmidt 12 yıl önce
ebeveyn
işleme
2aeea124cc
1 değiştirilmiş dosya ile 9 ekleme ve 24 silme
  1. 9 24
      buffer.go

+ 9 - 24
buffer.go

@@ -38,7 +38,15 @@ func (b *buffer) fill(need int) (err error) {
 
 	// grow buffer if necessary
 	if need > len(b.buf) {
-		b.grow(need)
+		for {
+			b.buf = append(b.buf, 0)
+			b.buf = b.buf[:cap(b.buf)]
+
+			if cap(b.buf) < size {
+				continue
+			}
+			return
+		}
 	}
 
 	b.idx = 0
@@ -55,29 +63,6 @@ func (b *buffer) fill(need int) (err error) {
 	}
 }
 
-// grow the buffer to at least the given size
-// credit for this code snippet goes to Maxim Khitrov
-// https://groups.google.com/forum/#!topic/golang-nuts/ETbw1ECDgRs
-func (b *buffer) grow(size int) {
-	// If append would be too expensive, alloc a new slice
-	if size > cap(b.buf)*2 {
-		newBuf := make([]byte, size)
-		copy(newBuf, b.buf)
-		b.buf = newBuf
-		return
-	}
-
-	for {
-		b.buf = append(b.buf, 0)
-		b.buf = b.buf[:cap(b.buf)]
-
-		if cap(b.buf) < size {
-			continue
-		}
-		return
-	}
-}
-
 // returns next N bytes from buffer.
 // The returned slice is only guaranteed to be valid until the next read
 func (b *buffer) readNext(need int) (p []byte, err error) {