SQ in silver chrome letters, a lab's gear stacked against the S: a rack, two oscilloscopes, pizza boxes and a six-pack. Strange Query Lab

CARDIAC › Tips and tricks › Room

Stream the table; don't store it

Say you need just one entry from a table of twelve.

The obvious way keeps the whole table in memory, twelve cells of it, and looks the entry up.

cell  contents  notes
 10      030    INP 30  cells 10-17: entry k of the table is in cell 39 + k
 11      130    CLA 30
 12      231    ADD 31
 13      614    STO 14
 14      100    CLA 00  CLA 39 + k, written by the cell before
 15      632    STO 32
 16      532    OUT 32
 17      900    HRS 00
 30      000
 31      139    CLA 39
 32      000
 40      031    cells 40-51: the whole table, in memory
 41      028
 42      031
 43      030
 44      031
 45      030
 46      031
 47      031
 48      030
 49      031
 50      030
 51      031

Step through the natural way in Cardigan

But if you only need one entry, why keep all twelve? Read the table as input cards, one at a time, and hold on to just the entry you want. The table takes no memory at all, only the time to read it. It works whenever you need a handful of entries. It won't work for a sort, which needs every entry at once.

cell  contents  notes
 10      030    INP 30  cells 10-19: read the table's cards one by one and keep only entry k
 11      031    INP 31
 12      130    CLA 30
 13      700    SUB 00
 14      630    STO 30
 15      700    SUB 00
 16      318    TAC 18
 17      811    JMP 11
 18      531    OUT 31
 19      900    HRS 00
 30      000
 31      000    the one card kept

Step through the CARDIAC way in Cardigan

Both print 28 for entry 2. Keeping the table takes 23 cells and runs 8 instructions; streaming it takes 12 cells and runs 16, plus the time to read the cards.

From the tips in cardiac-code on GitHub. All the tips.