kotlinx.coroutines icon indicating copy to clipboard operation
kotlinx.coroutines copied to clipboard

Simplify full-queue check to use single margin

Open PPLong222 opened this issue 7 months ago • 2 comments
trafficstars

Update outdated full-queue check logic of LockFreeTaskQueueCore. No more need to have 2 margin elements; one is sufficient.

Tested in LockFreeTaskQueueStressTest and LockFreeTaskQueueTest

See also #4205

PPLong222 avatar Apr 23 '25 23:04 PPLong222

I'm a little bit out of capacity right now, and the change is really subtle (aka requires quite a deep dive into git history and queue implementation semantics), so expect some arbitrary delay here. Sorry for the inconvenience!

qwwdfsad avatar May 06 '25 13:05 qwwdfsad

Thanks a lot for taking the time to reply!

I realise this change is quite subtle and that getting to the bottom of it means digging through git history and the queue’s invariants, so please feel free to review it whenever you have the bandwidth. From what I can tell, the patch shouldn’t affect performance or observable behaviour much—it’s mainly about correctness semantics and avoiding confusion for future readers(or maybe I just have misunderstood something).

The motivation came while I was reading LockFreeTaskQueueCore: I couldn’t understand why the full-queue check uses two extra slots instead of the usual single margin you see in a circular buffer. After spending a day tracing the code and trying a variety of stress-test cases, I still couldn’t find a situation where one margin element isn’t enough.

If I’ve overlooked something, just let me know and I’ll be happy to revise (or close) the PR. Thanks again, and take all the time you need!

PPLong222 avatar May 06 '25 16:05 PPLong222