-
Notifications
You must be signed in to change notification settings - Fork 2.2k
utils: add hint to enable/disable object validity checks #13213
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
base: main
Are you sure you want to change the base?
Conversation
Okay, looks like ObjectValid is used not only to detect use-after-free and can't be just disabled everywhere. What's the actual purpose of it? |
The related code looked rather more efficient for SDL2, where it was just a nullptr and magic field check: Lines 50 to 67 in 2bc3ec4
With the introduction of I think it's great that you're trying to introduce an option to disable the checks. What issue did you actually run into, though? If it's just about the CI build error, that's only because you have left in some static functions which are now unused, causing compiler warnings, which are set to errors:
So just extend your patch to wrap also these functions in
In this case, I think these checks are even harmful! Because now instead of being able to rely on AddressSanitizer to detect issues in our code, we would need to litter our code with manual checks on the return values of all the SDL API calls, and even then we can only guess at what caused the actual issue if things fail. :-( |
Btw, did you consider disabling these checks based on a hint (see |
I don't really understand the purpose of ObjectValid. At first glance it's just optional validity check that can be disabled safely, but looking at e.g. |
Hmm, indeed, this Since you're mainly worried about the performance of |
Looked into it again and it seems like SDL_CleanupTrays is not required and is a protection in case user forgets to call
That's reasonable, thanks for the idea! |
In that case this sounds like another case of hand-holding which makes life harder for people who're checking their code with a memory leak detector. It may then be acceptable that this cleanup no longer works when object validity checks are disabled, though it does seem like something that should be mentioned in the documentation of the hint. |
if(!check_validity) { | ||
SDL_SetInitialized(&SDL_objects_init, true); | ||
return; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my personal opinion, it would be alright to always initialize the hash table, but just not put any objects in it. This would rather simplify the patch because:
- We'd need only a single check on
check_validity
in this function (the one below, which I think is better written as early-out rather than nesting the insert/remove code). - We would not need to check the value of
check_validity
inSDL_GetObjects
orSDL_SetObjectsInvalid
at all. These rarely used functions would automatically not do anything with an empty hash map.
The empty hash table should have basically zero overhead anyway.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's actually a massive overhead. It needs to lock and unlock hashtable's RWLock (which is like a half of the lookup time), calculate hash and do one iteration in find_first_item
which will break because of empty hashtable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems you're talking about SDL_ObjectValid
. I wasn't suggesting to remove the check_validity
condition from there.
I'm also not a fan of these validity checks. Imagine rendering 1000 textures of different colours every frame. That's 120,000 validity checks a second on the same renderer object that you already know is valid at the start of the app. I remember @slouken saying they already had something in mind to optionally disable this at some point. I'd like for it to be all hidden behind an #ifdef, or to only be included in the debug build. |
These checks seem to affect performance, particularly they take a huge part of SDL_RenderCopy time:
This perf report is taken in debug build, as in release everything gets optimized out and perf doesn't report anything meaningful. So I've also made a simple RenderCopy benchmark:
Source
image.png:
Ran on Ryzen 9 7940HS, Gentoo Linux, performance governor. There is a noticeable performance difference (and it becomes more noticeable when increasing number of draws in single frame).
I've added a hint
SDL_HINT_CHECK_OBJECT_VALIDITY
to enable/disable validity checks at runtime.P.S. Are these checks needed at all when there's AddressSanitizer to detect use-after-free efficiently?