Old 03-26-2022, 08:25 AM   #1
sai'ke
Human being with feelings
 
sai'ke's Avatar
 
Join Date: Aug 2009
Location: NL
Posts: 1,263
Default JSFX: Switch statement.

I noticed that a fair amount of time in some of my more complicated plugins that have a lot of selectable options is being spent in linear chains of if-statements.

Since we don't really have dynamic function calls (like function pointers or something similar), I kind of rely on this pattern a lot.

I noticed that converting the following linear chain
Code:
(foo == 1) ? (
) : (foo == 2) ? (
) : (foo == 3) ? (
) : (foo == N) ? (
);
into a hierarchical one conferred a large performance boost:

Code:
(foo < 25) ? (
  (foo == 1) ? (
  ) : (foo == 2) ? (
  ) : (foo == 24) ? (
  )
) : (
  (foo == 25) ? (
  ) : (foo == 25) ? (
  ) : (foo == 26) ? (
  ) : (foo == N) ? (
  )
);
This made me wonder whether it'd be possible to have something like a switch statement in JSFX that just acts like a jump table to the active code.

I do realize that I'm probably using JSFX a bit beyond the scope of what it was really intended for though, so it's cool if this is out of scope. Just thought that if it was easy to do it'd be cool. If not, the hierarchical if-statements work well enough
__________________
[Tracker Plugin: Thread|Github|Reapack] | [Routing Plugin: Thread|Reapack] | [More JSFX: Thread|Descriptions|Reapack]
sai'ke is online now   Reply With Quote
Old 03-27-2022, 03:40 PM   #2
mschnell
Human being with feelings
 
mschnell's Avatar
 
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 11,634
Default

Yeah ! C++ would be really nice !
ROFL
mschnell is offline   Reply With Quote
Old 03-28-2022, 12:39 PM   #3
Justin
Administrator
 
Justin's Avatar
 
Join Date: Jan 2005
Location: NYC
Posts: 14,185
Default

Hmm, it would probably be possible to add to EEL2 a keyword with usage of something like:
Code:
switch(x, 
  0: code,
  1: code,
  2: code,
 [ default_code]);
the caveat would be that the 0/1/2 values would need to be constant values. Probably would be most useful if x was rounded to the nearest integer, too?
Justin is online now   Reply With Quote
Old 03-28-2022, 01:47 PM   #4
souk21
Human being with feelings
 
souk21's Avatar
 
Join Date: Mar 2021
Posts: 128
Default

I like the idea !
Code:
switch(x,
  0: (
    //Some
    //Multi
    //Line
    //Code
  ),
  1: (
    //Some
    //Multi
    //Line
    //Code
  ),
  (
    //Some
    //Multi
    //Line
    //Code
  )
);
vs
Code:
x == 0 ? (
  //Some
  //Multi
  //Line
  //Code
) : x == 1 ? (
  //Some
  //Multi
  //Line
  //Code
) : (
  //Some
  //Multi
  //Line
  //Code
);
The switch is a bit more readable to me.
Would that come with the performance improvement described by sai'ke ?
souk21 is offline   Reply With Quote
Old 03-28-2022, 03:19 PM   #5
sai'ke
Human being with feelings
 
sai'ke's Avatar
 
Join Date: Aug 2009
Location: NL
Posts: 1,263
Default

Quote:
Originally Posted by Justin View Post
Hmm, it would probably be possible to add to EEL2 a keyword with usage of something like:
Code:
switch(x, 
  0: code,
  1: code,
  2: code,
 [ default_code]);
the caveat would be that the 0/1/2 values would need to be constant values. Probably would be most useful if x was rounded to the nearest integer, too?
Sounds great to me! Would the code blocks in that case be allowed to be multi-line if enclosed by parentheses (the way souk21 suggests), or would they have to be single lines?

And yeah, I was assuming that the cases would have to be fixed, but they would be for the typical algorithm selection use case.

And yes on the rounding. Though I'm unsure whether nearest or truncation is more desirable. From other languages, I'd usually expect the latter, but I think you should probably pick what you think is most in line with what is typical in JSFX. For the use cases I have in mind it wouldn't make a difference which one you'd pick I think.
__________________
[Tracker Plugin: Thread|Github|Reapack] | [Routing Plugin: Thread|Reapack] | [More JSFX: Thread|Descriptions|Reapack]

Last edited by sai'ke; 03-28-2022 at 03:25 PM.
sai'ke is online now   Reply With Quote
Old 05-13-2022, 07:25 PM   #6
juan_r
Human being with feelings
 
juan_r's Avatar
 
Join Date: Oct 2019
Posts: 554
Default Return and break too

I'm having trouble writing a MIDI JSFX because I'm missing break or return--a way to exit a loop prematurely. Or a continue statement to cut it short if it's substantial in size.

So here's a similar request I just made for these statements: https://forum.cockos.com/showthread.php?t=266629
juan_r is offline   Reply With Quote
Old 05-14-2022, 01:35 AM   #7
mschnell
Human being with feelings
 
mschnell's Avatar
 
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 11,634
Default

In case you did not already find a solution: a (additional) variable needs to be checked in the loop statement to allow for "break" by setting that variable within the loop.
-Michael
mschnell is offline   Reply With Quote
Old 05-14-2022, 04:30 AM   #8
juan_r
Human being with feelings
 
juan_r's Avatar
 
Join Date: Oct 2019
Posts: 554
Default

Of course, but you either place a check every other instruction, or wait for the current iteration to end (if checking, say, in the head of the loop).
juan_r is offline   Reply With Quote
Old 05-14-2022, 05:03 AM   #9
mschnell
Human being with feelings
 
mschnell's Avatar
 
Join Date: Jun 2013
Location: Krefeld, Germany
Posts: 11,634
Default

Yep. In certain cases you additionally need to "?" check that variable to jump over the rest of the loop.

A "break" instruction might be slightly faster in certain cases.
-Michael
mschnell is offline   Reply With Quote
Old 05-14-2022, 06:33 AM   #10
juan_r
Human being with feelings
 
juan_r's Avatar
 
Join Date: Oct 2019
Posts: 554
Default

Quote:
Originally Posted by mschnell View Post
Yep. In certain cases you additionally need to "?" check that variable to jump over the rest of the loop.

A "break" instruction might be slightly faster in certain cases.
-Michael
Apart from efficiency (that an optimizing compiler could at least partially re-gain), the matter is that of ease of coding - legibility and such. An additional '?' yields one more level of nesting and a whole bunch of hairy "dangling else" dangers. I mean: skipping the rest of the current iteration can't be done without more nesting since we have no GOTO (and rightly so, IMHO).
juan_r is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 04:29 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2022, vBulletin Solutions Inc.