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

Return T as opposed to &T on remove #29

Open
ccollie opened this issue Nov 12, 2024 · 1 comment
Open

Return T as opposed to &T on remove #29

ccollie opened this issue Nov 12, 2024 · 1 comment

Comments

@ccollie
Copy link

ccollie commented Nov 12, 2024

I'm storing a struct which is relatively "heavy", but in certain situations I need the capability to swap elements between indexes. Remove current returns &T when I need T, so I need to do a .clone() on the returned value.

Perhaps it's possible to add a swap() method ?

@ibraheemdev
Copy link
Owner

The reason papaya returns T is because there may be readers that still have access to the value being removed. It may be possible to add a remove_blocking method that waits for all readers to stop accessing a given value, but this may be more expensive than expected as memory reclamation is performed in batches. You could also implement this yourself with a one shot channel and a custom Drop implementation.

There's also no hardware instruction for swapping two atomic pointers, so a swap method would require locks or a complex atomic algorithm that I'm not sure is worth it.

If the clone is very expensive for you your only alternative is unfortunately fine-grained locking on each value.

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

2 participants