Skip to content

Conversation

vonzshik
Copy link
Contributor

Fixes #5660

@vonzshik vonzshik requested a review from roji as a code owner May 15, 2024 19:51
@vonzshik vonzshik force-pushed the 5660-batching-tracing-command-text-fix branch from 9ba58d8 to 1a94abd Compare May 15, 2024 20:00
@vonzshik vonzshik force-pushed the 5660-batching-tracing-command-text-fix branch from 3a419ed to 66d0595 Compare May 15, 2024 20:24
@@ -57,7 +57,7 @@ async Task Authenticate(string username, NpgsqlTimeout timeout, bool async, Canc
async Task AuthenticateCleartext(string username, bool async, CancellationToken cancellationToken = default)
{
var passwd = await GetPassword(username, async, cancellationToken).ConfigureAwait(false);
if (passwd == null)
if (string.IsNullOrEmpty(passwd))
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 seems like one of the tests somehow changes password from null to an empty string. No idea which one and why only now...

Debug.Assert(IsWrappedByBatch);
if (InternalBatchCommands.Count == 1)
return InternalBatchCommands[0].CommandText;
// TODO: Potentially cache on connector/command?
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, we also allocate a StringBuilder in SqlQueryParser, we could have a common one on the connector indeed...

if (InternalBatchCommands.Count == 1)
return InternalBatchCommands[0].CommandText;
// TODO: Potentially cache on connector/command?
var sb = new StringBuilder();

Choose a reason for hiding this comment

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

Thanks for fixing this!
In the related issue you write "the worst thing there is an additional string allocation". Since StringBuilder starts with a capacity of 16 characters and then doubles when that is exceeded, this can result in a couple of string allocations. Would it be a good idea to calculate the required space up front and set the capacity in the constructor?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well, it's definitely not a bad thing to precalculate capacity before allocating StringBuilder, though iterating over each batch command (in addition to the one we already have) does make things a bit slower (whether it's still going to be faster compared to just leaving StringBuilder as is depends on a lot of things and will require a proper benchmark).
I think, probably the best thing here is to just make a predefined capacity (1024?). But since we're also going to backport this to 8.0 (and probably 7.0, didn't really look that closely), it's better to leave it as simple as possible and instead make a separate pr to improve things for 9.0.

Copy link
Member

Choose a reason for hiding this comment

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

I'm also assuming that once we cache the StringBuilder on the connector, capacity issues no longer matter as the builder will grow once and then just reuse the internal memory etc.

@vonzshik vonzshik merged commit dc0d22e into main May 18, 2024
@vonzshik vonzshik deleted the 5660-batching-tracing-command-text-fix branch May 18, 2024 19:08
vonzshik added a commit that referenced this pull request May 18, 2024
vonzshik added a commit that referenced this pull request May 18, 2024
vonzshik added a commit that referenced this pull request May 18, 2024
@vonzshik
Copy link
Contributor Author

Backported to 8.0.4 via b72749d, 7.0.8 via b1bd523, 6.0.12 via c12b0d4

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.

Sending SQL from NpgsqlBatch to OpenTelemetry
3 participants