Kweepa wrote: ↑Fri Oct 23, 2020 6:11 pm
Richard Russell wrote: ↑Thu Jul 02, 2020 9:44 am
Indeed BBC BASIC is particularly slow in one regard: the way that GOTO and GOSUB find their destination line by a linear search starting at the beginning of the program every time
Microsoft BASIC at least does the same thing (searching linearly that is -it does start from the current line if the destination is >= current). I would doubt any 8k BASIC works differently, given the minimal code size.
But I know from experience trying to read the names and values of variables that BBC BASIC definitely
does make a database of
PROCs and
FNs, which were seen as the "proper" way to write programs. (The dreaded
Bad program error means it can't make sense of a program when it tries to update that database.) Old-fashioned
GOTO and
GOSUB were really only provided for converting type-in listings from legacy systems. The expectation was for you eventually to replace
GOSUB with
PROC and infinite
GOTO loops with
REPEAT .....
UNTIL FALSE.
There are a few ways you could speed up GOTO/GOSUB a little bit compared to a linear search every time. You could enable two-way searching by storing another copy of the length of a line at the end of that line. Using BCD line numbers enables some optimisations to be made with respect to lines whose numbers are exact multiples of 100, but also restricts you to four digits. (It would work in decimal mode, too; but anyone who knows their 256 times table off by heart probably isn't going to be using much BASIC anyway .....) Or since BBC BASIC already tokenises line numbers referenced by
GOTO,
GOSUB and
RESTORE, you could build up a table of target line numbers and their addresses. Even on non-BBC-like systems, you could make a table of targets you have already searched for, which at least would make subsequent calls to the same line quicker. But all of these cost memory; an extra byte per program line for the two-way searching, three bytes per entry in a table of "round hundred" addresses, and who knows how much for tables of targets (four bytes for each
distinct target: two bytes for the line number, and two more for its address in memory). All in all, it probably would not be worth it for the minimal saving. You have to remember, the prevailing idea was that modern structured programming was the way forward, and
GOTO and
GOSUB were soon to be obsolete.
To be honest, the only time I really use
GOTO is as a poor person's substitute for multi-line
IF .....
THEN .....
ELSE .....
FI. Then the jumps are likely to be short, so Microsoft's optimisation of searching forward from the next program line rather than rewinding for every target might have been nice.