code wiki / (root) / nx_perf_lib.nx

nx_perf_lib.nx

buildroot/runtime/nx_perf_lib.nx

10491 B209 linesdepth 1pulls 1 transitivereach 146 importersview sourcekind librarytopic perf
docsdependenciesstructsconstsfunctions

about

nx_perf_lib.nx -- FRAME-BUDGET TELEMETRY, the AAA way, as ONE sovereign ruler (GE32 + GE15, 2026-09-02). Operator: "evaluate at minimum the way AAA teams do -- every frame is a budget that cannot be overspent; frame time and frametime CONSISTENCY, spikes, p50/p95/p99". The estate measured all of that in page JavaScript (?perf=1: p50/p95/p99/jank/worst) and NOTHING read it (gameengine GE15). This lib moves the arithmetic into NishiLang so the ENGINE (wasm), the served HUD and the pacing referee gate all compute the same numbers from the same window -- one ruler, three consumers -- and the page is left with a door call and a display line, which is the interop-only law. WASM-COMPATIBLE BY CONSTRUCTION: no allocation, no syscalls, no floating point. The caller hands in a region of PF_WORDS i64 (an engine arena offset, or a native mmap) and every function is a pure operation over it. Integer milliseconds throughout; the page rounds the rAF interval before it crosses. WINDOW vs CUMULATIVE, deliberately both: a ring buffer reports the RECENT, not the WORST (estate law), so the header carries cumulative counters (frames ever, worst ever, over-budget ever) beside the PF_N-frame window the percentiles are computed over. Percentiles use the nearest-rank method over a 0..PF_CLAMP_MS histogram (rank = ceil(permil x n / 1000)). pf_push preserves legacy capped input; pf_push_raw retains stalls beyond the histogram and resolves their ranks from the raw ring. An EMPTY window answers PF_UNOBSERVED (-1) to every statistic -- a check that reads 0 frames as 0 ms would pass on the empty set. COMPOSES nx_frame_budget (the estate's per-frame budget ruler, microseconds): the budget in ms for a target fps is fb_budget_us(fps)/1000 and fits/headroom are fb_fits/fb_headroom_us -- not re-derived here. license_tier: ORIGINAL No hw writes (Rule 26).

dependencies 1 imports · 31 importers

nx_frame_budget.nx nx_perf_lib.nx nx_craft_before_placement_t316.nx nx_frame_pacing_lib.nx nx_movement_base_engine_t139.nx nx_perf_pct_gate.nx nx_wasm_craft.nx nx_wasm_craft_body_t137.nx nx_wasm_craft_contact_candidate.nx nx_wasm_craft_dunes_t70.nx nx_wasm_craft_gait_candidate.nx nx_wasm_craft_gait_candidate_t139.

diagram shows first 10 each side; +0 more imports, +21 more importers in the complete lists below.

imports: nx_frame_budget.nx

imported by: nx_craft_before_placement_t316.nxnx_frame_pacing_lib.nxnx_movement_base_engine_t139.nxnx_perf_pct_gate.nxnx_wasm_craft.nxnx_wasm_craft_body_t137.nxnx_wasm_craft_contact_candidate.nxnx_wasm_craft_dunes_t70.nxnx_wasm_craft_gait_candidate.nxnx_wasm_craft_gait_candidate_t139.nxnx_wasm_craft_imported_body_t41.nxnx_wasm_craft_imported_body_t42.nxnx_wasm_craft_imported_body_t49.nxnx_wasm_craft_imported_t262.nxnx_wasm_craft_inspection_bounds_t181.nxnx_wasm_craft_inspection_distance_t218.nxnx_wasm_craft_inspection_t142.nxnx_wasm_craft_journal_index_t21.nxnx_wasm_craft_journal_index_t21_before_t44.nxnx_wasm_craft_lookup_candidate.nxnx_wasm_craft_move_t72.nxnx_wasm_craft_pre_inspection_t142.nxnx_wasm_craft_recovery_t20.nxnx_wasm_craft_relief_adopt_t143.nxnx_wasm_craft_relief_save_t143.nxnx_wasm_craft_relief_t143.nxnx_wasm_craft_residency_t44.nxnx_wasm_craft_turn_candidate_t139.nxnx_wasm_craft_visitors_canonical_t142.nxnx_wasm_craft_visitors_inspection_t142.nxnx_wasm_craft_visitors_t140.nx

structs

none

consts

26const PF_N: i64 = 256 // the window: ~4.3 s at 60 Hz, the same order as the page's 600-frame ?perf probe at 10 s
27const PF_HDR: i64 = 8 // header words before the samples (layout below)
28const PF_CLAMP_MS: i64 = 1000 // a frame longer than a second is recorded AT the clamp (and still counts as worst)
29const PF_HBINS: i64 = 1001 // histogram bins 0..PF_CLAMP_MS inclusive
30const PF_WORDS: i64 = 1265 // PF_HDR + PF_N + PF_HBINS -- the region a caller must provide (8 + 256 + 1001)
31const PF_UNOBSERVED: i64 = 0 - 1
32const PF_PERMIL: i64 = 1000
33const PF_US_PER_MS: i64 = 1000
34const PF_SPIKE_MULT: i64 = 2 // the shipped-industry jank definition the page already used: a frame above 2x the window median
36const PF_H_COUNT: i64 = 0 // samples in the window (<= PF_N)
37const PF_H_HEAD: i64 = 1 // next write slot
38const PF_H_TOTAL: i64 = 2 // frames ever pushed
39const PF_H_WORST: i64 = 3 // worst frame ever (ms, clamped)
40const PF_H_OVER: i64 = 4 // frames ever over the budget handed to pf_push
41const PF_H_SUM: i64 = 5 // sum of ms ever pushed (clamped values)
42const PF_H_BUDGET: i64 = 6 // the budget (ms) the last push was judged against
43const PF_H_RESERVED: i64 = 7
45const PF_GREEN: i64 = 0
46const PF_RED: i64 = 1
47const PF_ABSTAIN: i64 = 3
173const PF_LONG_FRAME_MS: i64 = 50

functions

49func pf_words() -> i64 { return PF_WORDS }
50func pf_window() -> i64 { return PF_N }
called by 2: mainmain
51func pf_clamp_ms() -> i64 { return PF_CLAMP_MS }
called by 2: mainmain
53func pf_init(r: *i64) -> i64
61func pf_valid(r: *i64) -> i64
called by 1: pf_push_raw
68func pf_push(r: *i64, ms: i64, budget_ms: i64) -> i64
74func pf_push_raw(r: *i64, ms: i64, budget_ms: i64) -> i64
88func pf_n(r: *i64) -> i64 { return r[PF_H_COUNT] }
89func pf_total(r: *i64) -> i64 { return r[PF_H_TOTAL] }
90func pf_worst_ever(r: *i64) -> i64 { if r[PF_H_TOTAL] == 0 { return PF_UNOBSERVED } return r[PF_H_WORST] }
91func pf_over(r: *i64) -> i64 { return r[PF_H_OVER] }
called by 2: mainmain
92func pf_budget(r: *i64) -> i64 { return r[PF_H_BUDGET] }
94func pf_mean_x10(r: *i64) -> i64 { if r[PF_H_TOTAL] == 0 { return PF_UNOBSERVED } return r[PF_H_SUM]*10 / r[PF_H_TOTAL] }
called by 1: main
96func pf_sample(r: *i64, i: i64) -> i64
105func pf_hist(r: *i64) -> i64
called by 1: pf_pct
121func pf_tail_rank(r: *i64, rank: i64) -> i64
called by 1: pf_pct
138func pf_pct(r: *i64, permil: i64) -> i64
154func pf_worst(r: *i64) -> i64 { return pf_pct(r, PF_PERMIL) }
156func pf_spikes(r: *i64) -> i64
174func pf_long(r: *i64) -> i64
called by 1: main
185func pf_spikes_permil(r: *i64) -> i64
called by 3: mainpf_refereemain calls 1: pf_spikes
191func pf_budget_ms(fps: i64) -> i64 { if fps <= 0 { return PF_UNOBSERVED } return fb_budget_us(fps) / PF_US_PER_MS }
called by 1: main calls 1: fb_budget_us
192func pf_fits(ms: i64, budget_ms: i64) -> i64 { return fb_fits(ms * PF_US_PER_MS, budget_ms * PF_US_PER_MS) }
called by 1: main calls 1: fb_fits
199func pf_referee(r: *i64, budget_ms: i64, min_n: i64, p50_permil: i64, p95_permil: i64, p99_permil: i64, jank_permil_max: i64, why: *i64) -> i64