Skip to content

fix: Reduce memory and unnecessary map growth during scanning - #10652

Closed
gaby wants to merge 1 commit into
syncthing:mainfrom
gaby:refactor-rename-tracking-in-folder.go
Closed

gaby wants to merge 1 commit into
syncthing:mainfrom
gaby:refactor-rename-tracking-in-folder.go

Conversation

@gaby

@gaby gaby commented Apr 22, 2026

Copy link
Copy Markdown

Purpose

  • Reduce memory and unnecessary map growth during scanning by avoiding recording every scanned filename and only tracking names that actually participate in renames.
  • Prevent reuse of a rename source for multiple destinations within the same buffered update window and keep rename-consumption semantics correct while minimizing tracked state.
  • Make rename-tracking scoped to the batch/flush window so the map does not grow unbounded across long scans.

Description

  • In scanSubdirsChangedAndNew replaced eager insertion of scanned/self-matching names with a map that only tracks names consumed by successful rename detection, and added a comment documenting the intent.
  • After a pending FileInfoBatch flush boundary the rename-tracking map is reset by detecting updateBatch.Full() before FlushIfFull() and clearing the map when a flush occurs, with an inline correctness note.
  • In findRename removed the insertion on self-match (fi.Name == file.Name) and avoided marking non-deleted or otherwise unsuitable candidates as used, and only mark the source and destination names in the tracking map when a rename match is actually accepted.
  • Added TestFindRenameDoesNotReuseConsumedSource to model_test.go to assert that a rename source consumed for one destination is not reused for a second destination with identical content.

Testing

  • Ran go test ./lib/model -run 'TestRenameSameFile|TestFindRenameDoesNotReuseConsumedSource' and the tests passed (ok).
  • The change compiles and existing targeted rename-related tests exercised the modified logic successfully.

Note: This bug and fix was found and produced by Codex using gpt-5.3-codex.

Copilot AI review requested due to automatic review settings April 22, 2026 12:46
@github-actions github-actions Bot added the bug A problem with current functionality, as opposed to missing functionality (enhancement) label Apr 22, 2026
@gaby

gaby commented Apr 22, 2026

Copy link
Copy Markdown
Author

This was found while looking for reasons as to why syncthing is using +30GB of RAM when a shared folder has +1 million files.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR refines rename detection during folder scanning to reduce memory usage and avoid unbounded growth of in-memory tracking state across long scans, while preserving correct rename-consumption semantics.

Changes:

  • Limit rename tracking to only names involved in accepted rename matches (sources/destinations), rather than recording every scanned filename.
  • Reset rename-tracking state at FileInfoBatch flush boundaries to keep the tracking map scoped to the buffered update window.
  • Add a regression test ensuring a consumed rename source is not reused for multiple destinations within the same tracking window.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
lib/model/folder.go Adjusts rename tracking map semantics and resets it at batch flush boundaries; tightens when candidates are marked “used”.
lib/model/model_test.go Adds a test to assert rename-source consumption is not reused across multiple destinations with identical content.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread lib/model/folder.go
Comment on lines +683 to 686
// Tracks names participating in successful rename detection within the
// current update batch. We intentionally don't track every scanned file,
// only consumed rename sources and destinations.
alreadyUsedOrExisting := make(map[string]struct{})
Comment thread lib/model/folder.go
// Rename tracking only needs to deduplicate source/destination use
// while updates are buffered. Once flushed, persisted state keeps
// rename source consumption correct, so we can safely reset.
clear(alreadyUsedOrExisting)
calmh added a commit to calmh/syncthing that referenced this pull request Apr 22, 2026
Inspired by the report in syncthing#10652.

Signed-off-by: Jakob Borg <[email protected]>

@calmh calmh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the report. I'm writing a slightly different, non-AI, take on the fix.

@gaby

gaby commented Apr 22, 2026

Copy link
Copy Markdown
Author

Thanks for the report. I'm writing a slightly different, non-AI, take on the fix.

Sounds good to me! Feel free to close this one.

@calmh calmh closed this Apr 23, 2026
@gaby
gaby deleted the refactor-rename-tracking-in-folder.go branch July 8, 2026 13:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug A problem with current functionality, as opposed to missing functionality (enhancement)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants