-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Comments
We also shouldn't change |
This feels like RFC territory to me. |
@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. |
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! |
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)
algorithmO(log N)
).Example:
binary_search
requires the input slice to be sorted, otherwise the result is unspecified. Checking if the input slice is sorted makesbinary_search
O(N)
instead ofO(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:
expensive_assert!
macro tostd
std
with different features (and make this easy to use via cargo)expensive_assert!(input_slice.is_sorted())
tobinary_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...The text was updated successfully, but these errors were encountered: