Skip to content

fix: stream file rewrites with bounded memory#88

Open
leno23 wants to merge 1 commit into
dolph:mainfrom
leno23:fix/stream-replace-issue-8-v2
Open

fix: stream file rewrites with bounded memory#88
leno23 wants to merge 1 commit into
dolph:mainfrom
leno23:fix/stream-replace-issue-8-v2

Conversation

@leno23
Copy link
Copy Markdown

@leno23 leno23 commented May 30, 2026

Summary

Fixes #8 and #14. Rewrites use a bounded streaming buffer instead of loading entire files into memory, and only write when replacements actually occur.

Test plan

  • go test ./...

Made with Cursor

Replace the read-all/strings.Replace/write path with a windowed stream
rewriter that skips writing when no matches occur, fixing OOM on large
files and eliminating the redundant Contains pre-scan.

Fixes dolph#8
Fixes dolph#14
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: be1b89a6eb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread stream_replace.go
Comment on lines +80 to +82
split := len(data) - overlap
process := data[:split]
rest = append([]byte(nil), data[split:]...)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Preserve bytes that can start a boundary match

For non-final chunks, process still includes the byte at len(data)-len(find), so an occurrence that ends exactly at the chunk boundary is split after its first byte and can never be matched. For example, with a 6-byte find ending at offset 256KiB, the first byte is written in this chunk and the remaining 5 bytes are carried, leaving the occurrence unchanged and possibly returning changed=false if it was the only match. The streamed replacer needs to keep every byte that could begin a match until the next pass.

Useful? React with 👍 / 👎.

Comment thread file_handling.go
Comment on lines +117 to +118
tempName := filepath.Join(f.Dir(), RandomString(20))
out, err := os.OpenFile(tempName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, mode)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Create temp files without clobbering existing paths

This opens the randomly named temp path with O_TRUNC before knowing whether the file contains a match, so if the generated name already exists in the target directory, a no-op replacement can truncate that unrelated file and then remove it at the !changed cleanup path. The previous ReplaceContents returned before creating any temp file when there was no match; use an exclusive temp-file primitive such as os.CreateTemp/O_EXCL, and preferably defer temp creation until a replacement is actually needed.

Useful? React with 👍 / 👎.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Whole file is buffered into memory; large files cause OOM

1 participant