Skip to content

Commit

Permalink
Remove fasthttp.ByteBuffer
Browse files Browse the repository at this point in the history
As advertised in b5f96d4
  • Loading branch information
erikdubbelboer committed Oct 1, 2018
1 parent 761788a commit d4f0cf5
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 196 deletions.
6 changes: 4 additions & 2 deletions args.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"io"
"sync"

"github.com/valyala/bytebufferpool"
)

// AcquireArgs returns an empty Args object from the pool.
Expand Down Expand Up @@ -243,10 +245,10 @@ func (a *Args) GetUint(key string) (int, error) {

// SetUint sets uint value for the given key.
func (a *Args) SetUint(key string, value int) {
bb := AcquireByteBuffer()
bb := bytebufferpool.Get()
bb.B = AppendUint(bb.B[:0], value)
a.SetBytesV(key, bb.B)
ReleaseByteBuffer(bb)
bytebufferpool.Put(bb)
}

// SetUintBytes sets uint value for the given key.
Expand Down
4 changes: 3 additions & 1 deletion args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"
"testing"
"time"

"github.com/valyala/bytebufferpool"
)

func TestDecodeArgAppend(t *testing.T) {
Expand Down Expand Up @@ -171,7 +173,7 @@ func TestArgsWriteTo(t *testing.T) {
var a Args
a.Parse(s)

var w ByteBuffer
var w bytebufferpool.ByteBuffer
n, err := a.WriteTo(&w)
if err != nil {
t.Fatalf("unexpected error: %s", err)
Expand Down
68 changes: 0 additions & 68 deletions bytebuffer.go

This file was deleted.

29 changes: 0 additions & 29 deletions bytebuffer_example_test.go

This file was deleted.

43 changes: 0 additions & 43 deletions bytebuffer_test.go

This file was deleted.

32 changes: 0 additions & 32 deletions bytebuffer_timing_test.go

This file was deleted.

4 changes: 3 additions & 1 deletion bytesconv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net"
"testing"
"time"

"github.com/valyala/bytebufferpool"
)

func TestAppendHTMLEscape(t *testing.T) {
Expand Down Expand Up @@ -92,7 +94,7 @@ func testAppendUint(t *testing.T, n int) {
}

func testWriteHexInt(t *testing.T, n int, expectedS string) {
var w ByteBuffer
var w bytebufferpool.ByteBuffer
bw := bufio.NewWriter(&w)
if err := writeHexInt(bw, n); err != nil {
t.Fatalf("unexpected error when writing hex %x: %s", n, err)
Expand Down
4 changes: 3 additions & 1 deletion bytesconv_timing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"html"
"net"
"testing"

"github.com/valyala/bytebufferpool"
)

func BenchmarkAppendHTMLEscape(b *testing.B) {
Expand Down Expand Up @@ -77,7 +79,7 @@ func BenchmarkInt2HexByte(b *testing.B) {

func BenchmarkWriteHexInt(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
var w ByteBuffer
var w bytebufferpool.ByteBuffer
bw := bufio.NewWriter(&w)
i := 0
for pb.Next() {
Expand Down
6 changes: 2 additions & 4 deletions compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ func WriteGzipLevel(w io.Writer, p []byte, level int) (int, error) {
switch w.(type) {
case *byteSliceWriter,
*bytes.Buffer,
*ByteBuffer,
*bytebufferpool.ByteBuffer:
// These writers don't block, so we can just use stacklessWriteGzip
ctx := &compressCtx{
Expand Down Expand Up @@ -249,7 +248,6 @@ func WriteDeflateLevel(w io.Writer, p []byte, level int) (int, error) {
switch w.(type) {
case *byteSliceWriter,
*bytes.Buffer,
*ByteBuffer,
*bytebufferpool.ByteBuffer:
// These writers don't block, so we can just use stacklessWriteDeflate
ctx := &compressCtx{
Expand Down Expand Up @@ -409,7 +407,7 @@ func isFileCompressible(f *os.File, minCompressRatio float64) bool {
// Try compressing the first 4kb of of the file
// and see if it can be compressed by more than
// the given minCompressRatio.
b := AcquireByteBuffer()
b := bytebufferpool.Get()
zw := acquireStacklessGzipWriter(b, CompressDefaultCompression)
lr := &io.LimitedReader{
R: f,
Expand All @@ -424,7 +422,7 @@ func isFileCompressible(f *os.File, minCompressRatio float64) bool {

n := 4096 - lr.N
zn := len(b.B)
ReleaseByteBuffer(b)
bytebufferpool.Put(b)
return float64(zn) < float64(n)*minCompressRatio
}

Expand Down
9 changes: 5 additions & 4 deletions fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"time"

"github.com/klauspost/compress/gzip"
"github.com/valyala/bytebufferpool"
)

// ServeFileBytesUncompressed returns HTTP response containing file contents
Expand Down Expand Up @@ -139,12 +140,12 @@ func NewVHostPathRewriter(slashesCount int) PathRewriteFunc {
if len(host) == 0 {
host = strInvalidHost
}
b := AcquireByteBuffer()
b := bytebufferpool.Get()
b.B = append(b.B, '/')
b.B = append(b.B, host...)
b.B = append(b.B, path...)
ctx.URI().SetPathBytes(b.B)
ReleaseByteBuffer(b)
bytebufferpool.Put(b)

return ctx.Path()
}
Expand Down Expand Up @@ -915,7 +916,7 @@ var (
)

func (h *fsHandler) createDirIndex(base *URI, dirPath string, mustCompress bool) (*fsFile, error) {
w := &ByteBuffer{}
w := &bytebufferpool.ByteBuffer{}

basePathEscaped := html.EscapeString(string(base.Path()))
fmt.Fprintf(w, "<html><head><title>%s</title><style>.dir { font-weight: bold }</style></head><body>", basePathEscaped)
Expand Down Expand Up @@ -975,7 +976,7 @@ func (h *fsHandler) createDirIndex(base *URI, dirPath string, mustCompress bool)
fmt.Fprintf(w, "</ul></body></html>")

if mustCompress {
var zbuf ByteBuffer
var zbuf bytebufferpool.ByteBuffer
zbuf.B = AppendGzipBytesLevel(zbuf.B, w.B, CompressDefaultCompression)
w = &zbuf
}
Expand Down
6 changes: 4 additions & 2 deletions header_timing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"bytes"
"io"
"testing"

"github.com/valyala/bytebufferpool"
)

var strFoobar = []byte("foobar.com")
Expand Down Expand Up @@ -65,7 +67,7 @@ func BenchmarkRequestHeaderWrite(b *testing.B) {
h.SetHost("foobar.com")
h.SetUserAgent("aaa.bbb")
h.SetReferer("http://google.com/aaa/bbb")
var w ByteBuffer
var w bytebufferpool.ByteBuffer
for pb.Next() {
if _, err := h.WriteTo(&w); err != nil {
b.Fatalf("unexpected error when writing header: %s", err)
Expand All @@ -83,7 +85,7 @@ func BenchmarkResponseHeaderWrite(b *testing.B) {
h.SetContentLength(1256)
h.SetServer("aaa 1/2.3")
h.Set("Test", "1.2.3")
var w ByteBuffer
var w bytebufferpool.ByteBuffer
for pb.Next() {
if _, err := h.WriteTo(&w); err != nil {
b.Fatalf("unexpected error when writing header: %s", err)
Expand Down
10 changes: 5 additions & 5 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func (resp *Response) BodyGunzip() ([]byte, error) {
}

func gunzipData(p []byte) ([]byte, error) {
var bb ByteBuffer
var bb bytebufferpool.ByteBuffer
_, err := WriteGunzip(&bb, p)
if err != nil {
return nil, err
Expand All @@ -373,7 +373,7 @@ func (resp *Response) BodyInflate() ([]byte, error) {
}

func inflateData(p []byte) ([]byte, error) {
var bb ByteBuffer
var bb bytebufferpool.ByteBuffer
_, err := WriteInflate(&bb, p)
if err != nil {
return nil, err
Expand Down Expand Up @@ -711,7 +711,7 @@ func (req *Request) MultipartForm() (*multipart.Form, error) {
}

func marshalMultipartForm(f *multipart.Form, boundary string) ([]byte, error) {
var buf ByteBuffer
var buf bytebufferpool.ByteBuffer
if err := WriteMultipartForm(&buf, f, boundary); err != nil {
return nil, err
}
Expand Down Expand Up @@ -1457,7 +1457,7 @@ func (resp *Response) String() string {
}

func getHTTPString(hw httpWriter) string {
w := AcquireByteBuffer()
w := bytebufferpool.Get()
bw := bufio.NewWriter(w)
if err := hw.Write(bw); err != nil {
return err.Error()
Expand All @@ -1466,7 +1466,7 @@ func getHTTPString(hw httpWriter) string {
return err.Error()
}
s := string(w.B)
ReleaseByteBuffer(w)
bytebufferpool.Put(w)
return s
}

Expand Down
Loading

0 comments on commit d4f0cf5

Please sign in to comment.