LWLock granular partition lock memory layout

Started by Alexandre Felipeabout 2 months ago5 messageshackers

Thread notes

1 note#performance#moderate#enhancement
hackorum-bot#performance#moderate#enhancement
hackorum-bot
2 days ago

@global #performance #moderate #enhancement

#performance#moderate#enhancement
Jump to latest
#1Alexandre Felipe
o.alexandre.felipe@gmail.com

This time I am not changing LWLock semantics :)

Consider the a hash table partitioned in two different ways.

1. having S partitions, guarded by LWLocks on the same cache line.
2. having a single LWLock.

Which one will give better concurrency?
Which one will cost more to lock/unlock?

I argue that having S partitions is better in several different ways.

a. Concurrency of LW_EXCLUSIVE
b. Reduced contention on LWLockWaitListLock
c. Reduced CAS iterations in LWLockAtttemptLock
d. Reduced waits (that implies wait list access and sleeping)

I included a (pointless) benchmark result.

And you can find a longer discussion in 0002 patch.

Attachments:

t254081_1
benchmark.txttext/plain; charset=US-ASCII; name=benchmark.txtDownload
v1-0002-introduce-LWLockAligned.patchapplication/octet-stream; name=v1-0002-introduce-LWLockAligned.patchDownload+37-12
v1-0001-LWLock-struct-layout.patchapplication/octet-stream; name=v1-0001-LWLock-struct-layout.patchDownload+63-53
#2Alexandre Felipe
o.alexandre.felipe@gmail.com
In reply to: Alexandre Felipe (#1)
Re: LWLock granular partition lock memory layout

Sorry for the typo

Attachments:

t254081_2
v1.1-0001-LWLock-struct-layout.patchapplication/octet-stream; name=v1.1-0001-LWLock-struct-layout.patchDownload+62-52
v1.1-0002-introduce-LWLockAligned.patchapplication/octet-stream; name=v1.1-0002-introduce-LWLockAligned.patchDownload+37-12
#3Nathan Bossart
nathandbossart@gmail.com
In reply to: Alexandre Felipe (#1)
Re: LWLock granular partition lock memory layout

On Tue, Oct 06, 2026 at 08:44:53AM +0100, Alexandre Felipe wrote:

I argue that having S partitions is better in several different ways.

a. Concurrency of LW_EXCLUSIVE
b. Reduced contention on LWLockWaitListLock
c. Reduced CAS iterations in LWLockAtttemptLock
d. Reduced waits (that implies wait list access and sleeping)

I included a (pointless) benchmark result.

Your argument would be greatly improved by showing (with numbers) what
sorts of benefits this provides in realistic workloads. I briefly looked
at benchmark.txt, but I couldn't quite tell what it was meant to show, and
you characterized it as pointless, anyway.

--
nathan

#4Alexandre Felipe
o.alexandre.felipe@gmail.com
In reply to: Nathan Bossart (#3)
Re: LWLock granular partition lock memory layout

On Wed, Oct 7, 2026 at 7:56 PM Nathan Bossart <nathandbossart@gmail.com>
wrote:

On Tue, Oct 06, 2026 at 08:44:53AM +0100, Alexandre Felipe wrote:

I argue that having S partitions is better in several different ways.

a. Concurrency of LW_EXCLUSIVE
b. Reduced contention on LWLockWaitListLock
c. Reduced CAS iterations in LWLockAtttemptLock
d. Reduced waits (that implies wait list access and sleeping)

I included a (pointless) benchmark result.

Your argument would be greatly improved by showing (with numbers) what
sorts of benefits this provides in realistic workloads. I briefly looked
at benchmark.txt, but I couldn't quite tell what it was meant to show, and
you characterized it as pointless, anyway.

Hi Nathan,

The problem is that on a realistic workload on my lightweight laptop
I can't measure observe the LWLock effect at all. So the best I can do
is to remove the realistic part and test them on a tight loop.

I believe that if you are running on a multi-socket machine that would
be visible.

#5Alexandre Felipe
o.alexandre.felipe@gmail.com
In reply to: Alexandre Felipe (#4)
Re: LWLock granular partition lock memory layout

I argue that having S partitions is better in several different ways.

a. Concurrency of LW_EXCLUSIVE
b. Reduced contention on LWLockWaitListLock
c. Reduced CAS iterations in LWLockAtttemptLock
d. Reduced waits (that implies wait list access and sleeping)

In theory this has benefits with no compromises.

In practice, it is more impactful than I initially expected.

I thought it would be noticeable only in large scale systems, but even on my
lightweight laptop with 8 cores the results are measurable.

With 128 partitions

clients base patched

1 26371.6 26149.2

8 35659.7 44958.5 +26.1%

Regards,

Alexandre

Attachments:

run-lwlock-aligned-pgbench.shtext/x-sh; charset=US-ASCII; name=run-lwlock-aligned-pgbench.shDownload