Skip to content

avoid Lock acquisition on ErrorCount/WarningCount when no errors exist#4157

Merged
paulmedynski merged 4 commits into
dotnet:mainfrom
SimonCropp:avoid-Lock-acquisition-on-ErrorCount/WarningCount-when-no-errors-exist
May 14, 2026
Merged

avoid Lock acquisition on ErrorCount/WarningCount when no errors exist#4157
paulmedynski merged 4 commits into
dotnet:mainfrom
SimonCropp:avoid-Lock-acquisition-on-ErrorCount/WarningCount-when-no-errors-exist

Conversation

@SimonCropp

Copy link
Copy Markdown
Contributor
  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.

  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.
@github-project-automation github-project-automation Bot moved this to To triage in SqlClient Board Apr 8, 2026
Copilot AI review requested due to automatic review settings April 8, 2026 02:58
@SimonCropp
SimonCropp requested a review from a team as a code owner April 8, 2026 02:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 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 _hasErrorOrWarning as volatile to support safe lock-free reads.
  • Add early-return fast paths in ErrorCount and WarningCount when _hasErrorOrWarning is false.

Comment thread src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs Outdated
Comment thread src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs Outdated
…arserStateObject.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings April 8, 2026 03:17
…arserStateObject.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

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

@paulmedynski paulmedynski added the Performance 📈 Issues that are targeted to performance improvements. label Apr 10, 2026
@paulmedynski paulmedynski self-assigned this Apr 10, 2026
@paulmedynski paulmedynski moved this from To triage to In review in SqlClient Board Apr 10, 2026

@paulmedynski paulmedynski left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have a look at the linked compiler errors.

Comment thread src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs Outdated
@github-project-automation github-project-automation Bot moved this from In review to In progress in SqlClient Board Apr 10, 2026
@github-actions

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the Stale The Issue or PR has become stale and will be automatically closed shortly if no activity occurs. label May 11, 2026
@paulmedynski paulmedynski added this to the 7.1.0-preview2 milestone May 11, 2026
@paulmedynski paulmedynski moved this from In progress to In review in SqlClient Board May 11, 2026
@paulmedynski

Copy link
Copy Markdown
Contributor

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 2 pipeline(s).

@paulmedynski paulmedynski removed the Stale The Issue or PR has become stale and will be automatically closed shortly if no activity occurs. label May 11, 2026
@paulmedynski
paulmedynski enabled auto-merge (squash) May 11, 2026 17:00
@codecov

codecov Bot commented May 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 64.46%. Comparing base (f217d82) to head (9f05e14).
⚠️ Report is 63 commits behind head on main.

❗ There is a different number of reports uploaded between BASE (f217d82) and HEAD (9f05e14). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (f217d82) HEAD (9f05e14)
CI-SqlClient 1 0
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     
Flag Coverage Δ
CI-SqlClient ?
PR-SqlClient-Project 64.46% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@priyankatiwari08 priyankatiwari08 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — clean optimization, volatile makes the existing lock-free read correct.

@paulmedynski
paulmedynski merged commit ab5324d into dotnet:main May 14, 2026
301 of 302 checks passed
@github-project-automation github-project-automation Bot moved this from In review to Done in SqlClient Board May 14, 2026
cheenamalhotra pushed a commit that referenced this pull request Jul 14, 2026
#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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Performance 📈 Issues that are targeted to performance improvements.

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants