Skip to content

filter: speed up large --include-from lists of directories - fixes #9014 - #10028

Merged
ncw merged 1 commit into
rclone:masterfrom
kaii9:perf-literal-filter-rules
Oct 14, 2026
Merged

ncw merged 1 commit into
rclone:masterfrom
kaii9:perf-literal-filter-rules

Conversation

@kaii9

@kaii9 kaii9 commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

What does this change do?

Speed up large lists of anchored directory filter rules, including --ignore-case, while preserving first-match precedence.

At 64 compiled rules, literal first-directory prefixes are indexed. Case-sensitive and case-insensitive rules have separate buckets; insensitive keys use Unicode simple folding consistent with Go's regexp matcher, including Kelvin sign, long s, and Greek sigma equivalents. Matching merges the exact bucket, folded bucket, and fallback rules in original order before evaluating the original regexps.

Small rule sets and expressions without a provable literal directory prefix retain the linear scan. Invalid UTF-8 in the first path component also uses the linear scan: regexp decodes invalid bytes as U+FFFD, which can match a literal U+FFFD even when a byte-based bucket lookup would miss it.

AddConfig copies mutable rule storage and index buckets so appending rules to a copied configuration cannot corrupt the original filter's index. Other configuration fields retain their existing shallow-copy behavior.

Linked issue

Fixes #9014

Changes following review

  • Use the requested user-facing commit subject: filter: speed up large --include-from lists of directories - fixes #9014.
  • Index anchored literal --ignore-case directory rules, with Unicode equivalence and mixed case-sensitive/insensitive precedence tests.
  • Correct the invalid UTF-8 fallback comment.
  • Fix the configuration-copy index regression found during follow-up testing.
  • Cover actual compiled rule counts of 63, 64, and 65, plus copied configurations, deduplication, reset, and rebuild.

Validation

  • RCLONE_CONFIG=/notfound go test -race ./fs/filter -count=1
  • golangci-lint run ./fs/filter (v2.14.0): 0 issues
  • Full make quicktest: 177 packages passed, with the freshly built rclone and flock on PATH and Docker available.
  • New ignore-case indexing tests fail on the previous implementation; the configuration-copy regression panics there and passes with the fix.

On Apple M1 Pro / Go 1.26, the same 100,000-directory ignore-case benchmark was run against the previous PR implementation and the revised code (-benchtime=500ms -count=2). Last-rule matches changed from 98–178 ms to 196–205 ns; misses changed from 90–96 ms to 341–498 ns. First-rule matches were already fast and changed from 134–143 ns to 296–351 ns. These are local filter-decision measurements, excluding remote I/O and rule loading, rather than end-to-end transfer results.

The index adds construction cost: at 10,000 ignore-case input patterns, cumulative allocation per NewFilter was approximately 267 MB versus 223 MB in the previous PR implementation. These are allocated bytes, not resident memory. Rule sets sharing one first directory or dominated by fallback expressions can still require a near-linear scan.

For new or changed backends

Not applicable; no backend changes.

Checklist

  • This change is trivial OR it has been discussed and agreed in the linked issue.
  • I have read the contribution guidelines.
  • (If I used AI tools to help write this code) I have read and understood the AI-assisted contributions guidance, and I have tested and take ownership of this change myself.
  • I have added tests for all changes in this PR if appropriate.
  • I have added documentation for the changes if appropriate.
  • All commit messages are in house style.
  • (Backend changes only) test_all passes for this backend and if submitting a new backend can provide a test account for the integration tester - see CONTRIBUTING.md.
  • This Pull Request is ready for review.

Checklist notes: the indexing approach and ignore-case extension are discussed in this PR's review. User documentation and backend integration tests are not applicable to this internal optimization. AI assistance was used for implementation and validation.

@kaii9
kaii9 requested a review from ncw as a code owner October 2, 2026 18:28

@ncw ncw 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.

This is a great fix - thank you :-)

The first line of the commit message goes into the changelog, so something users will recognise, like filter: speed up large --include-from lists of directories - fixes #9014, would be a little clearer.

I note that --ignore-case rules still use the linear scan. Can we deal with that in this PR too? Otherwise we'll have an awkward thing to explain to Windows users!

Comment thread fs/filter/rules.go Outdated
func (rs *rules) include(remote string) bool {
if len(rs.byDirectory) != 0 {
directory, _, _ := strings.Cut(remote, "/")
// The syntax package normalizes invalid UTF-8 in literals. Keep the

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.

As far as I can tell syntax.Parse rejects invalid UTF-8. The real reason for the fallback is that the regexp matcher decodes an invalid byte as U+FFFD, so a literal U+FFFD could match it. Could you reword the comment to say that?

@kaii9
kaii9 force-pushed the perf-literal-filter-rules branch from 2c14143 to 345fb1b Compare October 13, 2026 14:09
@kaii9 kaii9 changed the title filter: accelerate anchored literal directory rules filter: speed up large --include-from lists of directories - fixes #9014 Oct 13, 2026

@ncw ncw 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 fixups - this looks great now :-)

@ncw
ncw merged commit af364f5 into rclone:master Oct 14, 2026
9 checks passed
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.

rclone hangs indefinitely with large --include-from file (100k-300k patterns) and high concurrency settings

2 participants