Repository navigation
Conversation
|
Did I pick the wrong names all along? Should these just be OIIO_CONTRACT_ASSERT_FAST, etc? (Get rid of the HARDENING nomenclature to make it easier to remember?) We could do a "soft rename" with alias macros and get rid of the old names later. |
Signed-off-by: Larry Gritz <lg@larrygritz.com>
|
After some thought, I now believe that these are named wrong, so I'm including the following change (implemented as a second commit, so very easy to revert if you think I'm making a mistake). New take: The assertions all use the "CONTRACT" nomenclature: OIIO_CONTRACT_ASSERT (unconditional), OIIO_CONTRACT_ASSERT_FAST (if at least "fast" hardening level is selected at build time, which is the default), OIIO_CONTRACT_ASSERT_EXTENSIVE (enabled if "extensive" hardening selected at built time), OIIO_CONTRACT_ASSERT_DEBUG (enabled only if "debug" hardening is selected. The "HARDENING" nomenclature is used for the build-time CMake variables, allowing the builder to choose which throttling level of contract assertions to enable. I think that's more clear. We never used the old names! So nothing to update in the code base other than dassert.h itself. But I added |
Less confusing Signed-off-by: Larry Gritz <lg@larrygritz.com>
| // - OIIO_CONTRACT_ASSERT_DEBUG : only checks contract for DEBUG hardening. | ||
| #if OIIO_HARDENING_DEFAULT >= OIIO_HARDENING_FAST | ||
| # define OIIO_HARDENING_ASSERT_FAST_MSG(condition, message) \ | ||
| # define OIIO_CONTRACT_ASSERT_FAST_MSG(condition, message) \ |
There was a problem hiding this comment.
The new name is waaaay better. You nailed my point of confusion.
| // default is 2 = EXTENSIVE (and may also be set this way for releases in more | ||
| // security-sensitive environments). | ||
| // | ||
| // *Usage hints* |
There was a problem hiding this comment.
Mmm, I'm worried these hints are a bit too tailored to the span case, when in fact the assertions can be used in many more circumstances.
For example, span checks (size and type checks) are usually cheap O(1) operations, so the hints below make sense for them. But in general, the check itself could be expensive, e.g. O(n) when it scans an array, and so on... and.. and... maybe I'm overthinking this...
There was a problem hiding this comment.
I'm not sure which way you're arguing here.
The check in span::operator[] is cheap individually, but can slow things down if done on every iteration (among other things, it's almost certain to prevent the compiler from vectorizing the loop in circumstances when it might otherwise have done so.
The correct solution for arrays and spans, though, is to user range-for or iterators -- going from begin() to end() is by definition in range and doesn't require a check. If you're visiting every element, you should iterate or use ranges. Use operator[] when you truly need arbitrary order numeric indexing. This contrast used to be purely stylistic (and in fact, I preferred iterating the C way, it seemed more straightforward), but as both we and the C++ community are moving toward [] being range checked to disallow out of bounds accesses -- which I think is a necessary thing now -- it is a performance issue.
Anyway, I digress. I'll try to tighten up the language a bit.
| // validate input or runtime conditions. Failing a contract assertion will | ||
| // generally terminate the program. | ||
| // | ||
| // *Contract assertions* |
There was a problem hiding this comment.
How about we make this section more compact? The comments at each macro's definition already contain detailed descriptions, so people can go read those if they need more info. In the summary, though, I think we should give a big-picture view at a glance and only call out the points that are easy to confuse.
There was a problem hiding this comment.
I think the whole "Contract assertions" can turn into:
// OIIO_CONTRACT_ASSERT_FAST(cond) low performance impact
// OIIO_CONTRACT_ASSERT_EXTENSIVE(cond) may impact performance
// OIIO_CONTRACT_ASSERT_DEBUG(cond) maximum checks, for debugging
// OIIO_CONTRACT_ASSERT(cond) not gated by hardening level
//
// The hardening level (OIIO_HARDENING_DEFAULT) picks which checks are
// compiled in. OIIO's own build uses the CMake OIIO_HARDENING setting
// (default 1 = FAST for Release, 2 = EXTENSIVE for Debug)
//
// The response (OIIO_ASSERTION_RESPONSE_DEFAULT) picks what a failed check
// does: IGNORE, OBSERVE (print), ENFORCE (print and abort), or
// QUICK_ENFORCE (abort). The default is ENFORCE, or IGNORE if the hardening
// level is 0 and NDEBUG is defined.| # ifdef NDEBUG | ||
| # define OIIO_HARDENING_DEFAULT OIIO_HARDENING_FAST | ||
| # else | ||
| # define OIIO_HARDENING_DEFAULT OIIO_HARDENING_DEBUG |
There was a problem hiding this comment.
I just noticed in the new summary section, you mention default for debug build is EXTENSIVE, but here, it's set to DEBUG.
There was a problem hiding this comment.
Oof, we have a contradiction.
This header, in the absence of a definition, defaults to DEBUG if NDEBUG is not defined (non-release builds), FAST otherwise.
But our CMake files (compiler.cmake) explicitly sets it to EXTENSIVE for Debug builds, FAST otherwise.
Presumably, the times that the header is encountered without the cmake file setting it is in downstream code.
So effectively this means that release builds (ours and theirs) use FAST, the Debug build of OIIO uses DEBUG level internally, but debug builds of downstream software that consumes OIIO headers uses EXTENSIVE.
I can't really say if that was intentional, but it kind of has its own logic -- EXTENSIVE is kind of a debug level of vigilance, but DEBUG is for debugging OIIO itself?
That may be one degree too clever for its own good. Should we settle on it being DEBUG everywhere for Debug builds? Which kind of means that EXTENSIVE is specifically for Release builds where people want more hardening and are willing to trade performance to get it.
Signed-off-by: Larry Gritz <lg@larrygritz.com>
|
I pushed an update that fixes half your comments -- the typos, and clarifying that OIIO_STATIC_ASSERT is deprecated. Still need to resolve the ambiguity that you identified: is a debug build EXTENSIVE or DEBUG, or should it be the same or different inside or outside the library (versus downstream code). I'm leaning toward "consistent and DEBUG for debug builds" for simplicity, but await hearing your opinion. |
No description provided.