From 0a24db28bc7937449d52bb61496b04277c87d70c Mon Sep 17 00:00:00 2001 From: Valentin Haudiquet Date: Fri, 9 Oct 2026 17:00:27 +0200 Subject: [PATCH] chlog: walk the entry history lazily, out of limited mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The cap bounded the entry's output, but the walks still cost a full history: libgit2 puts a revwalk into limited mode when it sorts or hides, and prepare_walk then drains the whole graph (limit_list) before the first commit comes out — seconds on a kernel-sized tree on every run, however small the cap. Both walks now run unsorted and lazy, visiting only the entry window: the summaries are sorted by committer time afterwards, and the boundary is cut off by committer time, the boundary itself skipped. Same-second commits and merge branches with older dates can slip in or out around the boundary, which a draft entry edited before commit absorbs; the boundary-search window is likewise visited in walk order rather than newest-first. On a 200k-commit tree, chlog drops from a full-graph walk (~2s warm) to ~30ms on both the tag and no-tag paths. --- src/changelog.rs | 49 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 35 insertions(+), 14 deletions(-) diff --git a/src/changelog.rs b/src/changelog.rs index 3d1b0d0..fbcae14 100644 --- a/src/changelog.rs +++ b/src/changelog.rs @@ -547,33 +547,48 @@ fn find_version_tag(repo: &Repository, version: &str) -> Option { None } -/// The commit summaries from HEAD down to (excluding) `boundary`, newest -/// first, capped at `history_limit` entries (0 = unlimited) +/// The commit summaries from HEAD down to the previous entry's boundary, +/// newest first, capped at `history_limit` entries (0 = unlimited) fn commit_summaries_since( repo: &Repository, boundary: Oid, history_limit: usize, ) -> Result, Box> { + // Unsorted, no hide: both revwalk.sorting and revwalk.hide flip + // libgit2's walk into limited mode, where prepare_walk drains the + // whole graph (limit_list) before the first commit is emitted — a + // full-history walk on every run when the boundary is a release tag + // far behind HEAD. The unsorted walk is lazy, so only the collected + // window is visited; the summaries are sorted by committer time + // afterwards. Around the boundary this is approximate: same-second + // commits and merge branches carrying older dates can slip in or out, + // which a draft entry edited before commit absorbs. let mut revwalk = repo.revwalk()?; - revwalk.set_sorting(Sort::TIME)?; + revwalk.set_sorting(Sort::NONE)?; revwalk.push_head()?; - revwalk.hide(boundary)?; - let mut commits = Vec::new(); + let boundary_time = repo.find_commit(boundary)?.time().seconds(); + + let mut collected: Vec<(i64, String)> = Vec::new(); let mut truncated = false; for id in revwalk { let id = id?; let commit = repo.find_commit(id)?; - let message = commit.message().unwrap_or("").trim(); - let summary = message.lines().next().unwrap_or("").to_string(); - if summary.is_empty() { + if id == boundary { continue; } - if history_limit > 0 && commits.len() >= history_limit { - truncated = true; + if commit.time().seconds() < boundary_time { break; } - commits.push(summary); + let message = commit.message().unwrap_or("").trim(); + let summary = message.lines().next().unwrap_or(""); + if !summary.is_empty() { + collected.push((commit.time().seconds(), summary.to_string())); + if history_limit > 0 && collected.len() >= history_limit { + truncated = true; + break; + } + } } if truncated { log::warn!( @@ -581,6 +596,8 @@ fn commit_summaries_since( the entry lists only the newest {history_limit}." ); } + collected.sort_by_key(|&(time, _)| std::cmp::Reverse(time)); + let commits = collected.into_iter().map(|(_, summary)| summary).collect(); Ok(commits) } @@ -602,15 +619,19 @@ fn changelog_in_repo(repo: &Repository, changelog_path: &Path) -> Option( repo: &'a Repository, rel_path: &Path, history_limit: usize, ) -> Result, Box> { + // Unsorted, like commit_summaries_since: any sorting flips the walk + // into libgit2's limited mode, draining the whole graph before the + // first commit is visited. Within the `history_limit` window the + // visit order is walk order rather than newest-first. let mut revwalk = repo.revwalk()?; - revwalk.set_sorting(Sort::TIME)?; + revwalk.set_sorting(Sort::NONE)?; if repo.head().is_err() { // Unborn HEAD: the repository has no commits yet return Ok(Boundary::None);