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

allow standard library functions to check expensive pre-/postconditions #44371

Closed
3 tasks
gnzlbg opened this issue Sep 6, 2017 · 5 comments
Closed
3 tasks

Comments

@gnzlbg
Copy link
Contributor

gnzlbg commented Sep 6, 2017

See also: #44370

There should be a way for standard library functions to check expensive pre-/postconditions, by which I mean preconditions that change the complexity of the standard algorithms (e.g. by making a O(1) algorithm O(log N)).

Example: binary_search requires the input slice to be sorted, otherwise the result is unspecified. Checking if the input slice is sorted makes binary_search O(N) instead of O(log N) (*) defeating the purpose of binary search. Still, such a check is useful to detect logic errors in user programs: it is a violated precondition after all.

So we are at an impasse. What could we do?

We could:

  • add a expensive_assert! macro to std
  • allow users to configure std with different features (and make this easy to use via cargo)
  • add expensive_assert!(input_slice.is_sorted()) to binary_search

An alternative would be to perform this test using debug_assert!. However, making all debug builds exponentially slower is unacceptable.

(*) Since the standard library does not guarantee the complexity of anything, we can actually make binary_search O(N) in debug builds and that would be a backwards compatible change...

@gnzlbg gnzlbg changed the title allow standard library functions to check expensive preconditions allow standard library functions to check expensive pre-/postconditions Sep 6, 2017
@arielb1
Copy link
Contributor

arielb1 commented Sep 6, 2017

We also shouldn't change binary_search to be O(n) on debug-assertions builds - debug-assertions should be for the last 2x of performance, not for potential order-of-magnitude slowdowns.

@steveklabnik
Copy link
Member

This feels like RFC territory to me.

@gnzlbg
Copy link
Contributor Author

gnzlbg commented Sep 6, 2017

@steveklabnik is it ok to discuss this as a pre-RFC here then or should I post this in the internals forum? I really don't know what the design space is, so even if this turns out to be a mini RFC, I cannot write one yet.

@steveklabnik
Copy link
Member

I really don't know what the design space is

Yeah, this makes it not a good issue; issues should have a clear way to resolve them.

A post on the internals forum would be perfect. Thanks!

@gnzlbg
Copy link
Contributor Author

gnzlbg commented Sep 6, 2017

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

No branches or pull requests

3 participants