Skip to content

Conversation

@tarcieri
Copy link
Member

@tarcieri tarcieri commented Feb 8, 2022

Rust's built-in [T]::sort_by requires both alloc and a panic handler, which is limiting for no_std targets.

This commit switches to using a heapless-friendly insertion sort implementation which is also fallible and can bubble up errors from DerOrd::der_cmp.

Insertion sort is small (<10loc) and should perform well when the input is mostly in order, which should hopefully often be the case for SetOf constructors. It also works completely in-place and is thus friendly to heapless targets.

This PR replaces the previous [T]::sort_by approach used by SetOfVec, which included a workaround which handled DerOrd::der_cmp errors by returning Ordering::Less and doing a second pass over the results to check they were correctly ordered. This might provide better performance and be worth bringing back, but for now this PR chooses instead to consolidate on a single, well-tested implementation.

Testing is performed using proptest, to check equivalence with Rust's built-in [T]::sort_by.

@tarcieri tarcieri force-pushed the der/no_std-setof-sort branch from 459a782 to 01e9fd6 Compare February 8, 2022 23:20
Rust's built-in `[T]::sort_by` requires both `alloc` and a panic
handler, which is limiting for `no_std` targets.

This commit switches to using a heapless-friendly insertion sort
implementation which is also fallible and can bubble up errors from
`DerOrd::der_cmp`.

Insertion sort is small (<10loc) and should perform well when the input
is mostly in order, which should hopefully often be the case for `SetOf`
constructors. It also works completely in-place and is thus friendly to
heapless targets.

This PR replaces the previous `[T]::sort_by` approach used by
`SetOfVec`, which included a workaround which handled `DerOrd::der_cmp`
errors by returning `Ordering::Less` and doing a second pass over the
results to check they were correctly ordered. This might provide better
performance and be worth bringing back, but for now this PR chooses
instead to consolidate on a single, well-tested implementation.

Testing is performed using proptest, to check equivalence with
Rust's built-in `[T]::sort_by`.
@tarcieri tarcieri force-pushed the der/no_std-setof-sort branch from 01e9fd6 to 7ca39e3 Compare February 8, 2022 23:23
@tarcieri tarcieri merged commit b3c3ea1 into master Feb 8, 2022
@tarcieri tarcieri deleted the der/no_std-setof-sort branch February 8, 2022 23:54
@tarcieri tarcieri mentioned this pull request May 8, 2022
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