Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite BlockPool using double-checked locking pattern #1234

Open
wks opened this issue Nov 13, 2024 · 0 comments
Open

Rewrite BlockPool using double-checked locking pattern #1234

wks opened this issue Nov 13, 2024 · 0 comments

Comments

@wks
Copy link
Collaborator

wks commented Nov 13, 2024

Currently, BlockPool::head_global_freed_blocks is an RwLock<Option<BlockQueue<B>>>, despite the fact that BlockQueue already supports lock-free atomic pop() operation which is implemented using fetch_update.

We may rewrite BlockPool using the double-checked locking pattern. On the fast path, we simply do an atomic saturated-sub to get the index of the next available element. If there is no available element, we fall back to the slow path. In the slow path, we shall lock the global_freed_blocks, check if head_global_freed_blocks is still empty (the "double check" part because another thread may have entered the slow path, too). It it still is, the current thread shall move one BlockQueue from global_freed_blocks to head_global_freed_blocks. It shall populate the BlockQueue::data array before atomically setting BlockQueue::cursor to the proper length so that other threads know the head_global_freed_blocks has been re-populated.

Not sure how much performance benefit this would bring because this is on the slow path. But for allocation-intensive work loads, this may accelerate the allocation slow path which still takes up a significant amount of time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant