Skip to content

refactor(core): significantly speed up slow revert functionality#20551

Open
natewill wants to merge 13 commits intoanomalyco:devfrom
natewill:revert-batching-dev
Open

refactor(core): significantly speed up slow revert functionality#20551
natewill wants to merge 13 commits intoanomalyco:devfrom
natewill:revert-batching-dev

Conversation

@natewill
Copy link
Copy Markdown
Contributor

@natewill natewill commented Apr 1, 2026

Issue for this PR

Closes #20550

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

What does this PR do?

This PR speeds up Snapshot.revert for large revert operations.

Currently, revert handles files one at a time. In the worst case, that means up to 2n git subprocesses for n files: one git checkout per file, and one git ls-tree per file when checkout fails. Because each subprocess has overhead (10-20ms per file), revert gets very slow as file count grows.

This change batches files by snapshot hash and processes them in chunks. In the common case, that reduces the work from 2n git subprocesses to two batched git ls-tree and git checkout calls per 100 file chunks, while preserving the existing single-file path as a fallback if a batched operation fails.

In local testing on my machine, Snapshot.revert for a 300-file revert dropped from 8.72s to 96ms, which is about a 90x speedup.

This improves the file rollback portion of revert specifically. The remaining time in full session revert is still dominated by diff generation after rollback, which can be lengthy.

How did you verify your code works?

  • Verified locally with mixed revert scenarios covering modified existing files, deleted existing files, newly created files, and overlapping files across patches.
  • Verified the real session revert flow restores the worktree correctly for mixed file changes.
  • Ran bun typecheck in packages/opencode.

Screenshots / recordings

Not UI change

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 1, 2026

Hey! Your PR title perf(core): significantly speed up slow revert functionality doesn't follow conventional commit format.

Please update it to start with one of:

  • feat: or feat(scope): new feature
  • fix: or fix(scope): bug fix
  • docs: or docs(scope): documentation changes
  • chore: or chore(scope): maintenance tasks
  • refactor: or refactor(scope): code refactoring
  • test: or test(scope): adding or updating tests

Where scope is the package name (e.g., app, desktop, opencode).

See CONTRIBUTING.md for details.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Apr 1, 2026

The following comment was made by an LLM, it may be inaccurate:

Based on my search, the current PR (#20551) appears to be the primary result in most searches. The other related PRs found are:

However, none of these appear to be duplicates addressing the same optimization (batching by snapshot hash to speed up Snapshot.revert). PR #16103 touches on parallelizing git operations, but that's a different scope.

No duplicate PRs found

@natewill natewill changed the title perf(core): significantly speed up slow revert functionality refactor(core): significantly speed up slow revert functionality Apr 1, 2026
@natewill natewill force-pushed the revert-batching-dev branch from 0badd89 to 921c809 Compare April 1, 2026 21:03
}
}

const revertSingle = Effect.fnUntraced(function* (hash: string, file: string) {
Copy link
Copy Markdown
Contributor Author

@natewill natewill Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

revertSingle is the same functionality as the revert function that is in the current code. We use this as a fallback if the batching doesn't work for some reason or the snapshot hash only has one reverted file in it

}
continue
}

Copy link
Copy Markdown
Contributor Author

@natewill natewill Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here, we only checkout files that exist in this snapshot; files missing from the tree should be removed. The old code did the same thing but individually.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG} revert is very slow

2 participants