-
Notifications
You must be signed in to change notification settings - Fork 74
[GEN][ZH] Change TheGlobalData macro to variable in debug mode for debugging convenience #1054
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
static union | ||
{ | ||
GlobalData* TheWritableGlobalData; ///< The global data singleton | ||
const GlobalData* TheGlobalData; ///< Const shorthand for above singleton |
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.
If this works in Debug, then can we have this in Release too?
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.
Frankly, I'm not a great fan of this solution even though it seems quite neat. C++ has quite strict rules about unions:
It is undefined behavior to read from the member of the union that wasn't most recently written. Many compilers implement, as a non-standard language extension, the ability to read inactive members of a union.
https://www.cppreference.com/w/cpp/language/union.html
Now maybe in our case there are mitigating circumstances considering that both pointers are of the same type and only differ in const-ness, but I don't know. The idea was that the debugging convenience might outweigh the potential for UB, but I wouldn't want to do it this way in release mode.
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.
FWIW, my first commit is a setup with a second pointer like you suggested initially.
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.
Hmm unfortunate. Can we put
extern GlobalData* TheWritableGlobalData;
extern const GlobalData* TheGlobalData;
on the same cache line somehow?
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.
I think putting their declarations right under each other, as they are, is the best we can do in that regard.
What's the relevance of cache lines here?
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.
How about we replace TheWritableGlobalData with TheGlobalData and make it writeable. The whole const thing is stupid, let's not make it even more stupid.
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.
Yes sure. We can also protect against accidental writes by writing expression like so:
if (0 == TheGlobalData->m_value)
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.
Yoda notation isn't great for readability imo and modern compilers can detect and warn for accidental assignments in if statements.
I think there's value in applying const as much as possible and doubly so when accessing a singleton, so I wouldn't be in favor of removing it entirely. I'm on the fence with regards to the change to a template. One fewer macro is a good thing, having to change 800+ cases of TheWritableGlobalData
less so. I'd probably just stick to the first commit implementation.
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.
Never heard it called Yoda notation before :)
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.
I'd probably just stick to the first commit implementation.
It is mostly useless though. I almost always test in Release.
Btw, the recent commandline PR changed some things around GlobalData. I suggest updating before continuing on this. |
The value of a macro cannot be inspected with the VS22 debugger: https://learn.microsoft.com/en-us/visualstudio/debugger/expressions-in-the-debugger
This PR makes it more convenient to see the values of the
TheGlobalData
macro when_DEBUG
,RTS_DEBUG
orRTS_INTERNAL
is defined.The second pointer should not be used in release mode because of the potential for missed optimizations.(not relevant for the version withstatic union
)