I would like to optimize the dml-code I have for speed in a production environment. As far as I understand log statements such as:
log info, <n>: <string>;
Get translated in at least one if statement in the executable, because there is a need to check whether the log-level is larger than n.
I would like to avoid that by throwing the if statement out of the executable similar to #define and #ifdef statements in C and the only way I found in dml is by defining a constant and checking it and hoping the compiler will optimize out the code, recognizing it will never be executed.
constant LOG = 1; // there is not even the possibility to define a bool constant
if (LOG) log info, <n>: <string>;
My questions are:
- Is there a better (more efficient, more elegant) way to implement that?
- Will the dml-compiler or the C-compiler optimize the code out?
- How can I verify the code is no longer in the executable (or in the intermediate C source) ?
Thanks in advance.
My first reaction is that you should reconsider your ambition to do optimizations on this level. Do you have benchmarks that prove this code to be a bottleneck? The devices of a system seldom account for more than 5-10% of the total simulation time, and account for a vast majority of the development effort, so your device development effort is better spent on maintainability. There are definitely cases where devices account for more than that; in this case you need to understand why. It's often caused by a busy-wait loop, and there are techniques like hypersimulation that can be used to fast-forward simulation over busy-wait loops.
Also, the
ifgenerated by alogstatement is an easy-to-predict branch instruction, so you will have a hard time even measuring the speed of it.However, you may have other valid reasons for making sure a debug log is left out from the binary, e.g. concerns about code size or confidentiality. If you want to ensure that a statement stays outside the compiled result, then use
#ifinstead ofif. This ensures the body is expanded in compile-time; the discarded branch is even allowed to reference nonexistent identifiers.To summarize, my answers to your three questions are:
#ifmay be a better choice#ifgives you a hard guarantee that the code will be optimized out by DMLC; if you useifthen it will certainly be eliminated by the C compiler if you compile with optimization flags, but there is no formal guarantee.<host>/obj/modules/MODULE-NAME/DEVNAME-dml.c.And a final note: The recommended construct for constants in DML 1.4 is
param; you can writeparam LOG = true;.constantis a remnant of DML 1.2, which remains only to bridge a current compiler limitation (params cannot be used when specifying array sizes intypedefs).