Make array rental return after TryReadPlpUnicodeChars unconditional#2121
Merged
David-Engel merged 1 commit intodotnet:mainfrom Aug 15, 2023
Merged
Conversation
|
I tested this change in our application and the network request response times (end to end API calls) have improved a lot! BEFORE (Microsoft.Data.SqlClient v5.1.1) AFTER (Wraith2:fix-async-readsqlstring-rentals) -branch Awesome work @Wraith2 ! This change deserves a release! |
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## main #2121 +/- ##
==========================================
- Coverage 70.76% 70.65% -0.12%
==========================================
Files 306 306
Lines 62051 62053 +2
==========================================
- Hits 43911 43841 -70
- Misses 18140 18212 +72
Flags with carried forward coverage won't be shown. Click here to find out more.
☔ View full report in Codecov by Sentry. |
David-Engel
approved these changes
Aug 14, 2023
Contributor
David-Engel
left a comment
There was a problem hiding this comment.
Nice! LGTM.
Thanks, Wraith!
JRahnama
approved these changes
Aug 14, 2023
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


fixes #2120
In PR #1866 I introduced the ability for TryReadPlpUnicodeChars to use rented buffers to store the intermediate char[]'s that are needed before a string can be created. That change works well in sync calls but has a problem in async calls.
When used in async mode with a string that spans multiple packets the async replay mechanism causes the TryReadPlpUnicodeChars to be called repeatedly until it has all the data available in the buffered packets. An oversight in the method means that in the fail case a rented buffer is dropped to the GC. The rented buffers' size converge on the size of the requested string which means that as more packets are buffered into the snapshot larger and larger arrays will be allocated through the arraypool and then not returned.
This leads to performance which is no better than using newly allocated arrays and which contributes GC pressure (through the arrays it is dropping being on the LOH) leading to Gen2 GC which causes arraypools to be cleaned up which works against the use of rented buffers.
This change causes the caller of TryReadPlpUnicodeChars to return the rented array on failed paths as well as successful ones.
The test program is derived from the one provided in the issue. It uses the profiler api to get accurate results for small periods where trying to manually getting time snapshot would be error prone:
before this change:

after:

The GC time and run frequency are vastly reduced. This makes the overall repro faster.
The change is very simple. The difficulty was in finding and understanding it.
/cc @Havunen , @roji