From dbc88cf6b3990a905ced2641d3f7e60fb7ea25d3 Mon Sep 17 00:00:00 2001 From: Steve Yen Date: Tue, 19 Dec 2017 19:15:19 -0800 Subject: [PATCH] scorch docNumberToBytes() checks cap(buf) before allocating With more pprof focusing (zooming in on a particular func), there were still some memory allocations showing up with docNumberToBytes() in micro benchmarks of bleve-query. On a dev macbook, on an index of 50K wikipedia docs, using search of relatively common "text:date"... 400 qps - upsidedown/moss 680 qps - scorch before 775 qps - scorch after --- index/scorch/snapshot_index.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/index/scorch/snapshot_index.go b/index/scorch/snapshot_index.go index 7f5f4103..bb6a8be6 100644 --- a/index/scorch/snapshot_index.go +++ b/index/scorch/snapshot_index.go @@ -367,7 +367,11 @@ func (i *IndexSnapshot) TermFieldReader(term []byte, field string, includeFreq, func docNumberToBytes(buf []byte, in uint64) []byte { if len(buf) != 8 { - buf = make([]byte, 8) + if cap(buf) >= 8 { + buf = buf[0:8] + } else { + buf = make([]byte, 8) + } } binary.BigEndian.PutUint64(buf, in) return buf