Page 1 of 1

NXC: code optimization - what and how?

Posted: 14 Sep 2012, 12:26
by HaWe
hi,
is there a documentation about what code optimization is doing at any level?
How exactly is code optimized at which level?
is it possible (in doubt) to protect variables from being optimized away like by the keyword volatile ?

Re: NXC: code optimization - what and how?

Posted: 14 Sep 2012, 15:37
by spillerrec
AFAIK:
No documentation exists.
Volatile has been requested once in the wishlist topic. For now you can only avoid unused variables to be removed by using it somehow without affecting your program (if you don't want to change the optimization level).
Level 0 does nothing. Level 1 removes unused functions and perhaps does other stuff too.

Re: NXC: code optimization - what and how?

Posted: 14 Sep 2012, 19:36
by HaWe
yes, I suspect this, but I mostly use level 2, just out of habit.

Re: NXC: code optimization - what and how?

Posted: 16 Sep 2012, 09:39
by alex-kolotov
spillerrec wrote:Level 0 does nothing. Level 1 removes unused functions and perhaps does other stuff too.
Can you clarify what functions you are talking about? Is it user's functions declared but not used or system functions?

On the latest test_release (Sept, 9th) I see the following behavior:

Quite simple program:

Code: Select all

task main() {
    SetSensorLowspeed(IN_4);
    Wait(500);
    int tmp;
    long strt = CurrentTick();
    for(int i=0;i <1000;i++) {
        tmp = SensorUS(IN_4);
    }
    NumOut(12,LCD_LINE4, CurrentTick()-strt);
    until(ButtonPressed(BTNCENTER, false) > 0);
}
takes more that 20Kb on NXT:
nxc_002_usp.rxe - 21024

It is for optimization level "0". But I recall that it was not so in the past - such kind of programs were pretty small. Of course, if I enable level 2 of optimization the program takes just 954 bytes.

Re: NXC: code optimization - what and how?

Posted: 16 Sep 2012, 20:12
by spillerrec
It is both user and system functions and most likely variables as well. At some point of time level 0 started including all system functions, I don't know why. Level 0 might have worked differently back then.