Browse Source

Increase performance by reusing []byte, reducing GC.

pull/28/head
Chris Lu 10 years ago
parent
commit
6c5a3d3dbf
  1. 23
      go/weed/benchmark.go

23
go/weed/benchmark.go

@ -33,10 +33,12 @@ type BenchmarkOptions struct {
cpuprofile *string cpuprofile *string
maxCpu *int maxCpu *int
vid2server map[string]string //cache for vid locations vid2server map[string]string //cache for vid locations
} }
var ( var (
b BenchmarkOptions
b BenchmarkOptions
sharedBytes []byte
) )
func init() { func init() {
@ -55,6 +57,7 @@ func init() {
b.cpuprofile = cmdBenchmark.Flag.String("cpuprofile", "", "cpu profile output file") b.cpuprofile = cmdBenchmark.Flag.String("cpuprofile", "", "cpu profile output file")
b.maxCpu = cmdBenchmark.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs") b.maxCpu = cmdBenchmark.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
b.vid2server = make(map[string]string) b.vid2server = make(map[string]string)
sharedBytes = make([]byte, 1024)
} }
var cmdBenchmark = &Command{ var cmdBenchmark = &Command{
@ -512,6 +515,23 @@ func (l *FakeReader) Read(p []byte) (n int, err error) {
return return
} }
func (l *FakeReader) WriteTo(w io.Writer) (n int64, err error) {
size := int(l.size)
bufferSize := len(sharedBytes)
for size > 0 {
tempBuffer := sharedBytes
if size < bufferSize {
tempBuffer = sharedBytes[0:size]
}
count, e := w.Write(tempBuffer)
if e != nil {
return int64(size), e
}
size -= count
}
return l.size, nil
}
func Readln(r *bufio.Reader) ([]byte, error) { func Readln(r *bufio.Reader) ([]byte, error) {
var ( var (
isPrefix bool = true isPrefix bool = true
@ -524,3 +544,4 @@ func Readln(r *bufio.Reader) ([]byte, error) {
} }
return ln, err return ln, err
} }
Loading…
Cancel
Save