forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commit9fa6f00
committed
Rethink MemoryContext creation to improve performance.
This patch makes a number of interrelated changes to reduce the overheadinvolved in creating/deleting memory contexts. The key ideas are:* Include the AllocSetContext header of an aset.c context in its firstmalloc request, rather than allocating it separately in TopMemoryContext.This means that we now always create an initial or "keeper" block in anaset, even if it never receives any allocation requests.* Create freelists in which we can save and recycle recently-destroyedasets (this idea is due to Robert Haas).* In the common case where the name of a context is a constant string,just store a pointer to it in the context header, rather than copyingthe string.The first change eliminates a palloc/pfree cycle per context, andalso avoids bloat in TopMemoryContext, at the price that creatinga context now involves a malloc/free cycle even if the context neverreceives any allocations. That would be a loser for some commonusage patterns, but recycling short-lived contexts via the freelisteliminates that pain.Avoiding copying constant strings not only saves strlen() and strcpy()overhead, but is an essential part of the freelist optimization becauseit makes the context header size constant. Currently we make noattempt to use the freelist for contexts with non-constant names.(Perhaps someday we'll need to think harder about that, but in currentusage, most contexts with custom names are long-lived anyway.)The freelist management in this initial commit is pretty simplistic,and we might want to refine it later --- but in common workloads thatwill never matter because the freelists will never get full anyway.To create a context with a non-constant name, one is now required tocall AllocSetContextCreateExtended and specify the MEMCONTEXT_COPY_NAMEoption. AllocSetContextCreate becomes a wrapper macro, and it includesa test that will complain about non-string-literal context nameparameters on gcc and similar compilers.An unfortunate side effect of making AllocSetContextCreate a macro isthat one is now *required* to use the size parameter abstraction macros(ALLOCSET_DEFAULT_SIZES and friends) with it; the pre-9.6 habit ofwriting out individual size parameters no longer works unless youswitch to AllocSetContextCreateExtended.Internally to the memory-context-related modules, the context creationAPIs are simplified, removing the rather baroque original design wherebya context-type module called mcxt.c which then called back into thecontext-type module. That saved a bit of code duplication, but not much,and it prevented context-type modules from exercising control over theallocation of context headers.In passing, I converted the test-and-elog validation of aset sizeparameters into Asserts to save a few more cycles. The original thoughtwas that callers might compute size parameters on the fly, but in practicenobody does that, so it's useless to expend cycles on checking thosenumbers in production builds.Also, mark the memory context method-pointer structs "const",just for cleanliness.Discussion:https://postgr.es/m/2264.1512870796@sss.pgh.pa.us1 parent632b03d commit9fa6f00
File tree
21 files changed
+539
-341
lines changed- contrib/amcheck
- src
- backend
- access/transam
- catalog
- commands
- lib
- replication
- logical
- pgoutput
- utils
- cache
- hash
- mmgr
- include
- nodes
- utils
- pl
- plperl
- plpython
- tcl
21 files changed
+539
-341
lines changedLines changed: 1 addition & 3 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
295 | 295 |
| |
296 | 296 |
| |
297 | 297 |
| |
298 |
| - | |
299 |
| - | |
300 |
| - | |
| 298 | + | |
301 | 299 |
| |
302 | 300 |
| |
303 | 301 |
| |
|
Lines changed: 6 additions & 5 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
997 | 997 |
| |
998 | 998 |
| |
999 | 999 |
| |
1000 |
| - | |
1001 |
| - | |
1002 |
| - | |
1003 |
| - | |
1004 |
| - | |
| 1000 | + | |
| 1001 | + | |
| 1002 | + | |
| 1003 | + | |
| 1004 | + | |
| 1005 | + | |
1005 | 1006 |
| |
1006 | 1007 |
| |
1007 | 1008 |
| |
|
Lines changed: 4 additions & 3 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
520 | 520 |
| |
521 | 521 |
| |
522 | 522 |
| |
523 |
| - | |
524 |
| - | |
525 |
| - | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
526 | 527 |
| |
527 | 528 |
| |
528 | 529 |
| |
|
Lines changed: 1 addition & 3 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
259 | 259 |
| |
260 | 260 |
| |
261 | 261 |
| |
262 |
| - | |
263 |
| - | |
264 |
| - | |
| 262 | + | |
265 | 263 |
| |
266 | 264 |
| |
267 | 265 |
| |
|
Lines changed: 1 addition & 3 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
57 | 57 |
| |
58 | 58 |
| |
59 | 59 |
| |
60 |
| - | |
61 |
| - | |
62 |
| - | |
| 60 | + | |
63 | 61 |
| |
64 | 62 |
| |
65 | 63 |
| |
|
Lines changed: 1 addition & 3 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
925 | 925 |
| |
926 | 926 |
| |
927 | 927 |
| |
928 |
| - | |
929 |
| - | |
930 |
| - | |
| 928 | + | |
931 | 929 |
| |
932 | 930 |
| |
933 | 931 |
| |
|
Lines changed: 3 additions & 0 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
237 | 237 |
| |
238 | 238 |
| |
239 | 239 |
| |
| 240 | + | |
240 | 241 |
| |
241 | 242 |
| |
242 | 243 |
| |
243 | 244 |
| |
244 | 245 |
| |
| 246 | + | |
245 | 247 |
| |
246 | 248 |
| |
247 | 249 |
| |
248 | 250 |
| |
249 | 251 |
| |
| 252 | + | |
250 | 253 |
| |
251 | 254 |
| |
252 | 255 |
| |
|
Lines changed: 1 addition & 3 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
152 | 152 |
| |
153 | 153 |
| |
154 | 154 |
| |
155 |
| - | |
156 |
| - | |
157 |
| - | |
| 155 | + | |
158 | 156 |
| |
159 | 157 |
| |
160 | 158 |
| |
|
Lines changed: 17 additions & 12 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
669 | 669 |
| |
670 | 670 |
| |
671 | 671 |
| |
672 |
| - | |
673 |
| - | |
674 |
| - | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
675 | 676 |
| |
676 | 677 |
| |
677 | 678 |
| |
| |||
984 | 985 |
| |
985 | 986 |
| |
986 | 987 |
| |
987 |
| - | |
988 |
| - | |
989 |
| - | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
| 991 | + | |
990 | 992 |
| |
991 | 993 |
| |
992 | 994 |
| |
| |||
1566 | 1568 |
| |
1567 | 1569 |
| |
1568 | 1570 |
| |
1569 |
| - | |
1570 |
| - | |
1571 |
| - | |
| 1571 | + | |
| 1572 | + | |
| 1573 | + | |
| 1574 | + | |
1572 | 1575 |
| |
1573 | 1576 |
| |
1574 | 1577 |
| |
| |||
5537 | 5540 |
| |
5538 | 5541 |
| |
5539 | 5542 |
| |
5540 |
| - | |
5541 |
| - | |
5542 |
| - | |
| 5543 | + | |
| 5544 | + | |
| 5545 | + | |
| 5546 | + | |
| 5547 | + | |
5543 | 5548 |
| |
5544 | 5549 |
| |
5545 | 5550 |
| |
|
Lines changed: 4 additions & 3 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
294 | 294 |
| |
295 | 295 |
| |
296 | 296 |
| |
297 |
| - | |
298 |
| - | |
299 |
| - | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
300 | 301 |
| |
301 | 302 |
| |
302 | 303 |
| |
|
Lines changed: 5 additions & 3 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
340 | 340 |
| |
341 | 341 |
| |
342 | 342 |
| |
343 |
| - | |
344 |
| - | |
345 |
| - | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
346 | 348 |
| |
347 | 349 |
| |
348 | 350 |
| |
|
Lines changed: 5 additions & 7 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
177 | 177 |
| |
178 | 178 |
| |
179 | 179 |
| |
180 |
| - | |
181 |
| - | |
| 180 | + | |
182 | 181 |
| |
183 | 182 |
| |
184 | 183 |
| |
| |||
420 | 419 |
| |
421 | 420 |
| |
422 | 421 |
| |
423 |
| - | |
424 |
| - | |
425 |
| - | |
426 |
| - | |
427 |
| - | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
428 | 426 |
| |
429 | 427 |
| |
430 | 428 |
| |
|
0 commit comments
Comments
(0)