@@ -30,6 +30,8 @@ type chunkedIntCoder struct {
3030 encoder * govarint.Base128Encoder
3131 chunkLens []uint64
3232 currChunk uint64
33+
34+ buf []byte
3335}
3436
3537// newChunkedIntCoder returns a new chunk int coder which packs data into
@@ -67,12 +69,8 @@ func (c *chunkedIntCoder) Add(docNum uint64, vals ...uint64) error {
6769 // starting a new chunk
6870 if c .encoder != nil {
6971 // close out last
70- c .encoder .Close ()
71- encodingBytes := c .chunkBuf .Bytes ()
72- c .chunkLens [c .currChunk ] = uint64 (len (encodingBytes ))
73- c .final = append (c .final , encodingBytes ... )
72+ c .Close ()
7473 c .chunkBuf .Reset ()
75- c .encoder = govarint .NewU64Base128Encoder (& c .chunkBuf )
7674 }
7775 c .currChunk = chunk
7876 }
@@ -98,26 +96,25 @@ func (c *chunkedIntCoder) Close() {
9896
9997// Write commits all the encoded chunked integers to the provided writer.
10098func (c * chunkedIntCoder ) Write (w io.Writer ) (int , error ) {
101- var tw int
102- buf := make ([]byte , binary .MaxVarintLen64 )
103- // write out the number of chunks
99+ bufNeeded := binary .MaxVarintLen64 * (1 + len (c .chunkLens ))
100+ if len (c .buf ) < bufNeeded {
101+ c .buf = make ([]byte , bufNeeded )
102+ }
103+ buf := c .buf
104+
105+ // write out the number of chunks & each chunkLen
104106 n := binary .PutUvarint (buf , uint64 (len (c .chunkLens )))
105- nw , err := w .Write (buf [:n ])
106- tw += nw
107+ for _ , chunkLen := range c .chunkLens {
108+ n += binary .PutUvarint (buf [n :], uint64 (chunkLen ))
109+ }
110+
111+ tw , err := w .Write (buf [:n ])
107112 if err != nil {
108113 return tw , err
109114 }
110- // write out the chunk lens
111- for _ , chunkLen := range c .chunkLens {
112- n := binary .PutUvarint (buf , uint64 (chunkLen ))
113- nw , err = w .Write (buf [:n ])
114- tw += nw
115- if err != nil {
116- return tw , err
117- }
118- }
115+
119116 // write out the data
120- nw , err = w .Write (c .final )
117+ nw , err : = w .Write (c .final )
121118 tw += nw
122119 if err != nil {
123120 return tw , err
0 commit comments