Skip to content

shenwei356/bbuffer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bbuffer

GoDoc

bbufer -- an alternative of standard library bytes.Buffer.

bytes.Buffer frequently allots space and copies slice when the buffer size grows. However bbufer just stores the data from method Writes() into slice of byte slice ([][]byte), and only copies once when we need all the data by calling method Bytes(), which could reduce memory allocation.

Shortcoming

If writing a large number of times, it will bring high load to GC. system will cost a lot of time scanning objects.

Install

This package is "go-gettable", just:

go get -u github.com/shenwei356/bbuffer

Usage

Just like bytes.Buffer

import "github.com/shenwei356/bbuffer"
var b bbufer.Buffer // or b := bbufer.NewBuffer()
b.Write(data)       // write []byte to it
b.Bytes()           // return the data
b.Reset()           // reset

Benchmark

Time

$ go test -bench=. -memprofile mem.out -cpuprofile cpu.out
generate test data for test. scale 1/3: 1000
generate test data for test. scale 2/3: 1000000
generate test data for test. scale 3/3: 100000000
PASS
BenchmarkBBufer-4       2000000000               0.08 ns/op
BenchmarkBytesBufer-4   2000000000               0.16 ns/op

CPU profile

$ go tool pprof ./bbuffer.test cpu.out

cpu.png

Allocated memory

$ go tool pprof --alloc_space ./bbuffer.test mem.out

mem.png

License

Copyright (c) 2016, Wei Shen (shenwei356@gmail.com)

MIT License

About

An alternative of standard library bytes.Buffer

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages