Skip to content

Commit 37a6e5d

Browse files
committed
Optimize xid/subxid searches in XidInMVCCSnapshot().
As reported by Yura Sokolov, scanning the snapshot->xip array has noticeable impact on scalability when there are a large number of concurrent writers. Use the optimized (on x86-64) routine from b6ef167 to speed up searches through the [sub]xip arrays. One benchmark showed a 5% increase in transaction throughput with 128 concurrent writers, and a 50% increase in a pathological case of 1024 writers. While a hash table would have scaled even better, it was ultimately rejected because of concerns around code complexity and memory allocation. Credit to Andres Freund for the idea to optimize linear search using SIMD instructions. Nathan Bossart Reviewed by: Andres Freund, John Naylor, Bharath Rupireddy, Masahiko Sawada Discussion: https://postgr.es/m/20220713170950.GA3116318%40nathanxps13
1 parent a8c0128 commit 37a6e5d

File tree

1 file changed

+7
-21
lines changed

1 file changed

+7
-21
lines changed

src/backend/utils/time/snapmgr.c

+7-21
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include "datatype/timestamp.h"
5757
#include "lib/pairingheap.h"
5858
#include "miscadmin.h"
59+
#include "port/pg_lfind.h"
5960
#include "storage/predicate.h"
6061
#include "storage/proc.h"
6162
#include "storage/procarray.h"
@@ -2284,8 +2285,6 @@ RestoreTransactionSnapshot(Snapshot snapshot, void *source_pgproc)
22842285
bool
22852286
XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot)
22862287
{
2287-
uint32 i;
2288-
22892288
/*
22902289
* Make a quick range check to eliminate most XIDs without looking at the
22912290
* xip arrays. Note that this is OK even if we convert a subxact XID to
@@ -2317,13 +2316,8 @@ XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot)
23172316
if (!snapshot->suboverflowed)
23182317
{
23192318
/* we have full data, so search subxip */
2320-
int32 j;
2321-
2322-
for (j = 0; j < snapshot->subxcnt; j++)
2323-
{
2324-
if (TransactionIdEquals(xid, snapshot->subxip[j]))
2325-
return true;
2326-
}
2319+
if (pg_lfind32(xid, snapshot->subxip, snapshot->subxcnt))
2320+
return true;
23272321

23282322
/* not there, fall through to search xip[] */
23292323
}
@@ -2344,16 +2338,11 @@ XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot)
23442338
return false;
23452339
}
23462340

2347-
for (i = 0; i < snapshot->xcnt; i++)
2348-
{
2349-
if (TransactionIdEquals(xid, snapshot->xip[i]))
2350-
return true;
2351-
}
2341+
if (pg_lfind32(xid, snapshot->xip, snapshot->xcnt))
2342+
return true;
23522343
}
23532344
else
23542345
{
2355-
int32 j;
2356-
23572346
/*
23582347
* In recovery we store all xids in the subxact array because it is by
23592348
* far the bigger array, and we mostly don't know which xids are
@@ -2383,11 +2372,8 @@ XidInMVCCSnapshot(TransactionId xid, Snapshot snapshot)
23832372
* indeterminate xid. We don't know whether it's top level or subxact
23842373
* but it doesn't matter. If it's present, the xid is visible.
23852374
*/
2386-
for (j = 0; j < snapshot->subxcnt; j++)
2387-
{
2388-
if (TransactionIdEquals(xid, snapshot->subxip[j]))
2389-
return true;
2390-
}
2375+
if (pg_lfind32(xid, snapshot->subxip, snapshot->subxcnt))
2376+
return true;
23912377
}
23922378

23932379
return false;

0 commit comments

Comments
 (0)