]> Cypherpunks repositories - gostls13.git/commitdiff
net/http: reduce allocations in chunk reading & writing
authorBrad Fitzpatrick <bradfitz@golang.org>
Fri, 16 Nov 2012 21:25:01 +0000 (13:25 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Fri, 16 Nov 2012 21:25:01 +0000 (13:25 -0800)
R=golang-dev, r
CC=golang-dev
https://golang.org/cl/6847063

src/pkg/net/http/chunked.go
src/pkg/net/http/httputil/chunked.go

index 91d7bb6575ae30785ad69003caab8306066d0f0f..7cf39cfa5fcee812534f5ca96c898ee9066e6707 100644 (file)
@@ -13,6 +13,7 @@ import (
        "bufio"
        "bytes"
        "errors"
+       "fmt"
        "io"
        "strconv"
 )
@@ -39,6 +40,7 @@ type chunkedReader struct {
        r   *bufio.Reader
        n   uint64 // unread bytes in chunk
        err error
+       buf [2]byte
 }
 
 func (cr *chunkedReader) beginChunk() {
@@ -74,9 +76,8 @@ func (cr *chunkedReader) Read(b []uint8) (n int, err error) {
        cr.n -= uint64(n)
        if cr.n == 0 && cr.err == nil {
                // end of chunk (CRLF)
-               b := make([]byte, 2)
-               if _, cr.err = io.ReadFull(cr.r, b); cr.err == nil {
-                       if b[0] != '\r' || b[1] != '\n' {
+               if _, cr.err = io.ReadFull(cr.r, cr.buf[:]); cr.err == nil {
+                       if cr.buf[0] != '\r' || cr.buf[1] != '\n' {
                                cr.err = errors.New("malformed chunked encoding")
                        }
                }
@@ -147,9 +148,7 @@ func (cw *chunkedWriter) Write(data []byte) (n int, err error) {
                return 0, nil
        }
 
-       head := strconv.FormatInt(int64(len(data)), 16) + "\r\n"
-
-       if _, err = io.WriteString(cw.Wire, head); err != nil {
+       if _, err = fmt.Fprintf(cw.Wire, "%x\r\n", len(data)); err != nil {
                return 0, err
        }
        if n, err = cw.Wire.Write(data); err != nil {
index 91a7eb6b1adaa327f5b7627a690232bcc41c7eb7..26daee5f2c74f80ebb822c24072fe3917cb24396 100644 (file)
@@ -15,6 +15,7 @@ import (
        "bufio"
        "bytes"
        "errors"
+       "fmt"
        "io"
        "strconv"
 )
@@ -41,6 +42,7 @@ type chunkedReader struct {
        r   *bufio.Reader
        n   uint64 // unread bytes in chunk
        err error
+       buf [2]byte
 }
 
 func (cr *chunkedReader) beginChunk() {
@@ -76,9 +78,8 @@ func (cr *chunkedReader) Read(b []uint8) (n int, err error) {
        cr.n -= uint64(n)
        if cr.n == 0 && cr.err == nil {
                // end of chunk (CRLF)
-               b := make([]byte, 2)
-               if _, cr.err = io.ReadFull(cr.r, b); cr.err == nil {
-                       if b[0] != '\r' || b[1] != '\n' {
+               if _, cr.err = io.ReadFull(cr.r, cr.buf[:]); cr.err == nil {
+                       if cr.buf[0] != '\r' || cr.buf[1] != '\n' {
                                cr.err = errors.New("malformed chunked encoding")
                        }
                }
@@ -149,9 +150,7 @@ func (cw *chunkedWriter) Write(data []byte) (n int, err error) {
                return 0, nil
        }
 
-       head := strconv.FormatInt(int64(len(data)), 16) + "\r\n"
-
-       if _, err = io.WriteString(cw.Wire, head); err != nil {
+       if _, err = fmt.Fprintf(cw.Wire, "%x\r\n", len(data)); err != nil {
                return 0, err
        }
        if n, err = cw.Wire.Write(data); err != nil {