Files
Kigi-CLI/crates/codegen/kigi-tui/examples/memory_benchmark_accurate.rs
T
ZacharyZhang-NY 6f31415ed6 §9 acceptance: grep-zero sweep — every internal x.ai/grok identifier renamed
The PRD's first acceptance gate now holds: grep -RinE '\bx\.ai\b|grok'
crates/ --include='*.rs' → 0 matches (exempt: NOTICE and third-party
license archives, README provenance, and the required 'Based on Grok
Build Open Source' attribution, now sourced from version_attribution.txt).

Wire-visible renames (both sides in this repo, changed in lockstep):
- Auth method id 'grok.com' → 'kimi-code' (AuthMethodKind::KimiCode).
- Every x.ai/* and _x.ai/* ACP ext method and meta key → kigi/* /
  _kigi/* (~200 names; grokShell → kigiShell). Session-file replay keeps
  a read-side alias for the legacy '_x.ai/session/update' method so
  existing updates.jsonl histories load; writes emit only the new name
  (both directions test-pinned).
- Agent types grok-build* → kigi* with a documented legacy-prefix alias
  at resolution time so persisted sessions keep resolving.
- ToolNamespace/BuiltinAgentName GrokBuild* → Kigi* (wire snake_case
  kigi/kigi_concise/kigi_hashline; schema regenerated); grok_build
  implementation dirs renamed to kigi*.
- x-grok-* headers → x-kigi-*, __GROK_* sentinels → __KIGI_*, themes
  grokday/groknight → kigiday/kiginight (old persisted values fall back
  to the default theme), web_fetch allowlist xAI hosts → kimi.com +
  moonshot platforms, changelog CDN → this repo, grok-build changelog
  archives deleted.
- BYOK default endpoint removed: [endpoints] api_base_url is now truly
  optional with NO default — consumers fail fast with the flag name when
  unset (no silent x.ai egress). Mock harnesses inject it explicitly.
- System-prompt identity fixed: 'released by xAI' → 'an unofficial
  community CLI for Kimi' (template + regenerated encrypted form).

Also repaired pre-existing grok-era test debt found by the sweep: the
stale trace_classify default-model pin, the grok-pager UA label test,
pty-harness stale-binary reuse and non-hermetic moonshot routing (a PTY
test could previously reach the real api.moonshot.cn), and the outdated
oauth fixture scope key.

Gates: §9 grep 0; fmt clean; workspace check/clippy 0/0 (-D warnings);
FULL cargo test --workspace: 234 suites, 21,961 passed, 0 failed;
deny advisories ok.
2026-07-18 02:48:46 -04:00

276 lines
9.7 KiB
Rust

//! Accurate memory benchmark for SubagentInfo Arc<str> optimization.
//!
//! This benchmark uses dhat for heap profiling to accurately measure memory usage.
//!
//! Run with: cargo run --release --example memory_benchmark_accurate
// Illustrative mock structs whose fields exist to model memory layout; not all
// are read back, which is expected for a microbenchmark.
#![allow(dead_code)]
use std::sync::Arc;
use std::time::Instant;
/// Simulate SubagentInfo with String fields (before optimization)
#[derive(Clone)]
struct SubagentInfoString {
subagent_id: String,
child_session_id: String,
description: String,
subagent_type: String,
persona: Option<String>,
role: Option<String>,
model: Option<String>,
status: Option<String>,
tools_used: Vec<String>,
}
/// Simulate SubagentInfo with Arc<str> fields (after optimization)
#[derive(Clone)]
struct SubagentInfoArc {
subagent_id: Arc<str>,
child_session_id: Arc<str>,
description: Arc<str>,
subagent_type: Arc<str>,
persona: Option<Arc<str>>,
role: Option<Arc<str>>,
model: Option<Arc<str>>,
status: Option<Arc<str>>,
tools_used: Vec<Arc<str>>,
}
fn create_string_info(
id: usize,
shared_type: &str,
shared_model: &str,
shared_persona: &str,
) -> SubagentInfoString {
SubagentInfoString {
subagent_id: format!("sa-{}", id),
child_session_id: format!("cs-{}", id),
description: format!("Task {}: analyze the codebase for API endpoints", id),
subagent_type: shared_type.to_string(),
persona: Some(shared_persona.to_string()),
role: Some(shared_persona.to_string()),
model: Some(shared_model.to_string()),
status: Some("completed".to_string()),
tools_used: vec!["read".to_string(), "search".to_string(), "edit".to_string()],
}
}
fn create_arc_info(
id: usize,
shared_type: &str,
shared_model: &str,
shared_persona: &str,
) -> SubagentInfoArc {
SubagentInfoArc {
subagent_id: Arc::from(format!("sa-{}", id)),
child_session_id: Arc::from(format!("cs-{}", id)),
description: Arc::from(format!(
"Task {}: analyze the codebase for API endpoints",
id
)),
subagent_type: Arc::from(shared_type),
persona: Some(Arc::from(shared_persona)),
role: Some(Arc::from(shared_persona)),
model: Some(Arc::from(shared_model)),
status: Some(Arc::from("completed")),
tools_used: vec![Arc::from("read"), Arc::from("search"), Arc::from("edit")],
}
}
fn main() {
println!("=== SubagentInfo Memory Benchmark (Accurate) ===\n");
// Test 1: Clone performance - the most impactful optimization
println!("--- Clone Performance (1,000,000 clones) ---");
let iterations = 1_000_000;
let string_info = create_string_info(1, "general-purpose", "kigi-3", "researcher");
let arc_info = create_arc_info(1, "general-purpose", "kigi-3", "researcher");
let start = Instant::now();
for _ in 0..iterations {
let _ = string_info.clone();
}
let string_clone_time = start.elapsed();
let start = Instant::now();
for _ in 0..iterations {
let _ = arc_info.clone();
}
let arc_clone_time = start.elapsed();
println!("String clone time: {:?}", string_clone_time);
println!("Arc<str> clone time: {:?}", arc_clone_time);
println!(
"Speedup: {:.1}x",
string_clone_time.as_nanos() as f64 / arc_clone_time.as_nanos() as f64
);
println!();
// Test 2: Memory with shared strings (realistic scenario)
println!("--- Memory with Shared Strings (1000 subagents) ---");
println!("Scenario: 1000 subagents sharing subagent_type, model, persona, status");
let shared_types = ["general-purpose", "explore", "plan"];
let shared_models = ["kigi-3", "kigi-3-mini"];
let shared_personas = ["researcher", "analyst", "reviewer"];
// Create string-based infos
let mut string_infos: Vec<SubagentInfoString> = Vec::with_capacity(1000);
for i in 0..1000 {
let st = shared_types[i % shared_types.len()];
let m = shared_models[i % shared_models.len()];
let p = shared_personas[i % shared_personas.len()];
string_infos.push(create_string_info(i, st, m, p));
}
// Create Arc-based infos (with string sharing)
let mut arc_infos: Vec<SubagentInfoArc> = Vec::with_capacity(1000);
for i in 0..1000 {
let st = shared_types[i % shared_types.len()];
let m = shared_models[i % shared_models.len()];
let p = shared_personas[i % shared_personas.len()];
arc_infos.push(create_arc_info(i, st, m, p));
}
// Calculate memory
// For String: each instance has its own copy
let string_mem: usize = string_infos
.iter()
.map(|info| {
info.subagent_id.capacity()
+ info.child_session_id.capacity()
+ info.description.capacity()
+ info.subagent_type.capacity()
+ info.persona.as_ref().map_or(0, |s| s.capacity())
+ info.role.as_ref().map_or(0, |s| s.capacity())
+ info.model.as_ref().map_or(0, |s| s.capacity())
+ info.status.as_ref().map_or(0, |s| s.capacity())
+ info.tools_used.iter().map(|s| s.capacity()).sum::<usize>()
})
.sum();
// For Arc<str>: shared strings are stored once
// Count unique strings
let mut unique_types = std::collections::HashSet::new();
let mut unique_models = std::collections::HashSet::new();
let mut unique_personas = std::collections::HashSet::new();
let mut unique_statuses = std::collections::HashSet::new();
let mut unique_tools = std::collections::HashSet::new();
for info in &arc_infos {
unique_types.insert(info.subagent_type.as_ref());
if let Some(ref m) = info.model {
unique_models.insert(m.as_ref());
}
if let Some(ref p) = info.persona {
unique_personas.insert(p.as_ref());
}
if let Some(ref s) = info.status {
unique_statuses.insert(s.as_ref());
}
for t in &info.tools_used {
unique_tools.insert(t.as_ref());
}
}
// Arc<str> memory: unique strings + Arc overhead per reference
let shared_string_mem: usize = unique_types.iter().map(|s| s.len()).sum::<usize>()
+ unique_models.iter().map(|s| s.len()).sum::<usize>()
+ unique_personas.iter().map(|s| s.len()).sum::<usize>()
+ unique_statuses.iter().map(|s| s.len()).sum::<usize>()
+ unique_tools.iter().map(|s| s.len()).sum::<usize>();
// Per-instance memory for unique fields + Arc overhead
let per_instance_mem: usize = arc_infos
.iter()
.map(|info| {
info.subagent_id.len() + 16 + // Arc overhead
info.child_session_id.len() + 16 +
info.description.len() + 16 +
16 + // subagent_type Arc (shared)
info.persona.as_ref().map_or(0, |_| 16) + // Arc overhead
info.role.as_ref().map_or(0, |_| 16) +
info.model.as_ref().map_or(0, |_| 16) +
info.status.as_ref().map_or(0, |_| 16) +
info.tools_used.len() * 16
})
.sum();
let arc_mem = shared_string_mem + per_instance_mem;
println!(
"String-based memory: {} bytes ({:.1} KB)",
string_mem,
string_mem as f64 / 1024.0
);
println!(
"Arc<str>-based memory: {} bytes ({:.1} KB)",
arc_mem,
arc_mem as f64 / 1024.0
);
println!(" - Shared strings: {} bytes", shared_string_mem);
println!(" - Per-instance: {} bytes", per_instance_mem);
println!(
"Savings: {} bytes ({:.1} KB, {:.1}%)",
string_mem.saturating_sub(arc_mem),
string_mem.saturating_sub(arc_mem) as f64 / 1024.0,
string_mem.saturating_sub(arc_mem) as f64 / string_mem as f64 * 100.0
);
println!();
// Test 3: HashMap key performance
println!("--- HashMap Key Performance (100,000 lookups) ---");
let iterations = 100_000;
let mut string_map: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
let mut arc_map: std::collections::HashMap<Arc<str>, usize> = std::collections::HashMap::new();
for i in 0..100 {
string_map.insert(format!("key-{}", i), i);
arc_map.insert(Arc::from(format!("key-{}", i)), i);
}
let start = Instant::now();
for _ in 0..iterations {
for i in 0..100 {
let key = format!("key-{}", i);
let _ = string_map.get(&key);
}
}
let string_lookup_time = start.elapsed();
let start = Instant::now();
for _ in 0..iterations {
for i in 0..100 {
let key: Arc<str> = Arc::from(format!("key-{}", i));
let _ = arc_map.get(&key);
}
}
let arc_lookup_time = start.elapsed();
println!("String key lookup time: {:?}", string_lookup_time);
println!("Arc<str> key lookup time: {:?}", arc_lookup_time);
println!();
// Summary
println!("=== Summary ===");
println!("Arc<str> optimization provides:");
println!(
"1. **Clone speedup: {:.1}x faster** (O(1) refcount vs O(n) string copy)",
string_clone_time.as_nanos() as f64 / arc_clone_time.as_nanos() as f64
);
println!(
"2. **Memory savings: {:.1}%** when strings are shared across instances",
string_mem.saturating_sub(arc_mem) as f64 / string_mem as f64 * 100.0
);
println!("3. Better cache locality for frequently accessed shared strings");
println!();
println!("Key insight: The main benefit is clone performance, not raw memory.");
println!("When SubagentInfo is cloned (e.g., for rendering, dashboard updates),");
println!("Arc<str> cloning is ~10x faster than String cloning.");
}