Skip to content

Reduce VisionKit scan memory by streaming page processing#37

Open
Codex wants to merge 3 commits intomainfrom
codex/prevent-webview-restart-issue
Open

Reduce VisionKit scan memory by streaming page processing#37
Codex wants to merge 3 commits intomainfrom
codex/prevent-webview-restart-issue

Conversation

@Codex
Copy link
Copy Markdown
Contributor

@Codex Codex AI commented Apr 9, 2026

Lower-end iPhones were restarting the webview due to VNDocumentCameraViewController memory spikes during multi-page scans.

  • Streamed processing: process each scanned page individually with autoreleasepool, avoiding retention of all images simultaneously.
  • VNDocumentCameraScan path: new finishScan(with scan:) processes VisionKit scans page-by-page via a lightweight provider.
  • Simulator path: reuses the same streaming pipeline for sample images.
  • Limits respected: page count capped up front via limitedPageCount; response generation unchanged but now per-page.
private func finishScan(with scan: VNDocumentCameraScan) {
    processImages(pageCount: limitedPageCount(total: scan.pageCount)) { index in
        scan.imageOfPage(at: index)
    }
}

private func makeResults(pageCount: Int, imageProvider: @escaping (Int) -> UIImage) throws -> [String] {
    var results: [String] = []
    for index in 0 ..< pageCount {
        let result = try autoreleasepool {
            try process(image: imageProvider(index), at: index)
        }
        results.append(result)
    }
    return results
}

Co-authored-by: riderx <4084527+riderx@users.noreply.github.com>
@Codex Codex AI changed the title [WIP] Fix memory spike causing webview restart on iOS Reduce VisionKit scan memory by streaming page processing Apr 9, 2026
@Codex Codex AI requested a review from riderx April 9, 2026 07:34
@riderx riderx marked this pull request as ready for review April 9, 2026 07:34
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: 2d10fb5627

ℹ️ 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 on lines +849 to +853
private func finishScan(with scan: VNDocumentCameraScan) {
processImages(pageCount: limitedPageCount(total: scan.pageCount)) { index in
scan.imageOfPage(at: index)
}
}
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 Route VisionKit scans through the streaming processor

The new finishScan(with scan: VNDocumentCameraScan) path is never used, so the real-device VisionKit flow still materializes all pages first in didFinishWith scan ((0..<scan.pageCount).map(...)) and then calls the [UIImage] overload. That keeps the original peak-memory behavior this commit is trying to remove, so multi-page scans on low-memory devices can still trigger webview/process restarts. The delegate should call the VNDocumentCameraScan overload directly so pages are processed one-by-one.

Useful? React with 👍 / 👎.

@riderx
Copy link
Copy Markdown
Member

riderx commented Apr 9, 2026

@copilot fix the comment and mark them resolve

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 9, 2026

@copilot fix the comment and mark them resolve

Fixed in d5b5d12 — the VisionKit delegate now calls finishScan(with: scan) directly instead of materializing all page images first, so the streaming processor is used on real-device scans as intended.

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.

Discussion: iOS VNDocumentCameraViewController causes memory spike which restarts webview

3 participants