1
0
mirror of https://github.com/postgres/postgres.git synced 2025-10-25 13:17:41 +03:00

Eliminate divide in new fast-path locking code

c4d5cb71d2 adjusted the fast-path locking code to allow some
configuration of the number of fast-path locking slots via the
max_locks_per_transaction GUC.  In that commit the FAST_PATH_REL_GROUP()
macro used integer division to determine the fast-path locking group slot
to use for the lock.

The divisor in this case is always a power-of-two value.  Here we swap
out the divide by a bitwise-AND, which is a significantly faster
operation to perform.

In passing, adjust the code that's setting FastPathLockGroupsPerBackend
so that it's more clear that the value being set is a power-of-two.

Also, adjust some comments in the area which contained some magic
numbers.  It seems better to justify the 1024 upper limit in the
location where the #define is made instead of where it is used.

Author: David Rowley <drowleyml@gmail.com>
Reviewed-by: Tomas Vondra <tomas@vondra.me>
Discussion: https://postgr.es/m/CAApHDvodr3bcnpxcs7+k-3cFwYR0tP-BYhyd2PpDhe-bCx9i=g@mail.gmail.com
This commit is contained in:
David Rowley
2025-04-27 11:53:40 +12:00
parent 27757677ca
commit 936457419d
3 changed files with 27 additions and 20 deletions

View File

@@ -86,6 +86,14 @@ struct XidCache
*/
extern PGDLLIMPORT int FastPathLockGroupsPerBackend;
/*
* Define the maximum number of fast-path locking groups per backend.
* This must be a power-of-two value. The actual number of fast-path
* lock groups is calculated in InitializeFastPathLocks() based on
* max_locks_per_transaction. 1024 is an arbitrary upper limit (matching
* max_locks_per_transaction = 16k). Values over 1024 are unlikely to be
* beneficial as there are bottlenecks we'll hit way before that.
*/
#define FP_LOCK_GROUPS_PER_BACKEND_MAX 1024
#define FP_LOCK_SLOTS_PER_GROUP 16 /* don't change */
#define FastPathLockSlotsPerBackend() \