-
Notifications
You must be signed in to change notification settings - Fork 743
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
gsl::not_null can't be anymore zero cost abstraction. #877
Comments
@unixod Could you elaborate further on what you meant by Expects and Ensures being zero cost? |
Sure, I think the easier way to explain my point is to show it in example: https://godbolt.org/z/grfjgv The following is explanations. We have a function: #include <gsl/gsl>
int foo(gsl::not_null<int*> ptr)
{
return *ptr;
} and output of two compilation processes (both use -O3 and -DGSL_UNENFORCED_ON_CONTRACT_VIOLATION):
The result assembly generated for old version of GSL is identical to accessing raw pointer (https://godbolt.org/z/hYtxg8), i.e. doesn't introduce any overhead, while new GSL leads to generation of additional runtime checks and branching instructions. I didn't find a way to eliminate these instructions for release builds. I'd like to return to GSL the configuration option which would allow its clients to eliminate all redundant checks from release builds if it is necessary (as it is done for example for assert(s)). This possibility existed before #831, and had been removed after it. |
This is very strange behaviour. As far as I can see there is no way to create a gsl::not_null with a nullpointer. So that means all accesses to |
As a developer in a low-latency context, I'm firmly on the side of zero-cost, and it's frustrating that I will have to copy not_null into my own namespace and maintain my own version. Especially since zero-cost is one of the stated goals of the Guidelines that the gsl is supporting. It looks like the zero-costness of not_null was also considered in #674 and #675. At the time it was decided that making it work in perfect safety with unique_ptr was more important than having zero-cost. I didn't follow all the details there, but if not_null<unique_ptr> is almost-but-not-quite-always-guaranteed-to-be-not-null, that seems like a small price to pay for making every other not_null zero-cost. Maybe the developers involved in those pull requests could comment? That's @annagrin and @neilmacintosh, I think. Adding back the GSL_UNENFORCED_ON_CONTRACT_VIOLATION macro would be nice. Or triggering off of NDEBUG would also work well for me. Or a not_null-specific macro. Or really any way in which my release builds don't have to check a pointer on every use. |
Hi, Most of this discussion has been about I work on a software project that has many responsibilities in a safety-critical system. I'd really like to introduce GSL to the project for its features like However, the current GSL behavior of the entire process terminating due to a single error in logic is unacceptable. The proposed revival of skipping checking for errors, so if an error occurs it may "spread" and incidentally be caught at a later time, is also unacceptable. Instead, errors need to be detected early and quarantined to the smallest subsystem that introduced the error, which can then be reported, safely disabled, and potentially repaired/restarted. Exceptions are a great tool for implementing such behavior. So if considering adding back |
@runer112 Thanks. Throwing contract violation handlers are a deeply discussed topic, and one key consideration is that if we allow throwing contract violation handlers then we could never put a contract on a |
@hsutter Thanks for the (quick!) response. I hadn't considered the interaction of contracts and I think that what I'm really looking for regarding not-null can be seen in To further draw on the parallel with This wide-contract approach to not-null checking may somewhat move it away from "compiler/language support library" and towards "general utility library." But if it fits closely enough (like Late edit: It just occurred to me that what started out seemingly closely related to the initial issue (contract violation configuration defines) has diverged. Should this be forked off into its own issue? |
We do not plan on returning |
Hi,
Some time ago, until the PR #831 was accepted, GSL had 3 configuration options:
For some unclear reason, in #831 these options were removed.
Removal of those options led us to the following not good consequences:
So, my proposal: let's return back the GSL_UNENFORCED_ON_CONTRACT_VIOLATION, or something else which will allow clients to make Expects(cond)/Ensures(cond) zero cost.
The text was updated successfully, but these errors were encountered: