perf: Remove redundant Contains check in SecretObfuscator#1715
Conversation
SummaryRemoves unnecessary Contains check before calling StringBuilder.Replace in SecretObfuscator.Obfuscate. Critical IssuesPerformance Regression The removed Contains check (line 32) served as an important optimization. StringBuilder.Replace is an O(n) operation that always scans the entire string, even when the target does not exist. The string.Contains check was a quick O(n) pre-filter that avoided unnecessary work. Impact Analysis:
Example: With 10 secrets and a 1KB log string:
Recommendation: If the goal is to remove the conditional block for cleaner code, a single-line conditional would work: But the original 3-line version was clear and performant. Verdict |
The Contains check was redundant because StringBuilder.Replace already handles the case where the search string doesn't exist. Additionally, the check was done on the original input string while Replace operates on the StringBuilder, creating an inconsistency after the first replacement. Fixes #1549 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2cf1ead to
ca32899
Compare
There was a problem hiding this comment.
Pull request overview
This PR removes a redundant and buggy Contains check before calling StringBuilder.Replace in the SecretObfuscator.Obfuscate method. The change improves performance by eliminating an unnecessary string search operation and fixes a subtle bug where Contains was checking the original input string while Replace operated on the potentially-modified StringBuilder.
Key changes:
- Removed the
if (input.Contains(secret))conditional wrapper aroundstringBuilder.Replace() StringBuilder.Replace()already handles non-existent strings efficiently, making the check redundant
SummaryRemoves redundant Contains check before calling StringBuilder.Replace in SecretObfuscator. Critical IssuesCorrectness Issue Fixed ✅ The previous review raised performance concerns, but missed a critical correctness bug in the original code: Original code bug (lines 32-34): if (input.Contains(secret)) // ❌ Checks original input
{
stringBuilder.Replace(secret, LoggingConstants.SecretMask); // ✅ Operates on modified StringBuilder
}The problem: After the first secret replacement, Example scenario: This PR fixes this bug by removing the incorrect Contains check. Performance consideration: SuggestionsNone - the change correctly fixes a subtle but real bug. Previous Review StatusThe previous review correctly identified that this touches a hot path but missed the correctness issue. The performance concern is valid but secondary to correctness. StringBuilder.Replace is already optimized internally and won't allocate if the search string isn't found. Verdict✅ APPROVE - Fixes a correctness bug where secrets might not be obfuscated after earlier replacements |
Summary
Containscheck before callingStringBuilder.ReplaceReplacealready handles the case where the search string doesn't existContainschecked the original input butReplaceoperated on the modified StringBuilderChanges
Before:
After:
Test plan
Fixes #1549
🤖 Generated with Claude Code