Comment by tasty_freeze
Comment by tasty_freeze a day ago
ZX BASIC (as far as I know) was not a MS BASIC product, but many of the same techniques were used in MS BASIC. There were books describing the techniques that this blog post mentions, plus a lot more.
For instance, the symbol table is built as each variable is encountered, and it is not ordered. Thus, it was common for the first line of a BASIC program to be something like:
10 Y=X=B=Z=1
because Y, X, B, and Z were the most frequently accessed variables, so getting them at the start of the symbol table sped up future look-ups.Another thing: I'm pretty sure MS BASIC was smart enough to check if the GOTO or GOSUB line number was greater than the current line number, and if so, it would seek the target line from the current line, instead of the start of the program.
WANG 2200 BASIC-2 had an optimization for DEFFN statements. There was a small, fixed-size table, something like 16 entries, to hold a pointer to the first n DEFFN statements. Most programs used less than this limit. When an "FNX(...)" or whatever was encountered, it would search that DEFFN table, which then had a direct pointer to the line where the DEFFN was located. If there were more than 16 DEFFN statements, it would then have to do a linear search through the program text to find it.4
[EDIT] I forgot to mention, the article mentions that NEXT and RETURN also cared about line number placement. I haven't studied ZX BASIC so maybe it is different, but in MS BASIC and WANG BASIC there is a stack frame that contains a pointer to the head of the loop or the return point, respectively, for those two cases. Just storing a line number would be insufficient because the FOR body and the RETURN point might be in the middle of a line, eg
200 X=X+1:GOSUB 1000:FOR I=1 TO 20:T=T-B(X):NEXT I
GW-BASIC, and probably other MS BASICs, only looked up destination lines once, then replaced the GOTO code with a direct pointer to the destination. Something similar may have been used for other symbols, but I don't remember for sure?
Guess the ZX BASIC did not do that, or it would have improved the measurements seen in the article?
BLUE Book of GW-BASIC is full of details and tricks like that and some of it probably is useful for other BASICs as well.
https://github.com/robhagemans/hoard-of-gwbasic