# Costs: measured server work
Full price table: CPU budget, comparisons, maps, lists, text, functions, loops, processes, output, displays, building / switching maps. Open when code runs every tick, on many objects or with big lists.

## Budget and prices
- CPU budget (measured, dev/research/rerun-bench.md): give it in % of `value::cpu_usage` and read it on the spot (not
  in an every-tick loop). The ms → % factor changed 1.8x between sessions (1% = 68–125 µs of code per tick), so keep
  heavy code ≤ ~3 ms per tick and peaks ≤ 80%. ~100% for a moment is fine, sustained >100% crashes the world (high_cpu).
- Measured cost of code (dev/research/ops-bench.md, structure-bench.md; one simple action such as `a = i` = 1 ≈ 0.7 µs,
  a loop iteration adds ≈0.5):
  - Comparisons: `>` `<` ≈1 (`>=` `<=` compile to the same one-number actions) and `variable::number_in_range(x, a, a,
    "INCLUDE_ALL")` (numeric equality) ≈1.5, but `==` / `!=` ≈8 (they compile to equals / not_equals with a list) — in
    hot loops compare numbers without them. With `-O` (`jmvcc client deploy -O`) the compiler does it itself:
    `x == 5` / `x != 5` → number_in_range when x only ever holds numbers (loop counters, `+=`, `%math`, number
    constants and game values; not save var, not a var that ever gets a text) — verified, server-check-23/23b.
    `variable::is_type` ≈7 and an empty `code::catch_error(e) { }` ≈0.5 (server-check-34).
    `k == a || k == b || ...` ≈14 per term → `variable::equals(k, [..], compare_type = "ANY")` ≈11, `map_has_key` ≈9,
    or a range check.
  - Maps: `m[k]` ≈1.2 and `m[k] = v` ≈1.8 (they compile to get_map_value_single / put_map_value); the list-key
    forms `m.get_map_value(k)` ≈6 and `m.set_map_value(k, v)` ≈11 — only for several keys at once. Lists: `l[i]` ≈1, `l[i] = v` ≈1.5 (list size does not matter), `list_append_value` ≈7.5 (add
    several values in one `append([a, b])`), `get_list_index_of_value` ≈2.6 (check `> -1`) vs `list_contains_value`
    ≈9, sorting 50 numbers ≈67. Text `"$a$b"` ≈2 vs `variable::set_text([..])` ≈7; `%math` expression ≈3; `+=` ≈1;
    `variable::absolute` / `variable::pow` ≈1. line / game / save var cost the same.
  - One action with plain number/text arguments beats `%math` and hand-written chains: `variable::clamp` 1.2 vs 2.9–3.4,
    `lerp_number` 1.3 vs 3.1, `repeat_text` 1.4 vs 2.6, `format_timestamp` 5.8 vs 12.8, `location_in_range` 5.3 vs 7.9.
    Actions that take LISTS are dearer than the hand form: `get_list_value_multiple` 8.2 vs 2.0 (its indexes start at 0),
    `variable::multiple` 11.3 vs 2.9 (separate `=`), `map_value_equals` 8.5 vs 4.7 (`m[k] == v`).
  - Reading a list out of a map or variable copies it (≈0.25 µs per element): do not pull big lists every tick.
  - Edition 2 calls: ≈2.3 + ≈1 per parameter (an edition 1 call with parameters and return ≈12); a list parameter is
    not copied (its length for ≈4.7 at 100 and at 1000 elements; edition 1 copies it there and back, 9–76× dearer).
  - Functions: a call ≈2 (also `code::call_function(name)`), arguments + return ≈ +7 more — in loops of hundreds per
    tick inline the code or call a parameterless function over the shared vars. `start_process` ≈4 and runs the
    process at once, inside the caller's tick, up to its first wait. Loops: multi_times / on_range / for_each ≈0.5
    per iteration, `repeat::while` with a counter ≈1.8. Dispatch by key: a list (`LV[k]`,
    `code::call_function(LF[k])`) or `M[k]`, never an `==` if/else chain (10 branches ≈46 per key).
  - Many objects: a process per object with `wait(1)` ≈5.5 µs per object per tick; one loop that keeps object state
    in lists is only ≈20% cheaper, so up to ~50 objects pick what is convenient. A sleeping process is free, each
    wake-up ≈6 µs: use wait(5) / wait(20) where every tick is not needed.
  - Output with one player: a particle ≈17, action bar ≈8, sound / title ≈3 (network cost is invisible to cpu_usage).
  - `world::set_block` without update_blocks is ≈2.5x dearer on real changes than with `update_blocks = true` — pass
    it for blocks you change often (it is also what makes changes from a world loop visible).
