Skip to content

F-021: perf(services): stream whitespace-stripped comparison via Iterator::eq#24

Open
Sephyi wants to merge 1 commit intodevelopmentfrom
audit/f-021-iterator-eq-whitespace
Open

F-021: perf(services): stream whitespace-stripped comparison via Iterator::eq#24
Sephyi wants to merge 1 commit intodevelopmentfrom
audit/f-021-iterator-eq-whitespace

Conversation

@Sephyi
Copy link
Copy Markdown
Owner

@Sephyi Sephyi commented Apr 22, 2026

Summary

perf(services): stream whitespace-stripped comparison via Iterator::eq.

Audit context

Closes audit entry F-021 from #3.

Verification

  • cargo fmt --check
  • cargo clippy --all-targets --all-features -- -D warnings
  • cargo test --all-targets

Note: one pre-existing test porcelain_exits_within_timeout_with_no_staged_changes is a known macOS cold-start flake that reproduces on unmodified development — unrelated to this change.

Three functions compared two strings with whitespace stripped by first
collecting both into full `String` allocations, then comparing equality.
Replace each with a streaming `Iterator::eq()` over filtered char
iterators. This drops two heap allocations per call and short-circuits
on the first differing character, which is the hot path when bodies
actually differ.

Affected sites:
- ContextBuilder::classify_span_change (src/services/context.rs)
- ContextBuilder::classify_span_change_rich (src/services/context.rs)
- AstDiffer::bodies_semantically_equal (src/services/differ.rs)

Behaviour is identical: `Iterator::eq` compares element-by-element and
returns true iff both iterators yield the same sequence of chars.
Existing tests (context, analyzer, languages suites) cover the
semantic-classification paths and continue to pass.

Closes audit entry F-021 from #3.
Copilot AI review requested due to automatic review settings April 22, 2026 19:51
@Sephyi Sephyi added the audit Codebase audit cleanup (issue #3) label Apr 22, 2026
@Sephyi Sephyi self-assigned this Apr 22, 2026
Copy link
Copy Markdown

Copilot AI left a comment

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 addresses audit finding F-021 by optimizing whitespace-stripped comparisons to avoid intermediate String allocations and enable early-exit on the first mismatch.

Changes:

  • Switch AstDiffer::bodies_semantically_equal to stream filtered char iterators and compare via Iterator::eq.
  • Switch ContextBuilder::{classify_span_change, classify_span_change_rich} to compare non-whitespace character streams via Iterator::eq instead of allocating full strings.

Reviewed changes

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

File Description
src/services/differ.rs Removes allocation in body semantic equality check by streaming chars().filter(...).eq(...).
src/services/context.rs Removes allocations in span whitespace-only detection by streaming filtered char iterators into Iterator::eq.

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

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

Labels

audit Codebase audit cleanup (issue #3)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants