fork of https://github.com/sourcegraph/zoekt
1package gitindex
2
3// contentSlab reduces per-file heap allocations by sub-slicing from a
4// shared buffer. Each returned slice has its capacity capped (3-index
5// slice) so appending to one file's content cannot overwrite adjacent
6// data. Files larger than the slab get their own allocation.
7type contentSlab struct {
8 buf []byte
9 cap int
10}
11
12func newContentSlab(slabCap int) contentSlab {
13 return contentSlab{
14 buf: make([]byte, 0, slabCap),
15 cap: slabCap,
16 }
17}
18
19// alloc returns a byte slice of length n. The caller must write into it
20// immediately (the bytes are uninitialized when sourced from the slab).
21func (s *contentSlab) alloc(n int) []byte {
22 if n > s.cap {
23 return make([]byte, n)
24 }
25 if len(s.buf)+n > cap(s.buf) {
26 s.buf = make([]byte, n, s.cap)
27 return s.buf[:n:n]
28 }
29 off := len(s.buf)
30 s.buf = s.buf[:off+n]
31 return s.buf[off : off+n : off+n]
32}