Commit 018da15
authored
core: avoid per block allocation in aggregation (#1973)
The inner loop of MemoryDatabase.executeImpl runs once per block per
matched series, so for a broad query it can execute billions of times.
Three objects were being allocated on every iteration:
* The `newBuffer` argument was a local def. Passing a def where a
function is expected eta-expands at the call site, which is inside the
loop, so the captured cfStep/bufStart/bufEnd produced a new function
object per block. Bind it to a val once per query instead.
* The inner foreach body ended with the Int returned by add(), so the
loop body was typed as AnyVal and boxed a new Integer per block. The
value is buffer.values.length, well above the Integer cache limit, so
none of these were shared. Keep the body Unit typed.
* AggregateCollector.add iterated its blocks with foreach, allocating a
closure that captured the buffer, op, and the aggr/cf/multiple params.
Use an explicit loop over the list in both implementations.
After this the only remaining allocation in the per block frame is the
`List(b)` wrapper. Removing that means adding a single block overload to
the public AggregateCollector trait, left for a separate change.
Note that the four `Math.addNaN _` style eta-expansions in add() are NOT
a source of allocation, despite looking like one in a profile: Math is a
static module, so they compile to non-capturing invokedynamic call sites
that are bound to a singleton and are already specialized.1 parent 25c9e64 commit 018da15
2 files changed
Lines changed: 20 additions & 5 deletions
File tree
- atlas-core/src/main/scala/com/netflix/atlas/core/db
Lines changed: 13 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
121 | 121 | | |
122 | 122 | | |
123 | 123 | | |
124 | | - | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
125 | 130 | | |
126 | 131 | | |
127 | 132 | | |
| |||
130 | 135 | | |
131 | 136 | | |
132 | 137 | | |
| 138 | + | |
133 | 139 | | |
134 | 140 | | |
135 | 141 | | |
| |||
290 | 296 | | |
291 | 297 | | |
292 | 298 | | |
293 | | - | |
294 | | - | |
295 | | - | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
296 | 305 | | |
297 | 306 | | |
298 | 307 | | |
| |||
Lines changed: 7 additions & 1 deletion
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
229 | 229 | | |
230 | 230 | | |
231 | 231 | | |
232 | | - | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
233 | 236 | | |
234 | 237 | | |
235 | 238 | | |
| |||
249 | 252 | | |
250 | 253 | | |
251 | 254 | | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
252 | 258 | | |
253 | 259 | | |
254 | 260 | | |
| |||
0 commit comments