Hamon (刃文)
Hamon is a zero-cost, type-level static decorator library for Rust. It allows you to compose complex data processing pipelines that are resolved entirely at compile time, eliminating the performance overhead of dynamic dispatch and heap allocation. Additionally, the sake of modularity this crate brings about ensuring for the ability to extension, testability and ease of writting code in Rustacean fashion.
⚔️ The Philosophy
Data transformation takes input and produces the output when it recurs several times requiring us to write the boilerplat code which seems to be tedious tasks, somewhat hard to test, maintain, and utimately doesn't reserve rooms for the extension. In practice, Logic onwer may bring up a pipeline step with massive logic underneath. Even the down-breaking of processing units emerges at the cure for simplicity, it deems to be adequate due to the disunion of what it would take and produce.
On top of that, In high-performance systems programming, every instruction counts. Traditional decorator patterns often rely on Box<dyn Decorator>, which incurs a "vtable tax"—the performance penalty of pointer chasing and inhibited compiler optimizations.
Hamon leverages Monomorphization. By using recursive generics, the entire execution chain is baked into the type system. This allows the LLVM optimizer to "see through" the abstraction, inlining logic and generating machine code that is as fast as a hand-written monolithic function.
🚀 Outstanding Features
- Zero-Cost Abstractions: No dynamic dispatch. Direct function calls are resolved at compile time.
- Type-Level Validation: The compiler verifies your pipeline. If a decorator doesn't fit the lineage, it won't compile.
- Stack-First Efficiency: Avoids heap fragmentation. Data remains in CPU registers and the stack for maximum throughput.
- Occupancy for error-handling: Transformations are not crossing through. They should be considered fallible. Pipeline will be ceased if encoutered an error.
- Modular Maintainability: Break 500-line "spaghetti" functions into small, isolated, and testable
Decoratorstructs without losing performance.
📊 Performance Dojo
Benchmarks comparing a 50-step pipeline across three implementation strategies:
| Implementation | Average Time | Memory Allocations | Speed Delta |
|---|---|---|---|
| Monolithic Function | 4.36 µs | 1 (Final Result) | — |
| Hamon (Static) | 4.98 µs | 1 (Final Result) | Baseline |
| Traditional (Dynamic) | 10.14 µs | 51 (Boxes + Vec) | ~2.0x Slower |
Hamon provides the modularity of a dynamic system with the raw speed of hand-optimized code.
🛠️ Usage
Hamon uses a conservative, familiar API based on the Builder and Decorator patterns.
use ;
use *;
// A decorator that adds up an amout to the current value (as i32)
;
// A decorator that multiplies up an amout to the current value (as i32)
;
// A decorator converts the integer value to String value
;
// Convert back to INT
;