Commit f3e6b206 authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

net/http: reduce allocations in chunk reading & writing

R=golang-dev, r
CC=golang-dev
https://golang.org/cl/6847063
parent de58eb90
...@@ -13,6 +13,7 @@ import ( ...@@ -13,6 +13,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"errors" "errors"
"fmt"
"io" "io"
"strconv" "strconv"
) )
...@@ -39,6 +40,7 @@ type chunkedReader struct { ...@@ -39,6 +40,7 @@ type chunkedReader struct {
r *bufio.Reader r *bufio.Reader
n uint64 // unread bytes in chunk n uint64 // unread bytes in chunk
err error err error
buf [2]byte
} }
func (cr *chunkedReader) beginChunk() { func (cr *chunkedReader) beginChunk() {
...@@ -74,9 +76,8 @@ func (cr *chunkedReader) Read(b []uint8) (n int, err error) { ...@@ -74,9 +76,8 @@ func (cr *chunkedReader) Read(b []uint8) (n int, err error) {
cr.n -= uint64(n) cr.n -= uint64(n)
if cr.n == 0 && cr.err == nil { if cr.n == 0 && cr.err == nil {
// end of chunk (CRLF) // end of chunk (CRLF)
b := make([]byte, 2) if _, cr.err = io.ReadFull(cr.r, cr.buf[:]); cr.err == nil {
if _, cr.err = io.ReadFull(cr.r, b); cr.err == nil { if cr.buf[0] != '\r' || cr.buf[1] != '\n' {
if b[0] != '\r' || b[1] != '\n' {
cr.err = errors.New("malformed chunked encoding") cr.err = errors.New("malformed chunked encoding")
} }
} }
...@@ -147,9 +148,7 @@ func (cw *chunkedWriter) Write(data []byte) (n int, err error) { ...@@ -147,9 +148,7 @@ func (cw *chunkedWriter) Write(data []byte) (n int, err error) {
return 0, nil return 0, nil
} }
head := strconv.FormatInt(int64(len(data)), 16) + "\r\n" if _, err = fmt.Fprintf(cw.Wire, "%x\r\n", len(data)); err != nil {
if _, err = io.WriteString(cw.Wire, head); err != nil {
return 0, err return 0, err
} }
if n, err = cw.Wire.Write(data); err != nil { if n, err = cw.Wire.Write(data); err != nil {
......
...@@ -15,6 +15,7 @@ import ( ...@@ -15,6 +15,7 @@ import (
"bufio" "bufio"
"bytes" "bytes"
"errors" "errors"
"fmt"
"io" "io"
"strconv" "strconv"
) )
...@@ -41,6 +42,7 @@ type chunkedReader struct { ...@@ -41,6 +42,7 @@ type chunkedReader struct {
r *bufio.Reader r *bufio.Reader
n uint64 // unread bytes in chunk n uint64 // unread bytes in chunk
err error err error
buf [2]byte
} }
func (cr *chunkedReader) beginChunk() { func (cr *chunkedReader) beginChunk() {
...@@ -76,9 +78,8 @@ func (cr *chunkedReader) Read(b []uint8) (n int, err error) { ...@@ -76,9 +78,8 @@ func (cr *chunkedReader) Read(b []uint8) (n int, err error) {
cr.n -= uint64(n) cr.n -= uint64(n)
if cr.n == 0 && cr.err == nil { if cr.n == 0 && cr.err == nil {
// end of chunk (CRLF) // end of chunk (CRLF)
b := make([]byte, 2) if _, cr.err = io.ReadFull(cr.r, cr.buf[:]); cr.err == nil {
if _, cr.err = io.ReadFull(cr.r, b); cr.err == nil { if cr.buf[0] != '\r' || cr.buf[1] != '\n' {
if b[0] != '\r' || b[1] != '\n' {
cr.err = errors.New("malformed chunked encoding") cr.err = errors.New("malformed chunked encoding")
} }
} }
...@@ -149,9 +150,7 @@ func (cw *chunkedWriter) Write(data []byte) (n int, err error) { ...@@ -149,9 +150,7 @@ func (cw *chunkedWriter) Write(data []byte) (n int, err error) {
return 0, nil return 0, nil
} }
head := strconv.FormatInt(int64(len(data)), 16) + "\r\n" if _, err = fmt.Fprintf(cw.Wire, "%x\r\n", len(data)); err != nil {
if _, err = io.WriteString(cw.Wire, head); err != nil {
return 0, err return 0, err
} }
if n, err = cw.Wire.Write(data); err != nil { if n, err = cw.Wire.Write(data); err != nil {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment