avoid Lock acquisition on ErrorCount/WarningCount when no errors exist#4157
Conversation
SimonCropp
commented
Apr 8, 2026
- _hasErrorOrWarning marked volatile (line 278) -- the field was already read without a lock at line 1212, so this makes the existing pattern and the new fast paths correct across all memory models.
- ErrorCount fast path -- returns 0 immediately when _hasErrorOrWarning is false, skipping the lock. Safe because _hasErrorOrWarning is only set to false under the lock when _errors is also nulled.
- WarningCount fast path -- same treatment.
1. _hasErrorOrWarning marked volatile (line 278) -- the field was already read without a lock at line 1212, so this makes the existing pattern and the new fast paths correct across all memory models. 2. ErrorCount fast path -- returns 0 immediately when _hasErrorOrWarning is false, skipping the lock. Safe because _hasErrorOrWarning is only set to false under the lock when _errors is also nulled. 3. WarningCount fast path -- same treatment.
There was a problem hiding this comment.
Pull request overview
This PR optimizes error/warning counting on TdsParserStateObject by adding a lock-free fast path when no errors/warnings are present, using a volatile flag to make the existing lock-free reads safe across memory models.
Changes:
- Mark
_hasErrorOrWarningasvolatileto support safe lock-free reads. - Add early-return fast paths in
ErrorCountandWarningCountwhen_hasErrorOrWarningisfalse.
…arserStateObject.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…arserStateObject.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
paulmedynski
left a comment
There was a problem hiding this comment.
Have a look at the linked compiler errors.
|
This pull request has been marked as stale due to inactivity for more than 30 days. If you would like to keep this pull request open, please provide an update or respond to any comments. Otherwise, it will be closed automatically in 7 days. |
|
/azp run |
|
Azure Pipelines successfully started running 2 pipeline(s). |
Codecov Report✅ All modified and coverable lines are covered by tests.
Additional details and impacted files@@ Coverage Diff @@
## main #4157 +/- ##
==========================================
- Coverage 74.37% 64.46% -9.92%
==========================================
Files 279 270 -9
Lines 42983 65785 +22802
==========================================
+ Hits 31969 42406 +10437
- Misses 11014 23379 +12365
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
priyankatiwari08
left a comment
There was a problem hiding this comment.
LGTM — clean optimization, volatile makes the existing lock-free read correct.
#4157) * avoid Lock acquisition on ErrorCount/WarningCount when no errors exist 1. _hasErrorOrWarning marked volatile (line 278) -- the field was already read without a lock at line 1212, so this makes the existing pattern and the new fast paths correct across all memory models. 2. ErrorCount fast path -- returns 0 immediately when _hasErrorOrWarning is false, skipping the lock. Safe because _hasErrorOrWarning is only set to false under the lock when _errors is also nulled. 3. WarningCount fast path -- same treatment. * Update src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * Update TdsParserStateObject.cs --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>