write: exact allocation for newLinesIndices
Minor change which makes the function slightly slower, but use much less
memory / allocations. A benchmark on a medium sized repo showed this
function was 4% slower, but 60% less memory allocated. This is a minor
part of indexing so is likely not noticeable. However, was a quick
win.
Can also update the algorithm to use bytes.IndexByte. However, that code
is more tricky and this function doesn't contribute much time indexing.
Change-Id: I2e0f037878db08055c1b7d092667a53a0715c0d1
diff --git a/write.go b/write.go
index 38fbb52..8c5081e 100644
--- a/write.go
+++ b/write.go
@@ -16,6 +16,7 @@
import (
"bufio"
+ "bytes"
"encoding/binary"
"encoding/json"
"io"
@@ -154,7 +155,7 @@
}
func newLinesIndices(in []byte) []uint32 {
- out := make([]uint32, 0, len(in)/30)
+ out := make([]uint32, 0, bytes.Count(in, []byte{'\n'}))
for i, c := range in {
if c == '\n' {
out = append(out, uint32(i))