Architecture & Philosophy

The Xi namespace isn't just a reimplementation of the C++ Standard Template Library (STL)—it is a fundamental rethinking of how memory, errors, and collections should behave in severely constrained environments like the ESP32, Arduino, and bare-metal ARM Cortex architectures.

Why Not Just Use std::vec or std::string?

If you've ever built a long-running IoT device, you know the silent killer: Heap Fragmentation.

The C++ STL is designed for desktop systems with virtual memory and gigabytes of RAM. Features like std::function dynamically allocate memory silently under the hood. Resizing a std::string often involves copying the entire buffer, leaving holes in the heap. Eventually, malloc fails, and your ESP32 crashes unpredictably.

Xi eliminates this uncertainty through three core design pillars:

1. Zero Exceptions & No RTTI

Xi is compiled with -fno-exceptions and -fno-rtti.

  • There are no try/catch blocks. Stack unwinding metadata wastes flash space.

  • If a bounds check fails (e.g. array[999]), the system either returns a safely encoded error Result or enters a designated crash loop (std::abort()). This keeps binaries tiny and execution paths 100% deterministic.

2. Micro-Allocation Avoidance

  • Xi::Func uses a fixed-size internal buffer (e.g., 64 bytes) to store lambda captures entirely on the stack or inline within a class. It never calls malloc for standard closures.

  • Xi::Map uses an inline array for its buckets rather than allocating individual linked-list nodes for every single key-value pair.

3. Explicit Memory Control

In Xi, you are always aware of how memory is changing:

  • String distinguishes between its physical size() and its logical payload.

  • Expanding buffers is done explicitly (e.g., alloc()), guaranteeing you know exactly when an expensive heap operation is happening.

  • Many operations leverage View proxies (like InlineArray slices) that point to existing memory rather than cloning data.

4. Cross-Platform & STL Interoperability

While aggressively optimized for microcontrollers, the Xi framework is completely universal. It compiles and runs flawlessly on Linux, macOS, and Windows desktops or servers with zero headaches.

When compiled for standard desktop environments (#ifndef ARDUINO), Xi automatically hooks into underlying OS-level or standard library features where appropriate (like std::cerr for logging or os.urandom() for cryptography) to guarantee performance and thread-safety while maintaining its strict memory guarantees. Furthermore, Xi primitives expose standard C++ compliant memory pointers (.data()) and iterators (begin(), end()), meaning perfectly seamless integration with almost all standard C-APIs and POSIX systems.

Last updated