Skip to content

Commit 6d09272

Browse files
unhappychoiceclaude
andcommitted
perf(fetcher/claude_code_usage): skip stale JSONL files by mtime
Claude names every session file `<uuid>.jsonl` — no date in the filename, so we can't do the same filename-prefix trick the codex side just got. Stat each file instead (~10µs per syscall) and skip the open+line-parse path when the mtime is older than the `since` cutoff with a one-day buffer. On one developer's `~/.claude/projects/`: 1,431 JSONL files, ~454 MB total. With `since = "today"` only the handful of sessions touched today get opened; cold-cache first paint drops from seconds to milliseconds. `since = "all"` keeps reading everything, same as before. The per-line `since.includes(ts)` filter remains the source of truth for event-level inclusion; this commit only prunes the file *list* up front. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent bfc728f commit 6d09272

1 file changed

Lines changed: 73 additions & 1 deletion

File tree

‎src/fetcher/claude/code_usage.rs‎

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use std::collections::HashMap;
1515
use std::fs;
1616
use std::io::{BufRead, BufReader};
17-
use std::path::PathBuf;
17+
use std::path::{Path, PathBuf};
1818

1919
use async_trait::async_trait;
2020
use chrono::{DateTime, Duration, NaiveDate, Utc};
@@ -285,13 +285,43 @@ fn read_sessions(dir: &PathBuf, since: &Since) -> Vec<UsageEvent> {
285285
let Ok(entries) = fs::read_dir(dir) else {
286286
return Vec::new();
287287
};
288+
// Claude session files are `<uuid>.jsonl` — no date in the filename. mtime is the only
289+
// cheap signal we have for "was this session touched in the window?", so stat each file
290+
// (one syscall, ~10µs) and skip the open+line-parse path when the file is stale.
291+
let cutoff = mtime_cutoff(since);
288292
entries
289293
.flatten()
290294
.filter(|e| e.path().extension().is_some_and(|x| x == "jsonl"))
295+
.filter(|e| mtime_within_cutoff(&e.path(), cutoff))
291296
.flat_map(|e| read_session_file(&e.path(), since))
292297
.collect()
293298
}
294299

300+
fn mtime_cutoff(since: &Since) -> Option<NaiveDate> {
301+
let today = Utc::now().date_naive();
302+
match since {
303+
Since::All => None,
304+
// One-day buffer covers sessions Claude touched late yesterday whose mtime races our
305+
// midnight comparison.
306+
Since::Today => Some(today - Duration::days(1)),
307+
Since::Days(n) => Some(today - Duration::days(*n)),
308+
}
309+
}
310+
311+
fn mtime_within_cutoff(path: &Path, cutoff: Option<NaiveDate>) -> bool {
312+
let Some(cutoff) = cutoff else {
313+
return true;
314+
};
315+
let Ok(meta) = fs::metadata(path) else {
316+
return true;
317+
};
318+
let Ok(mtime) = meta.modified() else {
319+
return true;
320+
};
321+
let mtime_date = DateTime::<Utc>::from(mtime).date_naive();
322+
mtime_date >= cutoff
323+
}
324+
295325
fn read_session_file(path: &PathBuf, since: &Since) -> Vec<UsageEvent> {
296326
let Ok(file) = fs::File::open(path) else {
297327
return Vec::new();
@@ -956,6 +986,48 @@ mod tests {
956986
assert!(t.value.contains("3k"), "value was {:?}", t.value);
957987
}
958988

989+
#[test]
990+
fn mtime_cutoff_is_none_for_all_and_one_day_buffer_for_today() {
991+
assert!(mtime_cutoff(&Since::All).is_none());
992+
let today = Utc::now().date_naive();
993+
assert_eq!(mtime_cutoff(&Since::Today), Some(today - Duration::days(1)));
994+
assert_eq!(
995+
mtime_cutoff(&Since::Days(7)),
996+
Some(today - Duration::days(7))
997+
);
998+
}
999+
1000+
#[test]
1001+
fn mtime_within_cutoff_keeps_freshly_written_files() {
1002+
// A file written now has mtime = now → should always be within any non-empty cutoff.
1003+
let tmp = tempdir().unwrap();
1004+
let fresh = tmp.path().join("recent.jsonl");
1005+
fs::write(&fresh, "").unwrap();
1006+
let cutoff = mtime_cutoff(&Since::Today);
1007+
assert!(mtime_within_cutoff(&fresh, cutoff));
1008+
}
1009+
1010+
#[test]
1011+
fn mtime_within_cutoff_keeps_unreadable_files_conservatively() {
1012+
// If we can't stat the path, the safe thing is to let `read_session_file` try and
1013+
// fail silently — the per-line `since.includes(ts)` filter is the source of truth.
1014+
let cutoff = Some(Utc::now().date_naive());
1015+
assert!(mtime_within_cutoff(
1016+
Path::new("/nonexistent/path/does/not/exist.jsonl"),
1017+
cutoff
1018+
));
1019+
}
1020+
1021+
#[test]
1022+
fn mtime_within_cutoff_lets_anything_through_when_cutoff_is_none() {
1023+
// Since::All path: cutoff is None, every file (even nonexistent) passes.
1024+
let tmp = tempdir().unwrap();
1025+
let f = tmp.path().join("any.jsonl");
1026+
fs::write(&f, "").unwrap();
1027+
assert!(mtime_within_cutoff(&f, None));
1028+
assert!(mtime_within_cutoff(Path::new("/nonexistent.jsonl"), None));
1029+
}
1030+
9591031
#[test]
9601032
fn day_grouping_buckets_by_calendar_date() {
9611033
let day1 = Utc.with_ymd_and_hms(2026, 5, 5, 10, 0, 0).unwrap();

0 commit comments

Comments
 (0)