Skip to content

Make unsound code opt-in. #51

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Make unsound code opt-in. #51

wants to merge 1 commit into from

Conversation

finnbear
Copy link
Member

@finnbear finnbear commented Jan 1, 2025

Fixes #50

Benchmarked on i7-7700k

Before (default features) or After (unsound feature):
test str::tests2::bench_str_vec_encode                      ... bench:      98,474.54 ns/iter (+/- 34,320.73)
test derive::option::tests2::bench_option_u16_vec_encode    ... bench:         674.79 ns/iter (+/- 102.70)
test bool::test2::bench_bool_vecs_encode                    ... bench:         327.71 ns/iter (+/- 9.80)
test derive::vec::test::bench_vec_encode                    ... bench:          35.98 ns/iter (+/- 2.39)

After (default features):
test str::tests2::bench_str_vec_encode                      ... bench:     126,276.70 ns/iter (+/- 4,599.54)
test derive::option::tests2::bench_option_u16_vec_encode    ... bench:         767.94 ns/iter (+/- 26.88)
test bool::test2::bench_bool_vecs_encode                    ... bench:         454.65 ns/iter (+/- 18.57)
test derive::vec::test::bench_vec_encode                    ... bench:          38.67 ns/iter (+/- 4.02)

Edit: need to benchmark again to show worst case performance difference.

// the latter from seeing it with `not(debug_assertions)`.
#[cfg(all(feature = "unsound", not(debug_assertions)))]
{
let page_size = 4096;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello
I was skimming rust serialization libraries and somewhat ended up here.

I'm not 100% sure, but I think that for posix systems the hardcoded page size could be replaced by something likesysconf(PAGE_SIZE); (man sysconf) then in theory be sound on compliant posix systems.

Feel free to take or toss my comment

have a nice day.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, thanks for your comment. Note that this optimization is most efficient if the page size is known statically and sysconf appears to retrieve it at runtime. AFAIK 4096 is safe because the page size won't be lower than 4096.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello, thanks for your comment. Note that this optimization is most efficient if the page size is known statically and sysconf appears to retrieve it at runtime. AFAIK 4096 is safe because the page size won't be lower than 4096.

Oh makes sense for the compile time optimisations.

Sysconf at runtime could also be used to choose the correct implementation something akin to:

if sysconf(SC_PAGESIZE) == 4096 { // or using something like a lazy static
// read_mem aligned
} else {
ptr.copy_non_overlapping();
}

Downside: using sysconf would introduce a dependency on libc.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well a higher page size wouldn't require falling back to copy_non_overlapping. It would just slightly reduce the probability of the fallback, as the fast path could copy over 4096 byte boundaries within the larger page. The run-time overhead of this check (at least one predicted branch) might outweigh the benefits of this.

A lower page size would require something like this but, AFAIK, that's not possible.

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.

unsafe_wild_copy is undefined behaviour
2 participants