Hi,

On 2025-11-24 00:10:03 +0100, Andreas Karlsson wrote:
> Andres pointed out this possible optimization on Discord so I hacked up a
> quick patch which avoids taking a lock when reading the LSN from a page on
> architectures where we can be sure to not get a torn value. It is always
> nice to remove a lock from a reasonably hot code path.

Nice.

>  static inline XLogRecPtr
>  PageXLogRecPtrGet(PageXLogRecPtr val)
>  {
> -     return (uint64) val.xlogid << 32 | val.xrecoff;
> +     return val;
>  }
>  
>  #define PageXLogRecPtrSet(ptr, lsn) \
> -     ((ptr).xlogid = (uint32) ((lsn) >> 32), (ptr).xrecoff = (uint32) (lsn))
> +     ((ptr) = (lsn))
> +
> +#else
> +
> +static inline XLogRecPtr
> +PageXLogRecPtrGet(volatile PageXLogRecPtr val)
> +{
> +     return (val << 32) | (val >> 32);
> +}

A volatile on a non-pointer won't do you much good, I'm afraid. You need to
make sure that the underlying value is read as a single 8 byte read, I don't
see how this guarantees that, unfortunately.

Greetings,

Andres Freund


Reply via email to