stationeers.de Mod workshop · Dominik1199
DE Sign in
  1. Home
  2. IC10 academy
  3. Day 306

Day 306 of 365 · Expert · Month 11: Optimisation and debugging

Fast patterns

Three fast patterns: one batch instruction instead of a loop over devices, read readings once and keep them, and only write when something really changes.

Lesson 18 min Week 44

What you learn today

  • Batch instead of loops
  • Cache values
  • Avoid unnecessary writes

Yesterday you counted what a tick costs. Today it is about using less. Three patterns bring the most – and two of them make the code shorter at the same time.

Batch instead of loops

If twelve corridor lights should do the same thing, you need no loop over twelve names. Give them one name, and one sbn switches them all:

sbn LIGHT HASH("Flur") On dark     # all lights called Flur

One instruction instead of twelve passes of five steps each. A loop only pays off if each device should get something different – like the tank lights from the day before yesterday.

Caching values

Device values only change once per tick. Whoever reads the same value three times with lbn gets the same number three times and pays three steps. Read it once into a register and keep working there. That saves steps and makes sure all calculations of a tick work with the same value.

Avoiding unnecessary writes

Many chips write the same value to the same devices every tick. That costs steps – and with some LogicTypes more: command types like Harvest, Plant or Activate trigger their action on every write. Better: remember the last state in a register and only write on a change.

beq dark state start      # nothing changed: do not write
move state dark
sbn LIGHT HASH("Flur") On dark

state starts at −1, a value dark never takes. That guarantees one write on the first pass.

Warning

Whoever only writes on changes relies on the device keeping its value. If somebody rebuilds a light or switches it by hand, the chip learns nothing of it. For safety functions you would rather write every tick.

Tip

A counter of switching operations is a good probe: if it keeps rising, a condition is fluttering somewhere – for example a missing hysteresis.

Diagram

Three readings, five batch modes Sensor “Raum” 300 K Sensor “Raum” 310 K Sensor “Raum” 250 K lbn Average 286.67 Sum 860 Minimum 250 Maximum 310 Count 3 Sum on temperatures: 860 K – the heater thinks there is a fire.
One sbn reaches all lights with the same name – one instruction instead of a loop.

Exercise

Task

Control the corridor lights “Flur” by the daylight sensor “Aussen”:

  • dark when SolarIrradiance is below 100 or the sensor is missing
  • switch all lights with one batch instruction
  • only write when dark changes, and count every write on the display “Schaltungen”

Hints

Hint 1

sge dark dark THRESHOLD, then seqz – that turns NaN into “dark”.

Hint 2

beq dark state start skips everything if nothing has changed.

Hint 3

On a change: remember state, sbn, count up, display.

Quiz

3 questions. You pass from 60 % – the server does the grading.

Complete the day

  • Passed the simulator exercise
  • Passed the quiz with at least 60 %

Without an account progress is saved in this browser only.

Report a mistake in this lesson

Please sign in to report. Sign in

Esc
↑ ↓ selectEnter open/ opens search anywhere