Page 1 of 1

Nested If constructs

Posted: 27 Feb 2020, 09:43
by dynamicdave
This may seem a silly question.
I've just got this rule-set working to turn an individual LED on depending on the numerical value in an 'event' sent via MQTT (from Node-RED to a Wemos D1 Mini).
I'm surprised it works as I thought that ESP Easy didn't support nested IFs.

I'm running mega-20200222 on the Wemos.

Regards, David

Code: Select all

On ledcolour_code Do
TaskValueSet,1,1,%eventvalue%         // Store %eventvalue% in dummy variable ledcolour#code

if [ledcolour#code]=0                 // Clear all LEDs
    gpio,15,0
    gpio,13,0
    gpio,12,0
else 
    if [ledcolour#code]=1             // Turn on RED LED
        gpio,15,1
        gpio,13,0
        gpio,12,0
    else 
        if [ledcolour#code]=2         // Turn on GREEN LED
            gpio,15,0
            gpio,13,1
            gpio,12,0
        else 
            if [ledcolour#code]=3     // Turn on BLUE LED
                gpio,15,0
                gpio,13,0
                gpio,12,1
            endif
        endif
    endif
endif

endon

Re: Nested If constructs

Posted: 27 Feb 2020, 09:57
by grovkillen
I think it's parsing it just as it would parse this:

Code: Select all

On ledcolour_code Do
TaskValueSet,1,1,%eventvalue%         // Store %eventvalue% in dummy variable ledcolour#code

if [ledcolour#code]=0                 // Clear all LEDs
       gpio,15,0
       gpio,13,0
       gpio,12,0
endif
if [ledcolour#code]=1             // Turn on RED LED
        gpio,15,1
        gpio,13,0
        gpio,12,0
endif
if [ledcolour#code]=2         // Turn on GREEN LED
       gpio,15,0
       gpio,13,1
       gpio,12,0
endif
if [ledcolour#code]=3     // Turn on BLUE LED
       gpio,15,0
       gpio,13,0
       gpio,12,1
endif
endon

Re: Nested If constructs

Posted: 27 Feb 2020, 10:23
by dynamicdave
Thanks.

Is there any speed/performance issues with the way I've implemented it?

Note:
I did code it your way at the start, then thought I could improve the layout/readability.

Thanks again for your swift response.

Re: Nested If constructs

Posted: 27 Feb 2020, 12:33
by dampa
If they parse the same way, there won't be any difference. If they do parse differently you, in the nested case if the ledcolour#code was 0 you would only evaluate one if statement while in the non nested case all four if will always be evaluated. however, if ledcolour#code = 3 the in both cases all four if statements will be evaluated.

So (if they pares differently) on average the nested if's will evaluate 2 if statements while the non-nested if's will alyays evaluate 4 statements.

If won't make a differenct unless you were pushing thru a lot...a very, very lot of data thru that rule

Re: Nested If constructs

Posted: 28 Feb 2020, 16:04
by TD-er
Well there is a slight difference.
"else if" is one level deeper. So you may run into the max. depth in some nested situations.
Not sure what it is, but the parser does have a max. depth.

Also there is a very slight difference in parsing more lines, but I guess having less spaces until the comment slashes is probably more likely to be a speed improvement :)

Re: Nested If constructs

Posted: 28 Feb 2020, 20:07
by dynamicdave
I think I read somewhere the nested level depth was three(3).
(Which by coincidence is what my code has.)

So are you saying that I should remove all the comments?

I only put them in the code to make it obvious as to what was happening.

Thanks for your valuable feedback

Re: Nested If constructs

Posted: 28 Feb 2020, 21:50
by grovkillen
All rows are being parsed and since the comments are preceded by a lot of blanks it must parse through it all until it finds the dual slashes and realize it's a comment. So if you want to optimized the rules you should avoid those spaces and comments. But in reality the win isn't noticed I reckon.

Re: Nested If constructs

Posted: 29 Feb 2020, 16:42
by TD-er
Each byte is parsed as jimmy noted.
So in theory parsing comments does take time, but I doubt it will be noticeable.
If for whatever reason you may run into issues with saving the rules file (2048 bytes max. and some report practical limit of 1500 or slightly more before the rules may get corrupted at saving)

So the only reason to remove the comments is when you run into rules size issues.