Repository navigation
refactor(core): significantly speed up slow revert functionality #20551
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
f0fb09f
fix(opencode): batch snapshot revert checkout
82b0843
refactor(opencode): clarify snapshot revert naming
042462c
refactor(opencode): clarify single-file revert path
3bd9964
refactor(opencode): clarify grouped file naming
a426048
refactor(opencode): clarify file chunk naming
37028d6
refactor(opencode): clarify checkout file naming
4cc2a39
refactor(opencode): clarify chunk path naming
8687240
refactor(opencode): clarify file chunk path naming
921c809
refactor(opencode): polish snapshot batching flow
d4fb8a0
Merge upstream/dev into revert-batching-dev
8222e72
Update comments in snapshot index.ts
natewill 06f5cce
refactor(opencode): simplify file path naming
9eaac9d
Merge branch 'dev' into revert-batching-dev
natewill File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -300,27 +300,94 @@ export namespace Snapshot { | |
| const revert = Effect.fnUntraced(function* (patches: Snapshot.Patch[]) { | ||
| return yield* locked( | ||
| Effect.gen(function* () { | ||
| const groupByHash = new Map<string, string[]>() | ||
| const seen = new Set<string>() | ||
| for (const item of patches) { | ||
| const filesForHash = groupByHash.get(item.hash) ?? [] | ||
| groupByHash.set(item.hash, filesForHash) | ||
| for (const file of item.files) { | ||
| if (seen.has(file)) continue | ||
| seen.add(file) | ||
| log.info("reverting", { file, hash: item.hash }) | ||
| const result = yield* git([...core, ...args(["checkout", item.hash, "--", file])], { | ||
| cwd: state.worktree, | ||
| }) | ||
| if (result.code !== 0) { | ||
| const rel = path.relative(state.worktree, file) | ||
| const tree = yield* git([...core, ...args(["ls-tree", item.hash, "--", rel])], { | ||
| filesForHash.push(file) | ||
| } | ||
| } | ||
|
|
||
| const revertSingle = Effect.fnUntraced(function* (hash: string, file: string) { | ||
| log.info("reverting", { file, hash }) | ||
| const result = yield* git([...core, ...args(["checkout", hash, "--", file])], { | ||
| cwd: state.worktree, | ||
| }) | ||
| if (result.code === 0) return | ||
| const rel = path.relative(state.worktree, file).replaceAll("\\", "/") | ||
| const tree = yield* git([...core, ...args(["ls-tree", hash, "--", rel])], { | ||
| cwd: state.worktree, | ||
| }) | ||
| if (tree.code === 0 && tree.text.trim()) { | ||
| log.info("file existed in snapshot but checkout failed, keeping", { file, hash }) | ||
| return | ||
| } | ||
| log.info("file did not exist in snapshot, deleting", { file, hash }) | ||
| yield* remove(file) | ||
| }) | ||
|
|
||
| for (const [hash, filesByHash] of groupByHash) { | ||
| for (let i = 0; i < filesByHash.length; i += 100) { //run git commands in 100 file chunks to prevent ARG_MAX errors | ||
| const fileChunk = filesByHash.slice(i, i + 100) | ||
| if (fileChunk.length === 1) { | ||
| yield* revertSingle(hash, fileChunk[0]!) | ||
| continue | ||
| } | ||
|
|
||
| const fileChunkPaths = fileChunk.map((file) => ({ | ||
| rel: path.relative(state.worktree, file).replaceAll("\\", "/"), | ||
| file, | ||
| })) | ||
| const tree = yield* git( | ||
| [...core, ...args(["ls-tree", "--name-only", hash, "--", ...fileChunkPaths.map((item) => item.rel)])], | ||
| { | ||
| cwd: state.worktree, | ||
| }, | ||
| ) | ||
|
|
||
| if (tree.code !== 0) { | ||
| log.info("batched ls-tree failed, falling back to single-file revert", { hash, files: fileChunk.length }) | ||
| for (const file of fileChunk) { | ||
| yield* revertSingle(hash, file) | ||
| } | ||
| continue | ||
| } | ||
|
|
||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
| const snapshotPaths = new Set(tree.text.trim().split("\n").map((item) => item.trim()).filter(Boolean)) | ||
| const filesToCheckout: string[] = [] | ||
| const missingFiles: string[] = [] | ||
| for (const item of fileChunkPaths) { | ||
| if (snapshotPaths.has(item.rel)) { | ||
| filesToCheckout.push(item.file) | ||
| continue | ||
| } | ||
| missingFiles.push(item.file) | ||
| } | ||
|
|
||
| if (filesToCheckout.length) { | ||
| log.info("reverting", { hash, files: filesToCheckout.length }) | ||
| const result = yield* git([...core, ...args(["checkout", hash, "--", ...filesToCheckout])], { | ||
| cwd: state.worktree, | ||
| }) | ||
| if (tree.code === 0 && tree.text.trim()) { | ||
| log.info("file existed in snapshot but checkout failed, keeping", { file }) | ||
| } else { | ||
| log.info("file did not exist in snapshot, deleting", { file }) | ||
| yield* remove(file) | ||
| if (result.code !== 0) { | ||
| log.info("batched checkout failed, falling back to single-file revert", { | ||
| hash, | ||
| files: filesToCheckout.length, | ||
| }) | ||
| for (const file of filesToCheckout) { | ||
| yield* revertSingle(hash, file) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (const file of missingFiles) { | ||
| log.info("file did not exist in snapshot, deleting", { file, hash }) | ||
| yield* remove(file) | ||
| } | ||
| } | ||
| } | ||
| }), | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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