AI search engine and MCP server for bots and agents. One Rust binary indexes code, docs, logs, JSONL/CSV and PDFs for BM25 full-text, vector and hybrid search, code search without grep, agent memory and RAG. Elasticsearch-compatible API; Corpus Hub serves hundreds of reference memory corpora.
Rust agent-memoryai-agentsbm25claude-codeclicode-searchcursordatabaseelasticsearchfull-text-searchhybrid-searchjsonllocal-firstmcpmcp-servermodel-context-protocolragrustsearch-enginevector-search
perf(api): kill the fixed per-request read tax — TCP_NODELAY + alloc-free cache key + O(1) audit ring
xerj-org21e925187677
diff --git a/engine/crates/xerj-api/src/main.rs b/engine/crates/xerj-api/src/main.rsindex e11f5532a..8662af4f3 100644--- a/engine/crates/xerj-api/src/main.rs+++ b/engine/crates/xerj-api/src/main.rs@@ -37,9 +37,11 @@ async fn main() -> anyhow::Result<()> {let native_listener = tokio::net::TcpListener::bind(&native_addr).await?;let es_listener = tokio::net::TcpListener::bind(&es_addr).await?;+ // TCP_NODELAY matches the production `xerj` binary (xerj-server) —+ // see the comment there for the per-request latency rationale.tokio::select! {- result = axum::serve(native_listener, native_router) => { result?; }- result = axum::serve(es_listener, es_router) => { result?; }+ result = axum::serve(native_listener, native_router).tcp_nodelay(true) => { result?; }+ result = axum::serve(es_listener, es_router).tcp_nodelay(true) => { result?; }}Ok(())diff --git a/engine/crates/xerj-engine/src/audit.rs b/engine/crates/xerj-engine/src/audit.rsindex f15398c64..d5060e566 100644--- a/engine/crates/xerj-engine/src/audit.rs+++ b/engine/crates/xerj-engine/src/audit.rs@@ -49,7 +49,11 @@ pub struct AuditEntry {}pub struct AuditLog {- buf: RwLock<Vec<AuditEntry>>,+ // VecDeque: the ring rotates on every append once at capacity;+ // `Vec::remove(0)` was an O(capacity) memmove (~750 KB at the 4096+ // default) on EVERY audited request — a measurable slice of the+ // fixed per-request tax on trivial reads. `pop_front` is O(1).+ buf: RwLock<std::collections::VecDeque<AuditEntry>>,capacity: usize,next_seq: AtomicU64,}@@ -57,7 +61,7 @@ pub struct AuditLog {impl AuditLog {pub fn new(capacity: usize) -> Arc<Self> {Arc::new(Self {- buf: RwLock::new(Vec::with_capacity(capacity)),+ buf: RwLock::new(std::collections::VecDeque::with_capacity(capacity)),capacity,next_seq: AtomicU64::new(1),})@@ -80,7 +84,7 @@ impl AuditLog {.unwrap_or(0);let prev_hash = {let buf = self.buf.read();- buf.last().map(|e| e.hash.clone()).unwrap_or_else(|| "0".repeat(64))+ buf.back().map(|e| e.hash.clone()).unwrap_or_else(|| "0".repeat(64))};let mut entry = AuditEntry {seq,@@ -95,13 +99,13 @@ impl AuditLog {entry.hash = compute_hash(&prev_hash, &entry);let mut buf = self.buf.write();if buf.len() >= self.capacity {- buf.remove(0);+ buf.pop_front();}- buf.push(entry);+ buf.push_back(entry);}pub fn snapshot(&self) -> Vec<AuditEntry> {- self.buf.read().clone()+ self.buf.read().iter().cloned().collect()}/// Walk the chain top-to-bottom. Returns Ok(()) if the chain isdiff --git a/engine/crates/xerj-engine/src/index.rs b/engine/crates/xerj-engine/src/index.rsindex 4a4336f0e..1a4d86a03 100644--- a/engine/crates/xerj-engine/src/index.rs+++ b/engine/crates/xerj-engine/src/index.rs@@ -3591,17 +3591,31 @@ impl Index {let cache_eligible = !request.profile&& request.search_after.is_none();let cache_key: Option<(u64, u64)> = if cache_eligible {- // Hash the request via its serde_json representation.- // Std DefaultHasher is fine for the cache-key use case- // (uniformity matters more than speed; the hash is only- // ~600 ns and still way below the slow-path floor).+ // Hash the request via its serde_json representation,+ // streaming the serializer output STRAIGHT INTO the hasher.+ // The previous implementation built a full `String` with+ // `serde_json::to_string` on every request just to hash it —+ // a per-request allocation + copy proportional to the body+ // size that showed up as part of the fixed per-request tax+ // on trivial reads.use std::collections::hash_map::DefaultHasher;- use std::hash::{Hash, Hasher};- let body_hash: Option<u64> = serde_json::to_string(request).ok().map(|s| {- let mut h = DefaultHasher::new();- s.hash(&mut h);- h.finish()- });+ use std::hash::Hasher;+ struct HasherWriter<'a>(&'a mut DefaultHasher);+ impl std::io::Write for HasherWriter<'_> {+ #[inline]+ fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {+ self.0.write(buf);+ Ok(buf.len())+ }+ #[inline]+ fn flush(&mut self) -> std::io::Result<()> {+ Ok(())+ }+ }+ let mut h = DefaultHasher::new();+ let body_hash: Option<u64> = serde_json::to_writer(HasherWriter(&mut h), request)+ .ok()+ .map(|_| h.finish());let v = self.dataset_version.load(Ordering::Acquire);body_hash.map(|h| (h, v))} else {diff --git a/engine/crates/xerj-server/src/main.rs b/engine/crates/xerj-server/src/main.rsindex 92c4dcae8..a035e2211 100644--- a/engine/crates/xerj-server/src/main.rs+++ b/engine/crates/xerj-server/src/main.rs@@ -328,7 +328,12 @@ async fn serve(router: Router, addr: SocketAddr, name: &'static str) -> Result<(info!("{name} listening on {addr}");+ // TCP_NODELAY: disable Nagle on accepted connections. Elasticsearch+ // (Netty) sets this by default; without it small request/response+ // round-trips can stall on the delayed-ACK/Nagle interaction, which+ // shows up as a fixed per-request latency tax on trivial reads.axum::serve(listener, router)+ .tcp_nodelay(true).with_graceful_shutdown(shutdown_signal()).await.with_context(|| format!("{name} serve error"))?;