Files
Seele/docs/subsystems/core-utilities.md
coding-agent 1324d903cb docs: add per-subsystem deep-dives + cross-repo orientation (for coding agents)
Adds docs/ — a navigation layer on top of AGENTS.md for future coding
agents working on the Seele engine and the MeshShadingDemo harness.

- docs/README.md: index + 30-second orientation + how-to-use workflow.
- docs/cross-repo.md: the engine<->demo contract (2-symbol ABI, the
  ComponentSystem<...> demo pattern with the FlyCamSystem example, the
  CMake in-tree vs install(EXPORT) split, a where-to-look task map).
- docs/subsystems/*.md: 16 per-subsystem deep-dives (scene/ECS, systems,
  game-interface, window, graphics backends, render graph, materials,
  physics, UI, assets, platform, core utils). Each maps public types to
  their defining headers and gives file:line pointers + gotchas.

All cross-links verified resolving. This is documentation only — no source
changes.
2026-11-07 22:08:50 +00:00

88 lines
11 KiB
Markdown

# Core Utilities (Math, Containers, Log, Time, Memory, Reflection, Serialization)
> The engine's foundation: math, containers, logging, time, memory, the reflection/serialization system, and dynamic loading.
This is a **navigation** doc: it tells you where things live, how data actually flows, and the sharp edges — so you can orient in minutes instead of hours. It complements the repository's [AGENTS.md](../../AGENTS.md) (map + rules) and the [cross-repo guide](../cross-repo.md) (engine ↔ demo contract). Everything below is grounded in the checked-out source at commit `d3ed573`.
## Key types & where they live
| Type | Defined in |
|------|------------|
| `Log` | `git grep` in `src/` |
| `Log/Info` | `git grep` in `src/` |
| `Log/Warning` | `git grep` in `src/` |
| `Log/Error` | `git grep` in `src/` |
| `Log/Debug` | `git grep` in `src/` |
| `Seele::Math (Vec2/3/4, Mat3/4, Quat, Transform, BBox, Ray, Plane)` | `git grep` in `src/` |
| `Array<T>` | `git grep` in `src/` |
| `Stack<T>` | `git grep` in `src/` |
| `HashMap` | `git grep` in `src/` |
| `HashSet` | `git grep` in `src/` |
| `HashMapStringKey` | `git grep` in `src/` |
| `HashMapPtrKey` | `git grep` in `src/` |
| `StringMap` | `git grep` in `src/` |
| `StringHashMap` | `git grep` in `src/` |
| `StringHashMapPtrKey` | `git grep` in `src/` |
| `PtrHashMap` | `git grep` in `src/` |
| `PtrMap` | `git grep` in `src/` |
| `StringToPtrHashMap` | `git grep` in `src/` |
| `StringToPtrMap` | `git grep` in `src/` |
| `Optional<T>` | `git grep` in `src/` |
| `Result<T, E>` | `git grep` in `src/` |
| `String/ConstString` | `git grep` in `src/` |
| `GameTime` | `git grep` in `src/` |
| `Time` | `git grep` in `src/` |
| `Memory (malloc/free wrappers)` | `git grep` in `src/` |
| `Platform (OS abstraction)` | `git grep` in `src/` |
| `Reflection (struct member registry)` | `git grep` in `src/` |
| `Serialization (Archive/ArchiveBuffer/ArchiveJson)` | `git grep` in `src/` |
| `JobSystem (thread pool)` | `git grep` in `src/` |
Paths above are resolved against the actual source tree (namespace ≈ directory in this codebase). A `(search)` marker means the file contains the name but may also define others — open it and grep. `git grep` is always a fallback. The full per-file map is in **Where to look** below.
## Data flow / lifecycle
Log (src/Engine/Core/Log.h + Log.cpp, 1.8KB + 1.1KB) provides Log::info/warning/error/debug static methods that format a message with source location (file:line via __FILE__/__LINE__) and write to stdout + a log file (Log.cpp opens a log file in the working directory). There are also macros (LOG_INFO, LOG_WARNING, etc.) that wrap these. Math (src/Engine/Core/Math/) is a SIMD-friendly math library: Vec2/Vec3/Vec4 (float/double/int variants), Mat3/Mat4 (float/double), Quat, Transform (position/rotation/scale), BBox (axis-aligned bounding box with intersection tests), Ray (origin+direction, BBox intersection), Plane (normal+distance, point classification), Sphere. All are header-only inline functions with SIMD intrinsics (SSE/AVX where available). The math library is used pervasively: Transform (Component/Transform.h) uses Math::Mat4 for the world matrix, BBox is used for frustum culling in RenderPass::computePlane, Ray is used for ray queries. Containers (src/Engine/Containers/) wrap std:: containers with engine conventions: Array<T> (dynamic array with growth strategy, 5.6KB), Stack<T> (5.3KB), HashMap (open-addressing, 8.6KB), HashSet (10.7KB), HashMapStringKey/HashMapPtrKey (key-type specializations), StringMap/StringHashMap/StringHashMapPtrKey (string-keyed), PtrHashMap/PtrMap (pointer-keyed), StringToPtrHashMap/StringToPtrMap (string->pointer maps), Optional<T> (2.4KB, std::optional-like), Result<T,E> (2.7KB, tagged union for success/failure). The hash functions in HashMap/HashSet use a FNV-1a or similar non-cryptographic hash. String (src/Engine/Core/String/String.h + .cpp, 6.9KB + 16.9KB) is a custom string class (NOT std::string) with UTF-8 storage, format(), split(), find(), replace(), and interning (ConstString is an interned string reference — used for type names and asset names). ConstString (ConstString.h, 770B) is a non-owning reference to an interned string — it is implicitly convertible from string literals and std::string. GameTime (GameTime.h + .cpp, 812B + 945B) and Time (Time.h + .cpp, 730B + 1.2KB) provide high-resolution timing (std::chrono::high_resolution_clock wrappers). GameTime is the frame timer used by GameView. Memory (Memory.h + .cpp, 1.2KB + 2.8KB) wraps malloc/free/new/delete with allocation tracking (total allocated, peak, allocation count) and optional leak detection. Platform (Platform.h + .cpp, 1.4KB + 1.7KB) is the OS abstraction: file I/O (read/write/delete/mkdir), path manipulation (join, normalize, getExtension), command-line argument parsing, environment variable access, process ID. Reflection (Reflection.h + .cpp, 2.8KB + 3.4KB) provides a struct member registry — each serializable struct registers its members (name, offset, type) at compile time (via a macro or static initializer). This is used by the Serialization system to generically read/write struct fields by name. Serialization (src/Engine/Serialization/Serialization.h + .cpp, 5.1KB + 23.3KB) provides the Archive abstract class (read/write primitives: int/float/string/struct-by-reflection) + ArchiveBuffer (binary buffer archive, 1.3KB header + 13.6KB impl) + ArchiveJson (JSON archive, 2.2KB header + 11.6KB impl). ArchiveJson uses a JSON parser (likely nlohmann/json or a custom one — check the includes). The Archive class uses the Reflection registry to iterate struct members and serialize/deserialize them generically. JobSystem (JobSystem.h + .cpp, 801B + 963B) is a minimal thread pool for parallel work. It is used by the Executor (System/Executor.h) and potentially by the asset loader for parallel loading.
## Where to look
| Path | What's there |
|------|--------------|
| `src/Engine/Core/Log.h + .cpp` | logging |
| `src/Engine/Core/Math/ (all math types: Vec.h, Mat.h, Quat.h, Transform.h, BBox.h, Ray.h, Plane.h, Sphere.h, etc.)` | |
| `src/Engine/Containers/Array.h + .cpp` | |
| `src/Engine/Containers/HashMap.h + .cpp` | open-addressing hash map |
| `src/Engine/Containers/HashSet.h + .cpp` | |
| `src/Engine/Containers/Optional.h + .cpp` | |
| `src/Engine/Containers/Result.h + .cpp` | |
| `src/Engine/Core/String/String.h + .cpp` | custom string, 16.9KB impl |
| `src/Engine/Core/String/ConstString.h` | interned string reference |
| `src/Engine/Core/GameTime.h + .cpp` | frame timer |
| `src/Engine/Core/Time.h + .cpp` | high-res clock wrapper |
| `src/Engine/Core/Memory.h + .cpp` | allocation tracking |
| `src/Engine/Core/Platform.h + .cpp` | OS abstraction: file I/O, paths, args |
| `src/Engine/Core/Reflection.h + .cpp` | struct member registry |
| `src/Engine/Serialization/Serialization.h + .cpp` | Archive base + reflection-driven serialization, 23.3KB impl |
| `src/Engine/Serialization/ArchiveBuffer.h + .cpp` | binary archive, 13.6KB impl |
| `src/Engine/Serialization/ArchiveJson.h + .cpp` | JSON archive, 11.6KB impl |
| `src/Engine/Job/JobSystem.h + .cpp` | thread pool |
## Gotchas & sharp edges
- The custom String class is NOT std::string — it has a different API (no std::string interop). If you pass a std::string to a function expecting Seele::String, you must construct a String from it (String has a std::string constructor). The reverse (String -> std::string) is via String::c_str() + String::length().
- ConstString is an INTERNED string reference — it is cheap to copy (pointer + hash) but the underlying string must outlive the ConstString. ConstStrings are used for type names in the Reflection system and for asset names in the AssetManager. Do NOT create a ConstString from a temporary string literal that will be destroyed.
- The Reflection system requires explicit member registration — each serializable struct must call DEFINE_REFLECTION(TypeName) or equivalent macro at file scope. If you add a new field to a struct but forget to add it to the reflection registration, the field will be SILENTLY SKIPPED during serialization (no error, no warning). This is the most common source of data corruption bugs in this engine.
- ArchiveJson (JSON serialization) and ArchiveBuffer (binary serialization) are interchangeable — any struct can be serialized to either format. The Archive base class dispatches to the concrete archive at runtime. If you mix the two (read a binary file with ArchiveJson), you will get garbage data with no error (check the type tags in the serialization format).
- The Math library is HEADER-ONLY — all functions are inline. There is no separate .cpp file for most math types. If you get linker errors for math functions, check that you included the correct header (e.g. Math/Vec3.h for Vec3, not Math.h).
- The Array<T> container has a GROWTH STRATEGY — it does not use std::vector's doubling strategy. The growth factor is configurable (check Array.h). If you resize an Array to a very large size, the growth strategy may cause multiple reallocations. For predictable performance, reserve() in advance.
- The HashMap/HashSet use OPEN-ADDRESSING with a specific probe sequence (linear or quadratic — check the header). The load factor is capped (typically 0.7) — if you exceed the cap, the map rehashes. Rehashing is O(n) and can cause frame hitches if it happens during a frame update. Pre-size the map if you know the expected size.
- Platform.h provides file I/O — it is the ONLY way to do file I/O in the engine. Do NOT use std::ifstream/std::ofstream directly (the Platform layer may use a different I/O strategy, e.g. memory-mapped files on some platforms). The AssetManager uses Platform for all file access.
- JobSystem (thread pool) is minimal (963B impl) — it has a fixed thread count (from GraphicsInitializer or a default) and a simple task queue. It is NOT a work-stealing scheduler. If you submit more work than the pool can handle, the excess is queued and processed in order. There is no priority, no cancellation, no async result (no std::future equivalent).
- The Memory tracking (Memory.h) is ON by default in debug builds — it tracks every allocation (total, peak, count) and can report leaks at shutdown. In release builds, the tracking is compiled out (check the #ifdef in Memory.h). If you see performance issues, the memory tracking overhead in debug builds may be significant (each allocation has a tracking call).
- GameTime (frame timer) uses std::chrono::high_resolution_clock. On some platforms (Windows), high_resolution_clock has a 1ms granularity by default — the engine may use QueryPerformanceCounter directly for better resolution (check GameTime.cpp). If you see frame timing jitter, the clock resolution may be the cause.
- The Log system writes to BOTH stdout and a log file (Log.cpp opens the log file in the current working directory at first log call). The log file is NOT rotated — it grows without bound. For long-running applications, you may need to implement log rotation externally (or modify Log.cpp to add rotation).
- The String class has a format() method (printf-style) — use it for log messages. Do NOT use std::format or snprintf directly (the String::format is the engine's convention and may have platform-specific behavior).
---
*Generated from a full read of the checked-out source at commit `d3ed573`. If a path has moved, `git grep` for the type name — the engine is small enough to find anything in seconds.*