I recently studied open source code for my project and came across the following code.
internal ContentTypeReader[] LoadAssetReaders(ContentReader reader)
{
#pragma warning disable 0219, 0649
// Trick to prevent the linker removing the code, but not actually execute the code
if (falseflag)
{
// Dummy variables required for it to work on iDevices ** DO NOT DELETE **
// This forces the classes not to be optimized out when deploying to iDevices
var hByteReader = new ByteReader();
var hSByteReader = new SByteReader();
var hDateTimeReader = new DateTimeReader();
...
what happens if this is not done, does the compiler break the code ?
Can someone explain this more clearly
@Jan-Fokke the comment above the if
and the one inside explains why this is needed. The breakdown the #pragma warning disable 0219, 0649
will suppress two specific warnings:
CS0219 is generated when a variable is declared but never used. You can try this in your own code easily by creating a variable that isn't used anywhere. In some systems warnings break builds and if you can't get away from a specific warning you can choose to suppress it.
CS0649 I am not sure why is being triggered (could be from a previous iteration) The warning would be: The compiler detected an uninitialized private or internal field declaration that is never assigned a value.
So what the second comment is saying is that if the compiler doesn't find any instances of these classes it will get rid of them on iDevices. The compiler optimization strategies are usually very complex and usually when you see code like this - it means the team behind the codebase struggle with this issue and ended up with this work around.
Take a look at the following links for more on compiler optimization and DCE [Dead Code Elimination]
https://en.wikipedia.org/wiki/Dead_code_elimination https://www.mono-project.com/docs/about-mono/languages/csharp/#cil-optimizations
The following is from Incredibuild faq if you want to search further:
Q: I'm using a compiler/linker different from MSVC's for better code optimization. I still develop and build my project using Visual Studio. Will IncrediBuild support the compiler I'm using?
A: IncrediBuild currently supports Microsoft's cl.exe (all Visual Studio, eVC, Xbox and Xbox 360 platforms) and Intel C++ Compiler. Additional compilers may be supported in the future. The XGE Interfaces solution can be used with a variety of build tools and scripts to run compilers that are not currently supported.