# Performance

> What the framework costs over raylib and what it saves: the abstractions that compile to nothing, the few that cost a call, how the map, the collisions, the UI and the build keep a frame and a binary small, and how it compares with raylib and with OpenGL by hand.

Source: https://omardev29.github.io/rmp-docs/framework/performance.html

The framework sits on raylib and draws everything through it, so a frame can never be faster than raylib makes it. What the framework decides is how much it adds on top, and how much work it saves the game from doing at all. This page goes through both: the abstractions that cost nothing, the few that cost a function call, the places where the framework does *less* work than a hand-written raylib game would, and where OpenGL by hand still wins.

## The rule: you pay for what you use

"Zero-cost abstraction" means two things here, and the framework holds both.

- **What you do not use costs nothing.** The framework is a static library compiled with `-ffunction-sections -fdata-sections` and linked with `--gc-sections` and link-time optimisation in release. The linker takes only the objects something references and throws away every function nobody calls. A game that never touches `rmp::ui` ships no UI code; a game that never loads an Aseprite file ships no Aseprite reader. The strongest form of the rule is ["a 2D game pays nothing for 3D"](https://omardev29.github.io/rmp-docs/framework/architecture.html): not one byte of the 3D renderer, the physics or raylib's model loaders reaches a 2D binary.
- **What you use costs about what you would have written yourself.** A helper is not worth having if writing the raylib calls by hand would be faster. Where an abstraction does cost something, it is one indirect call or one lookup, said below.

## What compiles to nothing

| Abstraction | What the compiler makes of it |
|---|---|
| Options structs with designated initialisers (`{ .speed = 320 }`) | A plain struct built on the stack. No builder, no map of names, no allocation. |
| The math types (`Vector2` and friends from raylib-cpp, the operators of `rmp/math.h`) | Header-only and inline: `a + b * delta` is the same instructions as `Vector2Add(a, Vector2Scale(b, delta))`. |
| RAII ownership (`rmp::Texture`, `rmp::Sound`, the scenes and objects) | A destructor that calls the `Unload*` you would have called. No reference counting, no garbage collector, no pause. |
| No exceptions | No unwinding tables on the hot path, no `try` anywhere in the library: a failure is a return value, a report once, or a default. [No exceptions](https://omardev29.github.io/rmp-docs/framework/why/no-exceptions.html) says why. |
| Behaviors that leave out a hook | Hooks are found at compile time with `requires`. A behavior with no `_draw` has a null entry in its table of hooks, and the frame never calls it. |
| Hot reload in a release, or off the desktop | `changed()` is a constant `false`; no file is ever watched. |
| Configuration | The `.toml` becomes `#define`s at configure time ([the config pipeline](https://omardev29.github.io/rmp-docs/framework/config-pipeline.html)). Nothing is parsed when the game starts. |

## What costs a call, and why it is worth it

- **A behavior's hook** is one indirect call through a table of function pointers built for its type at compile time. That is the price of a virtual function, without a base class or a vtable pointer in the behavior. A hook the behavior does not define is not called at all.
- **`rmp::Callback`**, what every `on_click` and `on_hit` is, is one call through a function pointer over the lambda you gave it. It is move-only on purpose, so it carries none of `std::function`'s copying machinery.
- **A handle** (`rmp::Handle`, `rmp::Ref`) checks a slot and a generation number before it hands you the object: two loads and a compare, in exchange for never reading an object that was destroyed.
- **`obj.get<B>()`** looks for a behavior by type among the few an object carries. Keep the result in a local if you ask for it many times in one hook.
- **An action by name** (`rmp::input::pressed("jump")`) is a lookup in a small table, so the same name works for a key, a button and a touch.

## Where the framework does less work than a hand-written game

### The map draws only what the camera sees

A naïve tilemap draws every tile every frame. `Scene` hands `Tilemap::draw()` the camera's view, and only the tiles inside it are drawn: LDtk layers through chunks of 16×16 cells, Tiled layers through their range of cells. Nothing is baked into a render texture, so the map stays exact at any zoom and costs no video memory. Measured on a 400×400 map: **87 ms a frame drawn whole, 0.25 ms culled**, with the same pixels (`ui_render_test` checks that). See [Tilemaps](https://omardev29.github.io/rmp-docs/manual/modules/tilemaps.html).

### Collisions: a grid, and no tile colliders

Objects go through a uniform grid sized to the objects themselves, so each one is tested only against its neighbours instead of against everything (the O(n²) a beginner writes). The map's solid tiles are never turned into colliders: when the map is read, every source of solidity is folded into one table, and an object is moved in steps of at most half a cell and pushed out by bisection against that table. A map of any size adds the same few lookups per moving object. Fast objects use a swept test, so they cannot pass through a wall between two frames.

### The UI allocates nothing per frame

`rmp::ui` is immediate mode on top of Clay, a layout engine written in C. Clay works in one arena reserved once; every string the UI is given in a frame goes into a bump arena that is reset at the next `begin()`. A menu redrawn every frame makes no heap allocation. Fonts are baked once per size, and a glyph is added to the atlas only the first time a text uses it.

### One clock

The framework reads the time once per frame and hands the same `delta` to every scene, object and UI animation. Nothing asks the operating system for the time again in the middle of a frame.

### Hot reload that costs a bool

In a debug build on the desktop, the files a game asked to watch are checked at most every 0.25 s, by size and time; their bytes are read only when one of those moved. Asking `changed()` every frame reads a flag.

### A window nobody sees draws nothing

A minimized desktop window is not drawn at all. The world, the music and the clock go on, but the loop skips `BeginDrawing()` and `EndDrawing()` and sleeps a thirtieth of a second a frame instead of waiting for a vsync: a game left minimized on a laptop stops spending the GPU and most of a core. With [`[window] pause_unfocused`](https://omardev29.github.io/rmp-docs/reference/configuration.html#window-pause_unfocused) on, a window without the focus does not even move its scenes: it draws the same picture ten times a second and pauses the sound. A browser and a phone do this for a hidden page or app on their own, so the framework leaves them to it. [The frame](https://omardev29.github.io/rmp-docs/framework/the-frame.html#out-of-sight) has the order of it.

### A translated text is one lookup

An [`rmp::Text`](https://omardev29.github.io/rmp-docs/reference/classes/rmp-Text.html) keeps the words the code wrote and is translated when it is drawn: one lookup in a hash table of the loaded tables, then the placeholders filled. That is what makes [`rmp::lang::set()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-lang.html#set) change every screen at once, with nothing to rebuild. A widget's identity is the text's key, never its translation, so changing the language costs no layout memory either: the focus and the hover stay where they were. [Localization](https://omardev29.github.io/rmp-docs/manual/modules/localization.html) has the rest.

### A Yarn script is compiled once

A `.yarn` file is compiled into instructions when it is loaded, or, in a release on the desktop, when the game is built: `rmp_yarnc` runs over the game's scripts and their bytes are compiled into the binary, so the release reads no script and parses nothing. Either way a running conversation only steps a small stack machine from one line, choice or command to the next. [Dialogue](https://omardev29.github.io/rmp-docs/manual/modules/dialogue.html#build) says how to choose.

## The binary

- **Size is budgeted.** Every binary a release ships has a line in `tools/size_budget.txt`, and CI fails a growth of more than 10 % that nobody wrote down. A 2D game's release is a few hundred KB packed.
- **No iostreams in the library.** One `std::ifstream` once added 19 % to the Linux binary and 18 % to the web's `.wasm`; files are read with C's stdio now, and a check refuses an iostream header in the library.
- **A number in a sentence cost 126 KB once.** A floating-point `std::to_chars` brings libc++'s formatting tables into every static link: the Linux release of the demo game was 1,510,904 bytes with it and 1,384,760 without. The framework writes a number with `printf`'s `%g` at the fewest digits that read back as the same number, which gives the same digits `to_chars` would.
- **Android exports only what Java and the system call.** The game is one shared library, and a shared library keeps everything it exports. With every symbol exported, a game that never loads a dialogue carried the Yarn compiler. The release is compiled with `-fvisibility=hidden` and linked with `--gc-sections`, and what the system looks up by name is marked to stay: the arm64 library went from 3,091,080 bytes to 1,108,688.
- **Hardening costs little.** FORTIFY_SOURCE, the stack protector and stack-clash protection together cost 2.0 % of the binary. PIE costs 5.5 % and stays on for security reasons ([Security](https://omardev29.github.io/rmp-docs/framework/security.html) says why).
- **Debug is slow on purpose.** The framework's own Debug runs under ASan and UBSan, about three times slower. A game's Debug has no sanitizers unless the game asks for them, and on x86-64 Linux it can compile its C with tcc to build in a fraction of the time. Measure performance in a release.

## Compared with raylib and with OpenGL

|  | raylib by hand | rmp | OpenGL by hand |
|---|---|---|---|
| Draw calls | rlgl batches quads and lines into one buffer and flushes when the texture, shader or blend mode changes | The same: every sprite, tile, shape and UI element is drawn through raylib, so it batches exactly as raylib does | Whatever you build: instancing, persistent mapped buffers, texture arrays, bindless |
| Per-object cost | Your loop | Your loop, plus one indirect call per hook a behavior defines | Your loop |
| Large maps | Every tile unless you cull by hand | Culled to the camera | Usually one draw per chunk from static buffers |
| Collisions | Pairs you test yourself | Grid broad phase and a solid table for the map | Not its business |
| UI | raygui, redrawn by hand | Clay layout, no allocation per frame | Not its business |

In practice: for a 2D game a frame costs what the same game written well in raylib costs, often less, because the culling, the grid and the arenas are things a game written in a hurry does not have. The framework never adds a draw call of its own beyond what it draws for you.

Where OpenGL by hand still wins: tens of thousands of moving sprites, particles that should live on the GPU, or a renderer built around instancing. raylib's batch rebuilds its vertex buffer on the CPU every frame, and the framework does not change that. A game that needs more can still call rlgl or OpenGL directly inside an object's `_draw()`: the framework does not stand in the way of either.
