- 
                Notifications
    You must be signed in to change notification settings 
- Fork 316
Optimization: Use Environment.TickCount for SqlStatistics execution timing #3609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimization: Use Environment.TickCount for SqlStatistics execution timing #3609
Conversation
There was a problem hiding this 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 optimizes SQL execution timing performance by replacing expensive DateTime.UtcNow operations with lightweight Environment.TickCount calls in SqlStatistics. The change provides approximately 55x performance improvement (from ~57ns to ~1ns per timing operation) while maintaining adequate accuracy for database operation monitoring.
Key changes:
- Added ADP.FastTimerCurrent() method using Environment.TickCount for high-frequency timing operations
- Modified SqlStatistics execution timing to use nullable timestamp and wraparound handling for 32-bit integer overflow
- Updated timing units in dictionary output to maintain consistency with existing millisecond reporting
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description | 
|---|---|
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlStatistics.cs | Modified execution timing implementation to use nullable timestamps, added wraparound logic, and updated dictionary output format | 
| src/Microsoft.Data.SqlClient/src/Microsoft/Data/Common/AdapterUtil.cs | Added FastTimerCurrent() method using Environment.TickCount for optimized timing operations | 
        
          
                src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlStatistics.cs
          
            Show resolved
            Hide resolved
        
              
          
                src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlStatistics.cs
              
                Outdated
          
            Show resolved
            Hide resolved
        
      | /azp run | 
| Azure Pipelines successfully started running 2 pipeline(s). | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Questions about the wrapping algorithms and unit tests.
        
          
                src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlStatistics.cs
              
                Outdated
          
            Show resolved
            Hide resolved
        
      | 
 @dotnet-policy-service agree company="Microsoft" | 
…4489/SqlClient into perf/optimize-timer-current
| Commenter does not have sufficient privileges for PR 3609 in repo dotnet/SqlClient | 
        
          
                src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/CopyAllFromReader.cs
          
            Show resolved
            Hide resolved
        
      There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great!
        
          
                src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/SqlBulkCopyTest/CopyAllFromReader.cs
          
            Show resolved
            Hide resolved
        
      | /azp run | 
| Azure Pipelines successfully started running 2 pipeline(s). | 
| @SAY14489 - Thank you for the great PR, especially your first contribution. Performance improvements like this that are isolated and easy to verify are always welcome! | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work!
| 
 I’m trying to complete this PR. I just synced this branch with the up to date main since I couldn’t see the merge option, could you please start a pipeline run to fulfill the requirements? Thank You! | 
| /azp run | 
| Azure Pipelines successfully started running 2 pipeline(s). | 
| Codecov Report❌ Patch coverage is  
 
 Additional details and impacted files@@            Coverage Diff             @@
##             main    #3609      +/-   ##
==========================================
- Coverage   70.67%   62.50%   -8.18%     
==========================================
  Files         277      271       -6     
  Lines       61686    61373     -313     
==========================================
- Hits        43598    38360    -5238     
- Misses      18088    23013    +4925     
 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:
 | 
Description
This change optimizes the performance of SQL execution timing by replacing expensive DateTime operations with lightweight Environment.TickCount calls in SqlStatistics. The optimization targets the RequestExecutionTimer() and ReleaseAndUpdateExecutionTimer() methods, which are frequently called during database operations. This issue was exposed by profilers which showed that these two calling methods made up a significant portion of DateTime.UtcNow CPU usage.
Performance improvement:
^ Done Through BenchmarkDotNet
Benchmarking shows an approximately 55x performance improvement (from ~57ns to ~1ns per timing operation) while maintaining adequate accuracy for database operation monitoring.
Changes made:
Backwards compatibility:
Fully maintained. All existing APIs, return values, and units remain unchanged. The optimization is internal implementation only.
Scope:
This change specifically optimizes execution timing measurement without affecting timeout logic or other timing-dependent functionality in the codebase.
Issues
Performance optimization. Addresses inefficient use of DateTime.UtcNow.ToFileTimeUtc() for high-frequency elapsed time measurements.
Testing
Manual testing performed:
Accuracy results:
Testing showed the TickCount optimization maintains acceptable accuracy for duration timing, with error rates under 3% compared to StopWatch baseline for operations lasting 100ms or longer. It is a slight loss in accuracy compared to DateTime.UtcNow.ToFileTimeUtc(), which I believe is due to the lower precision, but for the use case and performance improvement of 55x, it seems like a worthwhile switch.