Various micro-optimizations for GetSnapshopData().

Heikki Linnakangas had the idea of rearranging GetSnapshotData to
avoid checking for sub-XIDs when no top-level XID is present.  This
patch does that plus further a bit of further, related rearrangement.
Benchmarking show a significant improvement on unlogged tables at
higher concurrency levels, and mostly indifferent result on permanent
tables (which are presumably bottlenecked elsewhere).  Most of the
benefit seems to come from using the new NormalTransactionIdPrecedes()
macro rather than the function call TransactionIdPrecedes().
This commit is contained in:
Robert Haas 2011-12-16 21:44:26 -05:00
parent a4cd6abcc9
commit 0d76b60db4
2 changed files with 24 additions and 17 deletions

View File

@ -1324,30 +1324,33 @@ GetSnapshotData(Snapshot snapshot)
/* Update globalxmin to be the smallest valid xmin */ /* Update globalxmin to be the smallest valid xmin */
xid = pgxact->xmin; /* fetch just once */ xid = pgxact->xmin; /* fetch just once */
if (TransactionIdIsNormal(xid) && if (TransactionIdIsNormal(xid) &&
TransactionIdPrecedes(xid, globalxmin)) NormalTransactionIdPrecedes(xid, globalxmin))
globalxmin = xid; globalxmin = xid;
/* Fetch xid just once - see GetNewTransactionId */ /* Fetch xid just once - see GetNewTransactionId */
xid = pgxact->xid; xid = pgxact->xid;
/* /*
* If the transaction has been assigned an xid < xmax we add it to * If the transaction has no XID assigned, we can skip it; it won't
* the snapshot, and update xmin if necessary. There's no need to * have sub-XIDs either. If the XID is >= xmax, we can also skip
* store XIDs >= xmax, since we'll treat them as running anyway. * it; such transactions will be treated as running anyway (and any
* We don't bother to examine their subxids either. * sub-XIDs will also be >= xmax).
*
* We don't include our own XID (if any) in the snapshot, but we
* must include it into xmin.
*/ */
if (TransactionIdIsNormal(xid)) if (!TransactionIdIsNormal(xid)
{ || !NormalTransactionIdPrecedes(xid, xmax))
if (TransactionIdFollowsOrEquals(xid, xmax))
continue; continue;
if (pgxact != MyPgXact)
snapshot->xip[count++] = xid; /*
if (TransactionIdPrecedes(xid, xmin)) * We don't include our own XIDs (if any) in the snapshot, but we
xmin = xid; * must include them in xmin.
} */
if (NormalTransactionIdPrecedes(xid, xmin))
xmin = xid;
if (pgxact == MyPgXact)
continue;
/* Add XID to snapshot. */
snapshot->xip[count++] = xid;
/* /*
* Save subtransaction XIDs if possible (if we've already * Save subtransaction XIDs if possible (if we've already
@ -1364,7 +1367,7 @@ GetSnapshotData(Snapshot snapshot)
* *
* Again, our own XIDs are not included in the snapshot. * Again, our own XIDs are not included in the snapshot.
*/ */
if (!suboverflowed && pgxact != MyPgXact) if (!suboverflowed)
{ {
if (pgxact->overflowed) if (pgxact->overflowed)
suboverflowed = true; suboverflowed = true;

View File

@ -58,6 +58,10 @@
(dest)--; \ (dest)--; \
} while ((dest) < FirstNormalTransactionId) } while ((dest) < FirstNormalTransactionId)
/* compare two XIDs already known to be normal; this is a macro for speed */
#define NormalTransactionIdPrecedes(id1, id2) \
(AssertMacro(TransactionIdIsNormal(id1) && TransactionIdIsNormal(id2)), \
(int32) ((id1) - (id2)) < 0)
/* ---------- /* ----------
* Object ID (OID) zero is InvalidOid. * Object ID (OID) zero is InvalidOid.