Commit 1bb1f2b
runtime: put AddCleanup cleanup arguments in their own allocation
Currently, AddCleanup just creates a simple closure that calls
`cleanup(arg)` as the actual cleanup function tracked internally.
However, the argument ends up getting its own allocation. If it's tiny,
then it can also end up sharing a tiny allocation slot with the object
we're adding the cleanup to. Since the closure is a GC root, we can end
up with cleanups that never fire.
This change refactors the AddCleanup machinery to make the storage for
the argument separate and explicit. With that in place, it explicitly
allocates 16 bytes of storage for tiny arguments to side-step the tiny
allocator.
One would think this would cause an increase in memory use and more
bytes allocated, but that's actually wrong! It turns out that the
current "simple closure" actually creates _two_ closures. By making the
argument passing explicit, we eliminate one layer of closures, so this
actually results in a slightly faster AddCleanup overall and 16 bytes
less memory allocated.
goos: linux
goarch: amd64
pkg: runtime
cpu: AMD EPYC 7B13
│ before.bench │ after.bench │
│ sec/op │ sec/op vs base │
AddCleanupAndStop-64 124.5n ± 2% 103.7n ± 2% -16.71% (p=0.002 n=6)
│ before.bench │ after.bench │
│ B/op │ B/op vs base │
AddCleanupAndStop-64 48.00 ± 0% 32.00 ± 0% -33.33% (p=0.002 n=6)
│ before.bench │ after.bench │
│ allocs/op │ allocs/op vs base │
AddCleanupAndStop-64 3.000 ± 0% 2.000 ± 0% -33.33% (p=0.002 n=6)
This change does, however, does add 16 bytes of overhead to the cleanup
special itself, and makes each cleanup block entry 24 bytes instead of 8
bytes. This means the overall memory overhead delta with this change is
neutral, and we just have a faster cleanup. (Cleanup block entries are
transient, so I suspect any increase in memory overhead there is
negligible.)
Together with CL 719960, fixes #76007.
Change-Id: I81bf3e44339e71c016c30d80bb4ee151c8263d5c
Reviewed-on: https://go-review.googlesource.com/c/go/+/720321
Reviewed-by: Cherry Mui <[email protected]>
Auto-Submit: Michael Knyszek <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>1 parent 9fd2e44 commit 1bb1f2b
3 files changed
+78
-37
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
75 | | - | |
| 75 | + | |
76 | 76 | | |
| 77 | + | |
77 | 78 | | |
78 | 79 | | |
79 | 80 | | |
| |||
82 | 83 | | |
83 | 84 | | |
84 | 85 | | |
85 | | - | |
| 86 | + | |
| 87 | + | |
86 | 88 | | |
87 | 89 | | |
88 | 90 | | |
| |||
98 | 100 | | |
99 | 101 | | |
100 | 102 | | |
101 | | - | |
102 | | - | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
103 | 118 | | |
104 | | - | |
105 | | - | |
106 | | - | |
| 119 | + | |
107 | 120 | | |
108 | 121 | | |
109 | 122 | | |
| |||
120 | 133 | | |
121 | 134 | | |
122 | 135 | | |
123 | | - | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
124 | 146 | | |
125 | 147 | | |
126 | 148 | | |
| |||
131 | 153 | | |
132 | 154 | | |
133 | 155 | | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
134 | 166 | | |
135 | 167 | | |
136 | 168 | | |
| |||
216 | 248 | | |
217 | 249 | | |
218 | 250 | | |
219 | | - | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
220 | 262 | | |
221 | 263 | | |
222 | 264 | | |
| |||
245 | 287 | | |
246 | 288 | | |
247 | 289 | | |
248 | | - | |
249 | | - | |
| 290 | + | |
| 291 | + | |
250 | 292 | | |
251 | 293 | | |
252 | 294 | | |
| |||
375 | 417 | | |
376 | 418 | | |
377 | 419 | | |
378 | | - | |
| 420 | + | |
379 | 421 | | |
380 | 422 | | |
381 | 423 | | |
| |||
396 | 438 | | |
397 | 439 | | |
398 | 440 | | |
399 | | - | |
| 441 | + | |
400 | 442 | | |
401 | 443 | | |
402 | 444 | | |
| |||
641 | 683 | | |
642 | 684 | | |
643 | 685 | | |
644 | | - | |
| 686 | + | |
| 687 | + | |
645 | 688 | | |
646 | 689 | | |
647 | 690 | | |
| |||
650 | 693 | | |
651 | 694 | | |
652 | 695 | | |
653 | | - | |
654 | | - | |
655 | | - | |
656 | | - | |
657 | | - | |
658 | | - | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
659 | 699 | | |
660 | | - | |
| 700 | + | |
661 | 701 | | |
662 | 702 | | |
663 | 703 | | |
664 | | - | |
665 | | - | |
666 | | - | |
| 704 | + | |
667 | 705 | | |
668 | 706 | | |
669 | 707 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
204 | 204 | | |
205 | 205 | | |
206 | 206 | | |
207 | | - | |
| 207 | + | |
208 | 208 | | |
209 | 209 | | |
210 | 210 | | |
| |||
251 | 251 | | |
252 | 252 | | |
253 | 253 | | |
254 | | - | |
| 254 | + | |
255 | 255 | | |
256 | 256 | | |
257 | 257 | | |
| |||
489 | 489 | | |
490 | 490 | | |
491 | 491 | | |
492 | | - | |
| 492 | + | |
493 | 493 | | |
494 | 494 | | |
495 | 495 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2161 | 2161 | | |
2162 | 2162 | | |
2163 | 2163 | | |
2164 | | - | |
| 2164 | + | |
2165 | 2165 | | |
2166 | 2166 | | |
2167 | 2167 | | |
| |||
2170 | 2170 | | |
2171 | 2171 | | |
2172 | 2172 | | |
2173 | | - | |
| 2173 | + | |
| 2174 | + | |
| 2175 | + | |
| 2176 | + | |
| 2177 | + | |
2174 | 2178 | | |
2175 | 2179 | | |
2176 | 2180 | | |
2177 | 2181 | | |
2178 | 2182 | | |
2179 | 2183 | | |
2180 | | - | |
| 2184 | + | |
2181 | 2185 | | |
2182 | 2186 | | |
2183 | 2187 | | |
| |||
2187 | 2191 | | |
2188 | 2192 | | |
2189 | 2193 | | |
2190 | | - | |
2191 | 2194 | | |
2192 | 2195 | | |
2193 | | - | |
| 2196 | + | |
2194 | 2197 | | |
2195 | 2198 | | |
2196 | | - | |
2197 | | - | |
2198 | | - | |
| 2199 | + | |
| 2200 | + | |
| 2201 | + | |
2199 | 2202 | | |
2200 | | - | |
| 2203 | + | |
2201 | 2204 | | |
2202 | 2205 | | |
2203 | 2206 | | |
| |||
2800 | 2803 | | |
2801 | 2804 | | |
2802 | 2805 | | |
2803 | | - | |
| 2806 | + | |
2804 | 2807 | | |
2805 | 2808 | | |
2806 | 2809 | | |
| |||
0 commit comments