chlog: walk the entry history lazily, out of limited mode

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.
This commit is contained in:
vhaudiquet authored and Valentin Haudiquet committed 2026-11-24 20:58:34 +01:00
1 parent 03e42f8432
commit 0a24db28bc
1 file changed
+35 -14
+35 -14
View File
@@ -547,33 +547,48 @@ fn find_version_tag(repo: &Repository, version: &str) -> Option<Oid> {
None None
} }
/// The commit summaries from HEAD down to (excluding) `boundary`, newest /// The commit summaries from HEAD down to the previous entry's boundary,
/// first, capped at `history_limit` entries (0 = unlimited) /// newest first, capped at `history_limit` entries (0 = unlimited)
fn commit_summaries_since( fn commit_summaries_since(
repo: &Repository, repo: &Repository,
boundary: Oid, boundary: Oid,
history_limit: usize, history_limit: usize,
) -> Result<Vec<String>, Box<dyn std::error::Error>> { ) -> Result<Vec<String>, Box<dyn std::error::Error>> {
// 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()?; let mut revwalk = repo.revwalk()?;
revwalk.set_sorting(Sort::TIME)?; revwalk.set_sorting(Sort::NONE)?;
revwalk.push_head()?; 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; let mut truncated = false;
for id in revwalk { for id in revwalk {
let id = id?; let id = id?;
let commit = repo.find_commit(id)?; let commit = repo.find_commit(id)?;
let message = commit.message().unwrap_or("").trim(); if id == boundary {
let summary = message.lines().next().unwrap_or("").to_string();
if summary.is_empty() {
continue; continue;
} }
if history_limit > 0 && commits.len() >= history_limit { if commit.time().seconds() < boundary_time {
truncated = true;
break; 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 { if truncated {
log::warn!( log::warn!(
@@ -581,6 +596,8 @@ fn commit_summaries_since(
the entry lists only the newest {history_limit}." 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) Ok(commits)
} }
@@ -602,15 +619,19 @@ fn changelog_in_repo(repo: &Repository, changelog_path: &Path) -> Option<std::pa
.map(|p| p.to_path_buf()) .map(|p| p.to_path_buf())
} }
/// The last commit that modified the file at `rel_path`, newest first, /// The last commit that modified the file at `rel_path`, walking at most
/// walking at most `history_limit` commits (0 = the whole history) /// `history_limit` commits (0 = the whole history)
fn last_commit_touching<'a>( fn last_commit_touching<'a>(
repo: &'a Repository, repo: &'a Repository,
rel_path: &Path, rel_path: &Path,
history_limit: usize, history_limit: usize,
) -> Result<Boundary<'a>, Box<dyn std::error::Error>> { ) -> Result<Boundary<'a>, Box<dyn std::error::Error>> {
// 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()?; let mut revwalk = repo.revwalk()?;
revwalk.set_sorting(Sort::TIME)?; revwalk.set_sorting(Sort::NONE)?;
if repo.head().is_err() { if repo.head().is_err() {
// Unborn HEAD: the repository has no commits yet // Unborn HEAD: the repository has no commits yet
return Ok(Boundary::None); return Ok(Boundary::None);