Skip to content

Improve example solution of circular-buffer #1724

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

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 force-pushed the circ-buffer-example branch from 5df3917 to 40dc162 Compare September 11, 2023 18:28
@senekor senekor mentioned this pull request Sep 11, 2023
@senekor senekor force-pushed the circ-buffer-example branch from 40dc162 to c406b9c Compare September 11, 2023 20:21
@senekor senekor force-pushed the circ-buffer-example branch from c406b9c to df1742b Compare September 11, 2023 21:08
@senekor senekor force-pushed the circ-buffer-example branch from df1742b to d1e235c Compare September 12, 2023 18:10
@senekor senekor force-pushed the circ-buffer-example branch from d1e235c to 9e4c8c5 Compare September 12, 2023 18:16
@senekor senekor force-pushed the circ-buffer-example branch from 9e4c8c5 to 746cfd5 Compare September 12, 2023 18:49
@senekor senekor force-pushed the circ-buffer-example branch from 746cfd5 to b388172 Compare September 13, 2023 16:57
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.
@senekor senekor force-pushed the circ-buffer-example branch from b388172 to d3ed68a Compare September 14, 2023 14:51
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.

None yet

2 participants