FiberTaskingLib icon indicating copy to clipboard operation
FiberTaskingLib copied to clipboard

Use an atomic bitfield for m_freeFibers instead of an array of atomic<bool>

Open RichieSams opened this issue 4 years ago • 1 comments

The idea being that we can use less atomic memory fetches / memory thrashing.

Another approach would be to still use the array of atomic, but have the threads start from a random index each time. To do this we would need to have a prng initialized and seeded in TLS. (PCG could work well / be fast and simple).

Thus, GetNextFreeFiberIndex() would first query the prng in order to get the start index, and then it would search from startIndex and loop around to startIndex - 1. (the current behavior iterates from 0 to m_fiberPoolSize)

RichieSams avatar Feb 18 '21 04:02 RichieSams

No significant difference in run time of tests.

First three runs are with the new code. Last three are original code:

PS T:\Programming_Projects\ftl\FiberTaskingLib\build\x64-Release\tests> Measure-Command { .\ftl-test.exe }

TotalSeconds      : 4.1932906



PS T:\Programming_Projects\ftl\FiberTaskingLib\build\x64-Release\tests> Measure-Command { .\ftl-test.exe }

TotalSeconds      : 3.9279423


PS T:\Programming_Projects\ftl\FiberTaskingLib\build\x64-Release\tests> Measure-Command { .\ftl-test.exe }

TotalSeconds      : 4.2488083


PS T:\Programming_Projects\ftl\FiberTaskingLib\build\x64-Release\tests> Measure-Command { .\ftl-test.exe }

TotalSeconds      : 3.9117287


PS T:\Programming_Projects\ftl\FiberTaskingLib\build\x64-Release\tests> Measure-Command { .\ftl-test.exe }

TotalSeconds      : 4.2749436


PS T:\Programming_Projects\ftl\FiberTaskingLib\build\x64-Release\tests> Measure-Command { .\ftl-test.exe }

TotalSeconds      : 3.955665

RichieSams avatar Feb 18 '21 05:02 RichieSams

Closing as Not Worth the Extra Code Complexity.

Memory saving is minimal. Benchmark shows basically zero speed difference

RichieSams avatar Oct 22 '23 18:10 RichieSams