Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

SqlClient enviroment change optimization #34573

Merged
merged 12 commits into from
Mar 29, 2019

Conversation

Wraith2
Copy link
Contributor

@Wraith2 Wraith2 commented Jan 13, 2019

When connecting to the sql server and during various ambient events environment changes are signalled back from the sql server to the client. These events are infrequent after login and handled extremely quickly. The current implementation always allocates new arrays for any binary data even though only a small number are acted on and it uses arrays to carry the change set leading to dropped short term allocations.

This PR changes the SqlEnvChange class to form it's own singly linked list, introduces a SqlEnvChange pool class based on a lock free pool from Roslyn and changes byte[] allocations to use the shared ArrayPool for backing arrays.

auto and manual tests pass. cc the usual people @afsanehr @keeratsingh @saurabh500

add SqlEnvChangePool to store a small number of env change objects for re-use
change SqlEnvChange to use rented arrays and uses to be aware of possible rental
@Wraith2
Copy link
Contributor Author

Wraith2 commented Jan 14, 2019

@dotnet-bot test Windows x64 Debug Build please

@karelz
Copy link
Member

karelz commented Mar 4, 2019

@afsanehr @tarikulsabbir @Gary-Zh @David-Engel can you please look at this PR too? This one is almost 2 months old :(

@AfsanehR-zz AfsanehR-zz added this to the 3.0 milestone Mar 5, 2019
@karelz
Copy link
Member

karelz commented Mar 18, 2019

@afsanehr @tarikulsabbir @Gary-Zh any update?

internal bool newBinRented;
internal bool oldBinRented;

public SqlEnvChange Next;
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's make all these public APIs internal.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, and I applied the class modifier guidelines to the file as well.

@@ -1812,7 +1812,18 @@ internal void OnEnvChange(SqlEnvChange rec)
break;

case TdsEnums.ENV_PROMOTETRANSACTION:
PromotedDTCToken = rec.newBinValue;
byte[] dtcToken = null;
if (rec.newBinRented)
Copy link
Contributor

Choose a reason for hiding this comment

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

This change is a bit unclear to me. Could you explain please? Why do we need to copy the newBinValue to dtcToken and then reference it to PromotedDTCToken? can't we directly copy to PromotedDTCToken?

Copy link
Contributor Author

@Wraith2 Wraith2 Mar 22, 2019

Choose a reason for hiding this comment

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

With this method PromotedDTCSession is never set until it has the correct contents. If we allocate and then copy into it directly there is a small window where it is set but the value is incorrect.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. Thanks for the explanation. What is the scenario which would cause the value to be incorrect?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the array contents neds to be kept the contents is copied in then assigned so there are only two states, null or array containing the correct data. otherwise there's a third state which is non-null with zeroed data.

@AfsanehR-zz
Copy link
Contributor

Thanks @Wraith2 . All the internal testing passes successfully. I re-ran the ci check on the PR and will merge it once green.

@AfsanehR-zz
Copy link
Contributor

@Wraith2 please get the latest changes here so the ci passes. Thanks.

@Wraith2
Copy link
Contributor Author

Wraith2 commented Mar 29, 2019

Done.

@AfsanehR-zz AfsanehR-zz merged commit 6977ddd into dotnet:master Mar 29, 2019
@Wraith2 Wraith2 deleted the sqlperf-envchange branch March 30, 2019 09:38
picenka21 pushed a commit to picenka21/runtime that referenced this pull request Feb 18, 2022
SqlClient enviroment change optimization

Commit migrated from dotnet/corefx@6977ddd
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
6 participants