News for hackers. Stuff that matters.
Re:Optimizing a Ring Buffer for Throughput by cafxx on Tuesday June 27, 2023 @04:54AM (#36490498)
A point that AFAICT is not articulated in the article is why the two cached fields should be in their own dedicated cache line (e.g. why readIdxCached_ can not share the cache line with writeIdx_).
Re:Optimizing a Ring Buffer for Throughput by gpderetta on Tuesday June 27, 2023 @05:30AM (#36490745)
They should be in the same cacheline as you suggest. The writer will always touch both writeIdx and readIdxCached so it makes sense to be together. Splitting them won't affect synthetic benchmarks, but it just wasting space in a larger application.
Re:Optimizing a Ring Buffer for Throughput by rigtorp on Tuesday June 27, 2023 @06:46AM (#36491178)
There might be an additional optimization in having the writer also cache it's write index on the cache line together with the read index cache. This way the writer would only do writes to the write index cache line. The hardware might be able to optimize this. I wonder how it interacts with UMWAIT on the latest cores.
Re:Optimizing a Ring Buffer for Throughput by gpderetta on Tuesday June 27, 2023 @07:11AM (#36491318)
Reading its own values is never a problem, the writeIndex cacheline will switch from E (or M) to S (or O), but it is never evicted, so reads from it never miss. For frequent writes the read will be fulfilled by the store buffer anyway.
Re:Optimizing a Ring Buffer for Throughput by rigtorp on Tuesday June 27, 2023 @08:59AM (#36492105)
I think it will be invalidated due to RFO when the reader reads the write index. Only when multiple readers reads the same cache line without any intervening write will the RFO heuristic be disabled.
Re:Optimizing a Ring Buffer for Throughput by lordnacho on Tuesday June 27, 2023 @05:12AM (#36490619)
False sharing perhaps? At least that is my shoot-from-the-hip response.

https://stackoverflow.com/questions/22766191/what-is-false-s...

Re:Optimizing a Ring Buffer for Throughput by mananaysiempre on Tuesday June 27, 2023 @05:41AM (#36490804)
I don’t get it. Both readIdx_ and writeIdxCached_ are “owned” by the consumer. (That being the point of a “cached” copy.) The producer might read readIdx_ occasionally, whenever we go through the whole buffer, but otherwise only the consumers access these. So why should we avoid putting them on the same cache line?
Re:Optimizing a Ring Buffer for Throughput by gpderetta on Tuesday June 27, 2023 @05:33AM (#36490768)
false sharing is the reason for splitting readIdx and writeIdx. But there is no reason to split readIdx and writeIdxCached.
Re:Optimizing a Ring Buffer for Throughput by 4gotunameagain on Tuesday June 27, 2023 @05:27AM (#36490719)
Yeap. The linked cpp ref doc has a nice example:

https://en.cppreference.com/w/cpp/thread/hardware_destructiv...

Re:Optimizing a Ring Buffer for Throughput by cafxx on Tuesday June 27, 2023 @06:19AM (#36491026)
In the example you linked it's comparing two threads reading from the same or separate cache lines, no? If so, that's not really the point I was referring to (as the two variables I mentioned as example are accessed by a single thread, not by two threads).