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

Improve example solution of circular-buffer #1724

Merged
merged 1 commit into from
Sep 15, 2023
Merged

Conversation

senekor
Copy link
Contributor

@senekor senekor commented Sep 10, 2023

The trait bounds Default + Clone are semantically incorrect. A proper circular buffer needs to handle all kinds of elements.

The reason these bounds are here is because it allows to avoid unsafe while enjoying the most efficient memory layout.
However, there is another performance downside:
The implementations of Default and Clone may be expensive (e.g. cause heap allocations.)

As came up in this discussion, there are other weird side effects, for example for Rc which has a non-standard implementation of Clone: #1652

The approach I chose here get's rid of the trait bounds and wraps every element in an Option.
In the worst case, this may lead to twice the memory consumption. (e.g. Option<u64> takes 16 bytes because of alignment and padding) This approach is semantically correct, but not the most performant.

The most performant solution would be to use unsafe. I'd like to avoid that in example solutions.

@senekor senekor marked this pull request as ready for review September 10, 2023 20:07
@senekor senekor changed the base branch from unimpl-todo to no-test-prefix September 11, 2023 18:28
@senekor senekor mentioned this pull request Sep 11, 2023
Base automatically changed from no-test-prefix to main September 14, 2023 10:17
The trait bounds `Default + Clone` are semantically incorrect.
A proper circular buffer needs to handle all kinds of elements.

The reason these bounds are here is because it allows to avoid `unsafe`
while enjoying the most efficient memory layout.
However, there is another performance downside:
The implementations of `Default` and `Clone` may be expensive
(e.g. cause heap allocations.)

As came up in this discussion, there are other weird side effects,
for example for `Rc` which has a non-standard implementation of `Clone`:
#1652

The approach I chose here get's rid of the trait bounds and wraps
every element in an `Option`.
In the worst case, this may lead to twice the memory consumption.
(e.g. `Option<u64>` takes 16 bytes because of alignment)
This approach is semantically correct, but not the most performant.

The most performant solution would be to use `unsafe`.
I'd like to avoid that in example solutions.
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

Successfully merging this pull request may close these issues.

2 participants