- Display entities (measured, dev/research/entities-bench.md): spawn ≈40 µs (40 per tick all spawn, no tick wait);
  remove as a batch ≈5.5 µs each — `select::entity_by_name(uuid_list)` + `entity::remove()` + `select::reset()` (one by
  one costs 3x). Changing one display (select [u] + action + reset ≈3 µs): block / item / scale / translation /
  rotation / teleport ≈4–6 µs, one text line ≈12, 3 colored lines ≈31, `set_display_transformation_matrix` ≈34 —
  prefer scale / translation / rotation. Every tick: 64 text changes ≈ +11% cpu, spawn + remove 20 ≈ +13%. Pixel
  screen: group pixels by color — `set_block(color, list, update_blocks = true)` and select(uuid list) +
  `set_block_display_block(color)` cost the same, per pixel 2.5–3.5x more. The block of a block_display cannot be read.
- Building / switching maps (measured, dev/research/map-build-bench.md, map-size-bench.md, air-bug-bench.md):
  - Keep templates in the SAME chunks as the arena (straight above or below it: same x,z, other y). A clone into
    other chunks costs +≈5 ms per call whatever its size, even 1×1×1 (≈ +40% cpu if done every 2 ticks).
  - Up to ~150 changed blocks: set only the DIFFERENCE (incl. air) with `world::set_block(block, list_of_locations,
    update_blocks = true)`, one call per block type (≈2 µs per block). More than ~1000 (or ≳20% of the zone):
    `world::clone_region(src1, src2, src1, dst_corner, ...)` from a template (≈0.5 ms up to 1500 cells, ≈1.5–2 ms for
    8192). In between it is about a tie.
  - At most 3000 locations per `set_block(..., update_blocks = true)` call — the rest is silently dropped. Split longer
    lists into several calls (several in one tick work).
  - A clone with air (`ignore_air = false`) skips the template's air until its first non-air block (walk: template
    chunks from the min corner, inside a chunk y → z → x) and leaves old blocks there. Fix: an ANCHOR — any non-air
    block (not cave_air; barrier is invisible) at the template's min corner (min x, y, z); or `clear_region` + clone
    with `ignore_air = true`, but then every chunk column of the template zone needs at least one block, or the clone
    costs +≈5 ms. An empty template erases nothing — use `clear_region`.
  - cpu: a clone of 8192 cells every tick ≈25%, every 2 ticks ≈13%; a difference ≈2–3% per 100 blocks per tick. Never
    check blocks before setting (a check costs more than setting). PER-TICK LIMIT: ~20 calls of set_region /
    clear_region / clone_region / set_block(update_blocks = false) per tick, more makes the thread wait a tick; dozens
    of clone_region in one tick stalled the server for 13 s and crashed the world code (high_cpu) — keep clones to 1–2
    per tick. set_block with update_blocks = true is not under that limit.
  - `world::get_block_material(p, "ID")` returns the id without `minecraft:` (`"air"`); `"ID_WITH_DATA"` includes it.

## Comparisons (also in topic server-facts)
- `==` / `!=` are the expensive conditions (≈8 actions: they compile to equals / not_equals with a list); `>` `<` cost ≈1
  (measured, dev/research/ops-bench.md; `>=` `<=` compile the same way). `x == 1 || x == 2 || x == 3` costs 3 equals — write
  `variable::equals(x, [1, 2, 3], compare_type = "ANY")` (one) on hot paths.
