From 42bd1639427b851640ef85eff551677e3c89b10c Mon Sep 17 00:00:00 2001 From: Kent Overstreet Date: Sun, 22 Mar 2026 03:35:40 -0400 Subject: [PATCH] TailMessages: only check first 200 bytes for type field MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The type field is near the start of JSONL objects. Scanning the full object (potentially megabytes for tool_results) was the bottleneck — TwoWaySearcher dominated the profile. Co-Authored-By: Claude Opus 4.6 (1M context) --- poc-memory/src/transcript.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/poc-memory/src/transcript.rs b/poc-memory/src/transcript.rs index beaf08a..3919050 100644 --- a/poc-memory/src/transcript.rs +++ b/poc-memory/src/transcript.rs @@ -203,11 +203,12 @@ impl Iterator for TailMessages { let obj_bytes = &self.mmap[open..=close]; - // Quick byte check: skip objects that aren't user/assistant - // messages. Avoids serde-parsing megabyte tool_result objects. - let is_user = memchr::memmem::find(obj_bytes, b"\"type\":\"user\"").is_some(); + // The "type" field is near the start of top-level objects. + // Only check the first 200 bytes to avoid scanning megabyte objects. + let prefix = &obj_bytes[..obj_bytes.len().min(200)]; + let is_user = memchr::memmem::find(prefix, b"\"type\":\"user\"").is_some(); let is_assistant = !is_user - && memchr::memmem::find(obj_bytes, b"\"type\":\"assistant\"").is_some(); + && memchr::memmem::find(prefix, b"\"type\":\"assistant\"").is_some(); if !is_user && !is_assistant { continue; } let obj: Value = match serde_json::from_slice(obj_bytes) {