Skip to content

Commit 1b88b89

Browse files
committed
Specialize checkpointer sort functions.
When sorting a potentially large number of dirty buffers, the checkpointer can benefit from a faster sort routine. One reported improvement on a large buffer pool system was 1.4s -> 0.6s. Reviewed-by: Andres Freund <andres@anarazel.de> Discussion: https://postgr.es/m/CA%2BhUKGJ2-eaDqAum5bxhpMNhvuJmRDZxB_Tow0n-gse%2BHG0Yig%40mail.gmail.com
1 parent 519e4c9 commit 1b88b89

File tree

1 file changed

+22
-15
lines changed

1 file changed

+22
-15
lines changed

src/backend/storage/buffer/bufmgr.c

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -488,8 +488,8 @@ static void FindAndDropRelFileNodeBuffers(RelFileNode rnode,
488488
static void AtProcExit_Buffers(int code, Datum arg);
489489
static void CheckForBufferLeaks(void);
490490
static int rnode_comparator(const void *p1, const void *p2);
491-
static int buffertag_comparator(const void *p1, const void *p2);
492-
static int ckpt_buforder_comparator(const void *pa, const void *pb);
491+
static inline int buffertag_comparator(const BufferTag *a, const BufferTag *b);
492+
static inline int ckpt_buforder_comparator(const CkptSortItem *a, const CkptSortItem *b);
493493
static int ts_ckpt_progress_comparator(Datum a, Datum b, void *arg);
494494

495495

@@ -1831,6 +1831,13 @@ UnpinBuffer(BufferDesc *buf, bool fixOwner)
18311831
}
18321832
}
18331833

1834+
#define ST_SORT sort_checkpoint_bufferids
1835+
#define ST_ELEMENT_TYPE CkptSortItem
1836+
#define ST_COMPARE(a, b) ckpt_buforder_comparator(a, b)
1837+
#define ST_SCOPE static
1838+
#define ST_DEFINE
1839+
#include <lib/sort_template.h>
1840+
18341841
/*
18351842
* BufferSync -- Write out all dirty buffers in the pool.
18361843
*
@@ -1931,8 +1938,7 @@ BufferSync(int flags)
19311938
* end up writing to the tablespaces one-by-one; possibly overloading the
19321939
* underlying system.
19331940
*/
1934-
qsort(CkptBufferIds, num_to_scan, sizeof(CkptSortItem),
1935-
ckpt_buforder_comparator);
1941+
sort_checkpoint_bufferids(CkptBufferIds, num_to_scan);
19361942

19371943
num_spaces = 0;
19381944

@@ -4567,11 +4573,9 @@ WaitBufHdrUnlocked(BufferDesc *buf)
45674573
/*
45684574
* BufferTag comparator.
45694575
*/
4570-
static int
4571-
buffertag_comparator(const void *a, const void *b)
4576+
static inline int
4577+
buffertag_comparator(const BufferTag *ba, const BufferTag *bb)
45724578
{
4573-
const BufferTag *ba = (const BufferTag *) a;
4574-
const BufferTag *bb = (const BufferTag *) b;
45754579
int ret;
45764580

45774581
ret = rnode_comparator(&ba->rnode, &bb->rnode);
@@ -4598,12 +4602,9 @@ buffertag_comparator(const void *a, const void *b)
45984602
* It is important that tablespaces are compared first, the logic balancing
45994603
* writes between tablespaces relies on it.
46004604
*/
4601-
static int
4602-
ckpt_buforder_comparator(const void *pa, const void *pb)
4605+
static inline int
4606+
ckpt_buforder_comparator(const CkptSortItem *a, const CkptSortItem *b)
46034607
{
4604-
const CkptSortItem *a = (const CkptSortItem *) pa;
4605-
const CkptSortItem *b = (const CkptSortItem *) pb;
4606-
46074608
/* compare tablespace */
46084609
if (a->tsId < b->tsId)
46094610
return -1;
@@ -4694,6 +4695,13 @@ ScheduleBufferTagForWriteback(WritebackContext *context, BufferTag *tag)
46944695
IssuePendingWritebacks(context);
46954696
}
46964697

4698+
#define ST_SORT sort_pending_writebacks
4699+
#define ST_ELEMENT_TYPE PendingWriteback
4700+
#define ST_COMPARE(a, b) buffertag_comparator(&a->tag, &b->tag)
4701+
#define ST_SCOPE static
4702+
#define ST_DEFINE
4703+
#include <lib/sort_template.h>
4704+
46974705
/*
46984706
* Issue all pending writeback requests, previously scheduled with
46994707
* ScheduleBufferTagForWriteback, to the OS.
@@ -4713,8 +4721,7 @@ IssuePendingWritebacks(WritebackContext *context)
47134721
* Executing the writes in-order can make them a lot faster, and allows to
47144722
* merge writeback requests to consecutive blocks into larger writebacks.
47154723
*/
4716-
qsort(&context->pending_writebacks, context->nr_pending,
4717-
sizeof(PendingWriteback), buffertag_comparator);
4724+
sort_pending_writebacks(context->pending_writebacks, context->nr_pending);
47184725

47194726
/*
47204727
* Coalesce neighbouring writes, but nothing else. For that we iterate

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy