
Building PR3, a personal AI inspired by OpenJarvis
PR3 is a privacy-first personal AI runtime, inspired by OpenJarvis’s modular architecture, I developed PR3 independently in Rust, with privacy and security at its core. PR3 stands for Portable Private Personal Intelligence. The P comes from my name, and the three Rs represent the names of my wife, my son, and our dog. PR3 runs a local model, keeps memory on the user’s own devices, and lets an agent act only through a deterministic trusted path.

The first working version had a small Rust core, a Tauri app and a local Qwen model running through llama.cpp. The model could ask to use a tool. The core checked the request, and the host did the work. That division became visible in the chat window when I added web research. I can turn Web on for a question or restrict it to a particular site. With Web enabled, the search provider gets the topic I submitted. Keywords the model comes up with to search my local documents stay on my device.
Many weeks ago I started a second version in C17 with SDL3, to see what a heap-free design could do on phones, computers and microcontrollers alike. So far it has a base library, canonical JSON and the agent core. This note is about measuring the two against each other, and what moved the numbers.
Same workloads, same machine
Both cores run four workloads: a plain turn, a tool turn of two rounds with two calls each, and a tool turn on a full history whose tool results are 64 bytes or 8 KB. The plain and tool turns end by exporting their trace as JSON; the history workloads leave it out, to measure what the history’s size costs. Everything ran on one M2 Max, as release builds, 30 runs of at least 100 ms each, with the builds interleaved over two rounds. Every Rust build below writes byte-identical trace JSON to the original on 40 turns of each workload.
JSON alone, the first thing both had, already didn’t go one way. C’s tape parser on an arena beat serde_json 2 to 5 times on nested structure, and serde_json won on numbers and escaped strings. The algorithm decided it, not the language. The whole turn told the same story at a larger scale.
Round one: C was 5 to 17 times faster
| Workload | C | Rust | Rust allocations per turn |
|---|---|---|---|
| Plain turn | 0.53 µs | 2.74 µs | 75 (8.6 KB) |
| Tool turn | 3.76 µs | 18.8 µs | 554 (68 KB) |
| Full history, 64 B results | 0.39 µs | 6.60 µs | 239 (26 KB) |
| Full history, 8 KB results | 0.66 µs | 9.17 µs | 239 (173 KB) |
The C core allocates nothing per turn: everything comes from arenas sized when the core starts. The Rust core took owned inputs and cloned the whole history into every inference request, which is 173 KB per turn once tool results are 8 KB. That is design, not language.
Writing the Rust the C way: Rust was 5 to 16 times faster
I changed a copy of the Rust core in three passes, profiling before each one and keeping #![forbid(unsafe_code)]:
- Borrowed views.
stepborrows the host’s input, and a model request lends the session’s history instead of cloning it. This made it 1.6 to 3 times faster and cut allocations by 60%. - Reused buffers. The trace became typed steps over one text buffer kept between turns, argument checking stopped building a JSON map, and dropped messages go to a bounded spare pool. Zero allocations per turn.
- Fixed slots. Each turn lives inside its session, the history is a ring lent as two slices, each call is stored once, numbers are written by hand instead of through
core::fmt, and the trace has its own JSON writer.
Time per turn, in µs:
| Workload | Rust first | Views | Reused buffers | Fixed slots | C first |
|---|---|---|---|---|---|
| Plain turn | 2.73 | 1.64 | 0.77 | 0.37 | 0.52 |
| Tool turn | 18.58 | 10.57 | 4.25 | 3.42 | 3.70 |
| Full history, 64 B | 6.50 | 2.72 | 0.62 | 0.40 | 0.39 |
| Full history, 8 KB | 9.16 | 3.08 | 0.91 | 0.64 | 0.64 |
The last pass put safe Rust 28% ahead of C on the plain turn and 8% ahead on the tool turn. Every change that got it there was something the C core already did.
Then I profiled the C: It was 1.04 to 1.93 times faster
The C core had never been profiled. Exporting the trace took two thirds of the plain turn and half of the tool turn, and the cause was the generated code, not the method. In C, a char store may alias any object, including the builder it writes through. So after every byte, the compiler reloaded the builder’s length, capacity and pointer from memory and stored the length back: about 12 instructions per write, each waiting on the one before.
The fix is to write through a copy whose address never leaves the function, so the compiler can keep it in registers:
pr3_buf copy = {dst->ptr, dst->len, dst->cap, dst->overflow}, *out = ©
/* ... every write goes through out ... */
dst->len = copy.len;
dst->overflow = copy.overflow;
Rust gets this for free: a &mut is never aliased, and the compiler knows it. With that change, strings scanned 8 bytes at a time, each key written as one constant, and a history with twice the slots, so trimming moves where it starts rather than the messages, the plain turn went from 0.52 to 0.19 µs and the tool turn from 3.7 to 2.1 µs. The output stayed byte for byte the same, and C was ahead again on all four workloads.
The latest build of each, interleaved over three rounds, in ns per turn:
| Workload | Rust, fixed slots | C, profiled | C faster by |
|---|---|---|---|
| Plain turn | 373 | 194 | 1.93× |
| Tool turn | 3,407 | 2,065 | 1.65× |
| Full history, 64 B | 401 | 344 | 1.17× |
| Full history, 8 KB | 633 | 610 | 1.04× |
C’s lead is largest on the plain and tool turns and down to 4% on the 8 KB history.
Final chance for Rust
The C profile doubled as a list of what Rust could still take. Profiling the Rust core showed the same costs, so I made five changes, still under #![forbid(unsafe_code)]:
- Strings scanned 8 bytes at a time. The same word trick as the C. Rust spells the wrap as
wrapping_sub, so there is nothing to tell a sanitizer. - Numbers pushed as ASCII. Digits come two at a time from a table and go in as characters, so no buffer is checked for UTF-8 afterwards.
- A history written in place. The ring holds message slots. A new message is written straight into a free slot and joins the history when committed. Dropping old exchanges moves nothing, and a reused slot keeps its buffers.
- Tool results stored once. The trace points at each call and result where the history keeps them, instead of copying them. The C core still copies each result into its trace, so with 8 KB results it holds every one twice.
- Arguments checked while they are parsed. Each value lands in its property’s slot, already in sorted order, and a string without escapes stays a span of the input. Anything unusual, such as a float, a nested value or an escape, falls back to the full
serde_jsoncheck, and a property test holds both paths to the same verdict.
Two smaller ones followed: each tool is looked up once per call instead of three times, and the loop guard records where each call sits in the history instead of keeping a copy of it.
Interleaved over two rounds, in ns per turn:
| Workload | Rust, fixed slots | Rust now | C, profiled | Faster |
|---|---|---|---|---|
| Plain turn | 373 | 274 | 199 | C, 1.38× |
| Tool turn | 3,407 | 2,370 | 2,105 | C, 1.13× |
| Full history, 64 B | 401 | 273 | 349 | Rust, 1.28× |
| Full history, 8 KB | 633 | 399 | 606 | Rust, 1.52× |
Rust is now ahead on both history workloads and within 13% on the tool turn, without a line of unsafe code in the core: #![forbid(unsafe_code)] makes the compiler reject any. Most of the plain turn’s gap is writing text, and numbers alone are a quarter of its profile. Safe Rust can only append characters, or text already known to be UTF-8, to a String, so each digit goes in as its own character: about 5 ns a number. The C core just writes the bytes. This looked like the clearest place where forbid(unsafe_code) costs time.
The app moved onto the borrowed API too. It still keeps its own copy of each action, because it acts on them across awaits with the core unlocked, and its backend moves each request to a worker thread. It no longer copies traces that nothing records, and tracing is off by default.
One more round: bytes, not characters
A review of that round found six bugs, and my first fix for one of them cost up to 73% on a full history: it rotated the history’s ring after every turn to hand its free slots back. Rotating only when the ring holds more free slots than one more exchange needs brought every workload back to within 5% of before.
Then I profiled both cores side by side. Stepping a plain turn took each about 84 ns, and Rust stepped the tool turn in 21% less time than C: 1.15 µs against 1.46. The whole gap was writing the trace: 187 ns against 111 on the plain turn, and 1.19 µs against 0.65 on the tool turn.
That cost came from the String, not from forbid(unsafe_code). A String takes only whole characters, so the writer pushed each digit on its own, and copied each run of text between two escapes with its own call to memmove; a tool call’s arguments, full of quotes, make many short runs. A Vec<u8> takes bytes and is just as safe, so the writer now does what the C does. It copies a string 8 bytes at a time and cuts back to the first byte that needs an escape:
while let Some((word, after)) = rest.split_first_chunk::<8>() {
out.extend_from_slice(word); // all 8 bytes, escapes or not
let flags = escape_flags(u64::from_le_bytes(*word));
if flags == 0 {
rest = after;
continue;
}
// Cut back to the first byte that needs an escape, and write that.
let at = (flags.trailing_zeros() / 8) as usize;
out.truncate(out.len().saturating_sub(8 - at));
put_escape(out, word.get(at).copied().unwrap_or(0));
rest = rest.get(at + 1..).unwrap_or(&[]);
}
A number’s digits go in as one 20-byte copy that is then cut to length, and every literal is a copy whose size the compiler knows. The writer no longer calls memmove, its output is byte for byte the same, and every test and fuzz run now checks each trace against serde_json. The plain turn went from 270 to 216 ns, and the tool turn from 2.32 to 1.93 µs.
The last thing was memory. The C core reserves one block when it starts and reports how far a turn reaches into it; the Rust core allocates as it goes. With 8 KB results, Rust held 296 KB against C’s 82 KB, even though it stores each result once and C copies each into its trace. A reused slot kept its buffers, and since slots go round the history, every slot had come to keep an 8 KB buffer while only 8 held a result at a time. Now a slot keeps at most 1 KB for itself. A larger buffer leaves with its text and waits among eight spares for the next text that large. Rust holds 99 KB, still allocates nothing per turn, and lost no time.
Interleaved over two rounds, in ns per turn:
| Workload | Rust, last round | Rust now | C, profiled | Faster |
|---|---|---|---|---|
| Plain turn | 274 | 223 | 197 | C, 1.13× |
| Tool turn | 2,370 | 1,988 | 2,110 | Rust, 1.06× |
| Full history, 64 B | 273 | 265 | 349 | Rust, 1.32× |
| Full history, 8 KB | 399 | 374 | 612 | Rust, 1.63× |
Rust is now ahead on three of the four workloads, the tool turn included, and C keeps the plain turn by about 25 ns. Neither allocates in a steady turn. C still holds less: 5.4 KB against 14 KB on a plain turn, and 82 KB against 99 KB with 8 KB results.
What fast C costs
The C speedup rests on rules the compiler does not check. A writer that returns before its write-back loses its output with no error. Copying the builder as a whole struct also copies its padding, and on the Pico’s Cortex-M0+ that becomes a call to __aeabi_memcpy, a function the board image does not have; only the board’s link check noticed. The 8-byte escape scan wraps unsigned integers on purpose, so it has to be marked, or UBSan aborts the tests.
I wrote nine of these rules into the memory-safety spec, each with what catches a break. Writing that table showed that one test checked an escaped string but not the bytes past the buffer. A stray write I planted passed it, until I added guard bytes.
The C repo carries a lot to keep this honest: 120 tests under five builds (debug, ASan, UBSan, TSan and release), CBMC proofs for the containers and parsers, libFuzzer runs, and link checks for two boards. In Rust, most of that is the compiler’s job.
Where it stands
- Same backend, same ceiling. Both compile through LLVM. Every gap I measured came from design: copies, allocations, maps and the code the compiler could generate. Each fix ported in both directions. Even the gap I had put down to
forbid(unsafe_code)came from a choice of type. - Users won’t feel it. A turn takes 0.2 to 2.1 µs in either language; the model takes milliseconds per token.
- C wins where there is no heap. The C core links for the Pico’s Cortex-M0+ and the ESP32-C3’s RV32IMC with no allocator, and the JSON image is 18.7 KB of code. The Rust core needs a heap and has only been type-checked for a microcontroller.
- Rust wins on keeping it correct. Its compiler holds the rules that C keeps in a document, tests and sanitizers. Every change above kept
forbid(unsafe_code), and the Rust version is already the whole product.
The Rust changes have moved from the copy into the main core and the app. I’m continuing with the Rust version, and the C core stays as the baseline it is measured against.