@@ -86,6 +86,9 @@ func (mc *CCacheMetric) AddRange(prev uint32, itergens []chunk.IterGen) {
86
86
mc .Lock ()
87
87
defer mc .Unlock ()
88
88
89
+ // pre-allocate 1 slice, cheaper than allocating one by one
90
+ chunks := make ([]CCacheChunk , 0 , len (itergens ))
91
+
89
92
// handle the first one
90
93
itergen := itergens [0 ]
91
94
ts := itergen .Ts
@@ -111,12 +114,13 @@ func (mc *CCacheMetric) AddRange(prev uint32, itergens []chunk.IterGen) {
111
114
112
115
// add chunk if we don't have it yet (most likely)
113
116
if _ , ok := mc .chunks [ts ]; ! ok {
114
- mc . chunks [ ts ] = & CCacheChunk {
117
+ chunks = append ( chunks , CCacheChunk {
115
118
Ts : ts ,
116
119
Prev : prev ,
117
120
Next : itergens [1 ].Ts ,
118
121
Itgen : itergen ,
119
- }
122
+ })
123
+ mc .chunks [ts ] = & chunks [len (chunks )- 1 ]
120
124
if addKeysDirect {
121
125
mc .keys = append (mc .keys , ts )
122
126
}
@@ -130,12 +134,13 @@ func (mc *CCacheMetric) AddRange(prev uint32, itergens []chunk.IterGen) {
130
134
ts := itergen .Ts
131
135
// add chunk if we don't have it yet (most likely)
132
136
if _ , ok := mc .chunks [ts ]; ! ok {
133
- mc . chunks [ ts ] = & CCacheChunk {
137
+ chunks = append ( chunks , CCacheChunk {
134
138
Ts : ts ,
135
139
Prev : prev ,
136
140
Next : itergens [i + 1 ].Ts ,
137
141
Itgen : itergen ,
138
- }
142
+ })
143
+ mc .chunks [ts ] = & chunks [len (chunks )- 1 ]
139
144
if addKeysDirect {
140
145
mc .keys = append (mc .keys , ts )
141
146
}
@@ -162,12 +167,13 @@ func (mc *CCacheMetric) AddRange(prev uint32, itergens []chunk.IterGen) {
162
167
163
168
// add chunk if we don't have it yet (most likely)
164
169
if _ , ok := mc .chunks [ts ]; ! ok {
165
- mc . chunks [ ts ] = & CCacheChunk {
170
+ chunks = append ( chunks , CCacheChunk {
166
171
Ts : ts ,
167
172
Prev : prev ,
168
173
Next : next ,
169
174
Itgen : itergen ,
170
- }
175
+ })
176
+ mc .chunks [ts ] = & chunks [len (chunks )- 1 ]
171
177
if addKeysDirect {
172
178
mc .keys = append (mc .keys , ts )
173
179
}
0 commit comments