Why We Rewrote KCL in Rust: 66% Faster Execution and 20× Faster Parsing
This article details the motivations, challenges, and outcomes of rewriting the KCL compiler from Python to Rust, highlighting significant performance gains, reduced bugs, memory savings, and the learning curve associated with adopting Rust for a large‑scale systems project.
Rust has quietly become one of the most popular programming languages, offering memory‑safety, near C/C++ performance, a strong community, and robust tooling.
The KCL project, an open‑source constraint‑based configuration language, was originally written in Python. To improve user experience, performance, and stability, the team rewrote it in Rust.
Rust's strong compile‑time checks reduced bugs.
End‑to‑end compilation and execution speed increased by 66%.
The front‑end parser became 20 times faster.
Semantic analysis improved 40 times.
Compiler memory usage dropped to half of the Python version.
Encountered Problems
The original Python implementation suffered from slow startup, difficult maintenance as the codebase grew, and runtime errors such as None dereferences. Scaling the compiler to meet higher automation and online execution demands proved impossible with Python.
Why Use Rust?
Performance tests comparing simple stack‑VM implementations in Python, Go, and Rust showed Rust matching Go and vastly outperforming Python, leading to the decision to adopt Rust. https://github.com/Peefy/StackMachine Rust is increasingly used in compilers, runtimes, front‑end infrastructure, databases, networking, cloud‑native projects, UI, embedded systems, and blockchain, confirming its suitability for large‑scale systems.
Rust also offers seamless FFI with C APIs, enabling multi‑language integration, and excellent WebAssembly support, allowing KCL to compile to WASM for browser execution.
Challenges of Using Rust
The team had limited Rust experience, mainly from reading "The Rust Programming Language". Learning Rust’s ownership, lifetimes, and lack of inheritance required a steep learning curve.
Example translation from Python dataclasses to Rust enums:
from dataclasses import dataclass
class KCLObject:
pass
@dataclass
class KCLIntObject(KCLObject):
value: int
@dataclass
class KCLFloatObject(KCLObject):
value: float enum KCLObject {
Int(u64),
Float(f64),
}Lifetime errors also posed difficulties, as shown in the following snippet:
struct Data<'a> { b: &'a u8 }
impl<'a> Data<'a> {
fn func1(&self) -> Data<'a> { Data { b: &0 } }
fn func2(&self) -> Data { Data { b: &0 } }
}Development Efficiency
Initially, development speed lagged behind Python, Go, or Java due to unfamiliar functional programming concepts. However, once the team mastered Rust’s standard library, common compiler errors, and best practices, productivity increased dramatically, enabling the production of high‑quality, safe, and efficient code.
Benefits Gained
After several months of rewriting, the Rust‑based KCL compiler became stable in production, offering faster compilation, lower memory usage, fewer bugs, and improved overall system robustness. Although not every module achieved a 40× speedup (some were limited by runtime memory copies), the overall gains justified the effort.
Conclusion
Rewriting KCL in Rust delivered a more stable, faster, and maintainable compiler, eliminating startup latency and automation bottlenecks while leveraging Rust’s zero‑GC, high performance, strong error handling, and memory management features.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
