Skip to content
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

Add array rental capability in TryReadPlpUnicodeChars #1866

Merged
merged 3 commits into from Feb 1, 2023

Conversation

Wraith2
Copy link
Contributor

@Wraith2 Wraith2 commented Dec 9, 2022

fixes #1864

This changes TryReadPlpUnicodeChars in two ways.

  1. it adds the ability to specify that the calling context can support a rented char[] buffer and adds support for using the arraypool inside the function
  2. it adds a check for the int.max/2 which is used as a sentinel value in cases where the length is not yet known. Adding this check prevents the attempted use of very very large array pool buffers for small strings and defers the rental of the buffer until the length calculation has been made.

The outcome of this is that in general use the arraypool will be used no intermediate buffers will be dropped to the GC. This does some with a minor performance loss in CPU time spend dealing with the array pool rental mechanics and clearing. See the fixed thread for the details on that. The perf loss is small enough that I think #1544 will probably make up for it.

/cc @masonwheeler

@codecov
Copy link

codecov bot commented Dec 9, 2022

Codecov Report

Base: 70.71% // Head: 70.21% // Decreases project coverage by -0.49% ⚠️

Coverage data is based on head (e845b1d) compared to base (c765d83).
Patch coverage: 60.86% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1866      +/-   ##
==========================================
- Coverage   70.71%   70.21%   -0.50%     
==========================================
  Files         292      292              
  Lines       61727    61745      +18     
==========================================
- Hits        43648    43355     -293     
- Misses      18079    18390     +311     
Flag Coverage Δ
addons 92.38% <ø> (ø)
netcore 73.38% <60.86%> (-0.81%) ⬇️
netfx 69.10% <ø> (-0.04%) ⬇️

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

Impacted Files Coverage Δ
.../netcore/src/Microsoft/Data/SqlClient/TdsParser.cs 73.36% <60.86%> (-0.03%) ⬇️
...c/Microsoft/Data/SqlClient/TdsParserStateObject.cs 23.30% <0.00%> (-22.77%) ⬇️
...ata/SqlClient/SqlConnectionTimeoutErrorInternal.cs 44.64% <0.00%> (-16.08%) ⬇️
...c/Microsoft/Data/SqlClient/TdsParserStateObject.cs 66.89% <0.00%> (-5.62%) ⬇️
...re/src/Microsoft/Data/SqlClient/SNI/SNINpHandle.cs 73.77% <0.00%> (-4.92%) ⬇️
...e/src/Microsoft/Data/SqlClient/SNI/SNITcpHandle.cs 62.62% <0.00%> (-2.32%) ⬇️
...Client/Reliability/Common/SqlRetryLogicProvider.cs 89.13% <0.00%> (-2.18%) ⬇️
...SqlClient/src/Microsoft/Data/Common/AdapterUtil.cs 68.24% <0.00%> (-0.83%) ⬇️
...c/Microsoft/Data/SqlClient/SqlConnectionFactory.cs 68.85% <0.00%> (-0.82%) ⬇️
...rc/Microsoft/Data/ProviderBase/DbConnectionPool.cs 85.19% <0.00%> (-0.67%) ⬇️
... and 13 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Contributor

@lcheunglci lcheunglci left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the optimization!

{
buff = new char[(int)Math.Min((int)stateObj._longlen, len)];
if (supportRentedBuff && len < 1073741824) // 1 Gib
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there ever a case where the buffer will be so short that renting won't be worthwhile?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How would you determine the answer to this question? Is it ever cheaper to new and GC an array than the overhead of renting? Possibly, I'd expect it to be a very low difference and almost impossible to quantify without a specific weird use case that is pathalogical along that route.

Copy link
Contributor

Choose a reason for hiding this comment

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

My understanding from other articles/comments about ArrayPool is that it isn't necessarily worthwhile for small arrays (on my machine it takes ~3x longer for arrays of length 10 for example).

Not sure if that matters here or not. You're probably correct that it won't be the bottleneck.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In terms of speed you're right. Speed isn't really a limiting factor for this library at the moment, network delay is. In general this will has a small speed penalty that would be quantifiable in a microbenchmark but very unlikely to become a bottleneck, it'll have an overall improvement in reducing GC'ed objects which will allow more time to be spend in user code then GC.

If a pathalogical case turns up then we can determine a more complex solution to avoid it.

// clear only the length that we know we have used.
cc.AsSpan(0, length).Clear();
ArrayPool<char>.Shared.Return(cc, clearArray: false);
cc = null;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this line necessary? Isn't cc about to go out of scope anyway?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It isn't strictly necessary in the current configuration. Imagine a future refactoring which hoists cc to an outer scope, with this in place it'll be cleared correctly in future. It costs very little.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Heavy GC thrashing in TdsParser
7 participants