forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commit590b045
committed
Improve memory management and performance of tuplestore.c
Here we make tuplestore.c use a generation.c memory context rather thanallocating tuples into the CurrentMemoryContext, which primarily is theExecutorState or PortalHoldContext memory context. Not having adedicated context can cause the CurrentMemoryContext context to becomebloated when pfree'd chunks are not reused by future tuples. Usinggeneration speeds up users of tuplestore.c, such as the Materialize,WindowAgg and CTE Scan executor nodes. The main reason for the speedup isdue to generation.c being more memory efficient than aset.c memorycontexts. Specifically, generation does not round sizes up to the nextpower of 2 value. This both saves memory, allowing more tuples to fit inwork_mem, but also makes the memory usage more compact and fit on fewercachelines. One benchmark showed up to a 22% performance increase in aquery containing a Materialize node. Much higher gains are possible ifthe memory reduction prevents tuplestore.c from spilling to disk. This isespecially true for WindowAgg nodes where improvements of several thousandtimes are possible if the memory reductions made here prevent tuplestorefrom spilling to disk.Additionally, a generation.c memory context is much better suited for thisjob as it works well with FIFO palloc/pfree patterns, which is exactly howtuplestore.c uses it. Because of the way generation.c allocates memory,tuples consecutively stored in tuplestores are much more likely to bestored consecutively in memory. This allows the CPU's hardware prefetcherto work more efficiently as it provides a more predictable pattern toallow cachelines for the next tuple to be loaded from RAM in advance ofthem being needed by the executor.Using a dedicated memory context for storing tuples also allows us to moreefficiently clean up the memory used by the tuplestore as we can reset ordelete the context rather than looping over all stored tuples andpfree'ing them one by one.Also, remove a badly placed USEMEM call in readtup_heap(). The tuplewasn't being allocated in the Tuplestorestate's context, so no need toadjust the memory consumed by the tuplestore there.Author: David RowleyReviewed-by: Matthias van de Meent, Dmitry DolgovDiscussion:https://postgr.es/m/CAApHDvp5Py9g4Rjq7_inL3-MCK1Co2CRt_YWFwTU2zfQix0p4A@mail.gmail.com1 parent53abb1e commit590b045
1 file changed
+40
-15
lines changedLines changed: 40 additions & 15 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
266 | 266 |
| |
267 | 267 |
| |
268 | 268 |
| |
269 |
| - | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
270 | 277 |
| |
271 | 278 |
| |
272 | 279 |
| |
| |||
429 | 436 |
| |
430 | 437 |
| |
431 | 438 |
| |
432 |
| - | |
| 439 | + | |
| 440 | + | |
433 | 441 |
| |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
434 | 452 |
| |
435 |
| - | |
436 |
| - | |
437 |
| - | |
438 |
| - | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
439 | 458 |
| |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
440 | 471 |
| |
441 | 472 |
| |
442 | 473 |
| |
| |||
458 | 489 |
| |
459 | 490 |
| |
460 | 491 |
| |
461 |
| - | |
462 |
| - | |
463 | 492 |
| |
464 | 493 |
| |
465 |
| - | |
466 |
| - | |
467 |
| - | |
468 |
| - | |
469 |
| - | |
470 |
| - | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
471 | 497 |
| |
472 | 498 |
| |
473 | 499 |
| |
| |||
1578 | 1604 |
| |
1579 | 1605 |
| |
1580 | 1606 |
| |
1581 |
| - | |
1582 | 1607 |
| |
1583 | 1608 |
| |
1584 | 1609 |
| |
|
0 commit comments
Comments
(0)