Skip to content

Make unsound code opt-in.#51

Open
finnbear wants to merge 1 commit into
mainfrom
opt-in-unsound
Open

Make unsound code opt-in.#51
finnbear wants to merge 1 commit into
mainfrom
opt-in-unsound

Conversation

@finnbear

@finnbear finnbear commented Jan 1, 2025

Copy link
Copy Markdown
Member

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.

Comment thread src/derive/vec.rs
// the latter from seeing it with `not(debug_assertions)`.
#[cfg(all(feature = "unsound", not(debug_assertions)))]
{
let page_size = 4096;

Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown

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
Copy Markdown
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.

epage added a commit to epage/ruff that referenced this pull request Jun 25, 2026
Summary

`bincode` is unmaintained.
This isn't a problem on its own but if a problem comes up with it.
As ruff's use of `bincode` is relatively isolated, the risk is low.

Options:
- defer: reevaluating what to do
- use a fork: didn't see one I was confident in
- fork: nothing pressing us to do this atm
- `bitcode`: faster than `bincode` but blocked on SoftbearStudios/bitcode#51 but how that is being
  handled doesn't give me much confidence in the maintenance
- `rkyv`: almost as fast as `bitcode` and highly respected

If it wasn't for `rkyv` looking so promising, I would say we defer
this.

Test Plan

Leveraging existing tests as this should be transparent
epage added a commit to astral-sh/ruff that referenced this pull request Jun 25, 2026
Summary

`bincode` is unmaintained.
This isn't a problem on its own but if a problem comes up with it.
As ruff's use of `bincode` is relatively isolated, the risk is low.

Options:
- defer: reevaluating what to do
- use a fork: didn't see one I was confident in
- fork: nothing pressing us to do this atm
- `bitcode`: faster than `bincode` but blocked on SoftbearStudios/bitcode#51 but how that is being
  handled doesn't give me much confidence in the maintenance
- `rkyv`: almost as fast as `bitcode` and highly respected

If it wasn't for `rkyv` looking so promising, I would say we defer
this.

Test Plan

Leveraging existing tests as this should be transparent
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