NXC: code optimization - what and how?

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

NXC: code optimization - what and how?

Post 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 ?
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: NXC: code optimization - what and how?

Post 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.
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
HaWe
Posts: 2500
Joined: 04 Nov 2014, 19:00

Re: NXC: code optimization - what and how?

Post by HaWe »

yes, I suspect this, but I mostly use level 2, just out of habit.
alex-kolotov
Posts: 8
Joined: 29 Sep 2010, 07:00

Re: NXC: code optimization - what and how?

Post 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.
spillerrec
Posts: 358
Joined: 01 Oct 2010, 06:37
Location: Denmark
Contact:

Re: NXC: code optimization - what and how?

Post 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.
My blog: http://spillerrec.dk/category/lego/
RICcreator, an alternative to nxtRICeditV2: http://riccreator.sourceforge.net/
Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests