Skip to content

fix: avoid unnecessary allocation in preprocess#82

Merged
eikopf merged 2 commits intomainfrom
fix/19-preprocess-cow-str
Mar 14, 2026
Merged

fix: avoid unnecessary allocation in preprocess#82
eikopf merged 2 commits intomainfrom
fix/19-preprocess-cow-str

Conversation

@eikopf
Copy link
Copy Markdown
Owner

@eikopf eikopf commented Mar 14, 2026

Summary

  • Change preprocess() return type from String to Cow<'_, str>
  • Add a fast path that returns Cow::Borrowed when the input has no BOM, no CRLF sequences, and no shebang -- the common case for typical source files
  • BOM removal uses a slice into the original input (still Borrowed), only CRLF normalization and shebang stripping allocate (Owned)
  • No caller changes needed: Cow<str> derefs to &str transparently
  • Add 4 new tests verifying the Borrowed/Owned behavior

Test plan

  • All 19 preprocess unit tests pass (including 4 new Cow-variant tests)
  • Full workspace test suite passes (570 tests across 12 suites)
  • Fuzz target compiles cleanly (Cow<str> is compatible with existing assert_eq! comparisons)

Closes #19

🤖 Generated with Claude Code

eikopf and others added 2 commits March 14, 2026 13:46
Return Cow<str> instead of String so that typical source files (no BOM,
no CRLF, no shebang) take a zero-allocation fast path. BOM removal also
avoids allocation since it is a simple slice operation.

Closes #19

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@eikopf eikopf merged commit ca0f443 into main Mar 14, 2026
4 checks passed
@eikopf eikopf deleted the fix/19-preprocess-cow-str branch March 14, 2026 12:49
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.

preprocess always allocates even when no transformation needed

1 participant