How to? Speedup of gosub/return

Dec 10, 2014
63
1
#1
I was wondering why some of my scripts are so slow and discovered that each gosub/return call is absolutely crawling. Right now I'm copy/paste unrolling these, duplicating the code rather as the only way to speed it up I can think of. Are there any other smart ideas why this is so slow and how to fix it?
 

rconn

Administrator
Staff member
May 14, 2008
10,572
97
#2
Without a example of what you're seeing it's difficult to provide detailed workarounds.

A GOSUB call has to read the entire batch file line-by-line looking for the matching subroutine name. Naming your scripts as .BTMs will be a little faster because TCC doesn't have to read a disk file, though this isn't as significant as in the past given cached drives and SSDs. Depending on the size of your batch files, it might be faster to have your subroutines in separate files and have GOSUB call the files.
 
Dec 10, 2014
63
1
#3
A GOSUB call has to read the entire batch file line-by-line looking for the matching subroutine name.
Right, that would be the reason with my large batch file. So placing subroutines on the top of the btm will speed up things, too, as the search stops once a matching :sub_routine is found? If that doesn't help, I'll try the separate file approach.
 
Dec 10, 2014
63
1
#6
GOSUB starts looking for the subroutine label *after* the current line. If it doesn't find it by the end of the file, it starts at the beginning and goes to the current line.
Who would have known - thanks. Probably worth mentioning in the docs under GOSUB, as I just discovered how huge the difference of different sub positions is with large batch files.
 

rconn

Administrator
Staff member
May 14, 2008
10,572
97
#7
Who would have known - thanks. Probably worth mentioning in the docs under GOSUB, as I just discovered how huge the difference of different sub positions is with large batch files.
In the (existing) help for GOSUB:

"GOSUB begins its search for the label on the line of the batch file immediately after the GOSUB command. If the label is not found between the current position and the end of the file, GOSUB will restart the search at the beginning of the file. If the label still is not found, the batch file is terminated with the error message "Label not found"."
 
#9
At first you asked about other ways to speed up batch files. Considering that TCC or the OS must do a lot of reading (ultimately character by character) and a lot of hunting for matches through names of aliases, commands, and variables ...

1. empty the environment all you can
2. empty the alias list all you can
3. use short variable names (one-letter ones are the best)
4. use multiple commands on one line (&-separated) as much as you can

All that makes for ugly batch files, but I've tried it in Monte Carlo probability simulations and they make a big difference.

A question for Rex: When trying to match the name of a function or command what kind of search does TCC use ... linear ... binary?