Skip to content

Commit 454f5f7

Browse files
committed
Additional Allocation Optimizations
1 parent b54e807 commit 454f5f7

4 files changed

Lines changed: 86 additions & 353 deletions

File tree

NpgsqlRest/NpgsqlRestEndpoint.cs

Lines changed: 33 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,9 @@ await Options.ValidateParametersAsync(new ParameterValidationValues(
391391
var p = Options.AuthenticationOptions.ObfuscateAuthParameterLogValues && endpoint.IsAuth ?
392392
"***" :
393393
FormatParameterForLog(parameter);
394-
cmdLog!.AppendLine(string.Concat(
395-
"-- $",
396-
paramIndex.ToString(),
397-
" ", parameter.TypeDescriptor.OriginalType,
398-
" = ",
399-
p));
394+
cmdLog!.Append("-- $").Append(paramIndex).Append(' ')
395+
.Append(parameter.TypeDescriptor.OriginalType)
396+
.Append(" = ").AppendLine(p);
400397
}
401398
}
402399
else
@@ -475,12 +472,9 @@ await Options.ValidateParametersAsync(new ParameterValidationValues(
475472
var p = Options.AuthenticationOptions.ObfuscateAuthParameterLogValues && endpoint.IsAuth ?
476473
"***" :
477474
FormatParameterForLog(parameter);
478-
cmdLog!.AppendLine(string.Concat(
479-
"-- $",
480-
paramIndex.ToString(),
481-
" ", parameter.TypeDescriptor.OriginalType,
482-
" = ",
483-
p));
475+
cmdLog!.Append("-- $").Append(paramIndex).Append(' ')
476+
.Append(parameter.TypeDescriptor.OriginalType)
477+
.Append(" = ").AppendLine(p);
484478
}
485479
}
486480
}
@@ -575,12 +569,9 @@ await Options.ValidateParametersAsync(new ParameterValidationValues(
575569
var p = Options.AuthenticationOptions.ObfuscateAuthParameterLogValues && endpoint.IsAuth ?
576570
"***" :
577571
FormatParameterForLog(parameter);
578-
cmdLog!.AppendLine(string.Concat(
579-
"-- $",
580-
paramIndex.ToString(),
581-
" ", parameter.TypeDescriptor.OriginalType,
582-
" = ",
583-
p));
572+
cmdLog!.Append("-- $").Append(paramIndex).Append(' ')
573+
.Append(parameter.TypeDescriptor.OriginalType)
574+
.Append(" = ").AppendLine(p);
584575
}
585576

586577
continue;
@@ -671,12 +662,9 @@ await Options.ValidateParametersAsync(new ParameterValidationValues(
671662
var p = Options.AuthenticationOptions.ObfuscateAuthParameterLogValues && endpoint.IsAuth ?
672663
"***" :
673664
FormatParameterForLog(parameter);
674-
cmdLog!.AppendLine(string.Concat(
675-
"-- $",
676-
paramIndex.ToString(),
677-
" ", parameter.TypeDescriptor.OriginalType,
678-
" = ",
679-
p));
665+
cmdLog!.Append("-- $").Append(paramIndex).Append(' ')
666+
.Append(parameter.TypeDescriptor.OriginalType)
667+
.Append(" = ").AppendLine(p);
680668
}
681669

682670
continue;
@@ -832,12 +820,9 @@ await Options.ValidateParametersAsync(new ParameterValidationValues(
832820
var p = Options.AuthenticationOptions.ObfuscateAuthParameterLogValues && endpoint.IsAuth ?
833821
"***" :
834822
FormatParameterForLog(parameter);
835-
cmdLog!.AppendLine(string.Concat(
836-
"-- $",
837-
paramIndex.ToString(),
838-
" ", parameter.TypeDescriptor.OriginalType,
839-
" = ",
840-
p));
823+
cmdLog!.Append("-- $").Append(paramIndex).Append(' ')
824+
.Append(parameter.TypeDescriptor.OriginalType)
825+
.Append(" = ").AppendLine(p);
841826
}
842827
}
843828

@@ -1021,12 +1006,9 @@ await Options.ValidateParametersAsync(new ParameterValidationValues(
10211006
var p = Options.AuthenticationOptions.ObfuscateAuthParameterLogValues && endpoint.IsAuth ?
10221007
"***" :
10231008
FormatParameterForLog(parameter);
1024-
cmdLog!.AppendLine(string.Concat(
1025-
"-- $",
1026-
paramIndex.ToString(),
1027-
" ", parameter.TypeDescriptor.OriginalType,
1028-
" = ",
1029-
p));
1009+
cmdLog!.Append("-- $").Append(paramIndex).Append(' ')
1010+
.Append(parameter.TypeDescriptor.OriginalType)
1011+
.Append(" = ").AppendLine(p);
10301012
}
10311013

10321014
continue;
@@ -1117,12 +1099,9 @@ await Options.ValidateParametersAsync(new ParameterValidationValues(
11171099
var p = Options.AuthenticationOptions.ObfuscateAuthParameterLogValues && endpoint.IsAuth ?
11181100
"***" :
11191101
FormatParameterForLog(parameter);
1120-
cmdLog!.AppendLine(string.Concat(
1121-
"-- $",
1122-
paramIndex.ToString(),
1123-
" ", parameter.TypeDescriptor.OriginalType,
1124-
" = ",
1125-
p));
1102+
cmdLog!.Append("-- $").Append(paramIndex).Append(' ')
1103+
.Append(parameter.TypeDescriptor.OriginalType)
1104+
.Append(" = ").AppendLine(p);
11261105
}
11271106

11281107
continue;
@@ -1277,12 +1256,9 @@ await Options.ValidateParametersAsync(new ParameterValidationValues(
12771256
var p = Options.AuthenticationOptions.ObfuscateAuthParameterLogValues && endpoint.IsAuth ?
12781257
"***" :
12791258
FormatParameterForLog(parameter);
1280-
cmdLog!.AppendLine(string.Concat(
1281-
"-- $",
1282-
paramIndex.ToString(),
1283-
" ", parameter.TypeDescriptor.OriginalType,
1284-
" = ",
1285-
p));
1259+
cmdLog!.Append("-- $").Append(paramIndex).Append(' ')
1260+
.Append(parameter.TypeDescriptor.OriginalType)
1261+
.Append(" = ").AppendLine(p);
12861262
}
12871263
}
12881264

@@ -1474,6 +1450,9 @@ await Results.Problem(
14741450
await HttpClientTypeHandler.InvokeAllAsync(customHttpTypes, lookup, command.Parameters, cancellationToken);
14751451
}
14761452

1453+
// Cache the cache key string once to avoid repeated ToString() allocations
1454+
string? cacheKeyString = cacheKeys?.ToString();
1455+
14771456
// Handle reverse proxy endpoints
14781457
if (endpoint.IsProxy && Options.ProxyOptions.Enabled)
14791458
{
@@ -1483,7 +1462,7 @@ await Results.Problem(
14831462
endpoint.Cached is true &&
14841463
Options.CacheOptions.DefaultRoutineCache is not null)
14851464
{
1486-
if (Options.CacheOptions.DefaultRoutineCache.Get(endpoint, cacheKeys?.ToString()!, out var cachedProxyResponse))
1465+
if (Options.CacheOptions.DefaultRoutineCache.Get(endpoint, cacheKeyString!, out var cachedProxyResponse))
14871466
{
14881467
// Cache hit - return cached proxy response
14891468
if (cachedProxyResponse is Proxy.ProxyResponse cached)
@@ -1553,7 +1532,7 @@ endpoint.Cached is true &&
15531532
// Cache the proxy response if caching is enabled
15541533
if (endpoint.Cached is true && Options.CacheOptions.DefaultRoutineCache is not null)
15551534
{
1556-
Options.CacheOptions.DefaultRoutineCache.AddOrUpdate(endpoint, cacheKeys?.ToString()!, proxyResponse);
1535+
Options.CacheOptions.DefaultRoutineCache.AddOrUpdate(endpoint, cacheKeyString!, proxyResponse);
15571536
}
15581537
await Proxy.ProxyRequestHandler.WriteResponseAsync(context, proxyResponse, Options.ProxyOptions, cancellationToken);
15591538
return;
@@ -1750,7 +1729,7 @@ await command.ExecuteNonQueryWithRetryAsync(
17501729
object? valueResult;
17511730
if (Options.CacheOptions.DefaultRoutineCache is not null && endpoint.Cached is true)
17521731
{
1753-
if (Options.CacheOptions.DefaultRoutineCache.Get(endpoint, cacheKeys?.ToString()!, out valueResult) is false)
1732+
if (Options.CacheOptions.DefaultRoutineCache.Get(endpoint, cacheKeyString!, out valueResult) is false)
17541733
{
17551734
if (await PrepareCommand(connection, command, commandText, context, endpoint, true, cancellationToken) is false)
17561735
{
@@ -1769,7 +1748,7 @@ await command.ExecuteNonQueryWithRetryAsync(
17691748
if (await reader.ReadAsync(cancellationToken))
17701749
{
17711750
valueResult = descriptor.IsBinary ? reader.GetFieldValue<byte[]>(0) : reader.GetValue(0) as string;
1772-
Options.CacheOptions.DefaultRoutineCache.AddOrUpdate(endpoint, cacheKeys?.ToString()!, valueResult);
1751+
Options.CacheOptions.DefaultRoutineCache.AddOrUpdate(endpoint, cacheKeyString!, valueResult);
17731752
}
17741753
else
17751754
{
@@ -1886,7 +1865,7 @@ await command.ExecuteNonQueryWithRetryAsync(
18861865

18871866
if (canCacheRecordsAndSets)
18881867
{
1889-
if (Options.CacheOptions.DefaultRoutineCache!.Get(endpoint, cacheKeys?.ToString()!, out var cachedResult))
1868+
if (Options.CacheOptions.DefaultRoutineCache!.Get(endpoint, cacheKeyString!, out var cachedResult))
18901869
{
18911870
if (shouldLog)
18921871
{
@@ -2266,7 +2245,7 @@ await command.ExecuteNonQueryWithRetryAsync(
22662245
// Store in cache if within limits
22672246
if (shouldCache && cacheBuffer is not null)
22682247
{
2269-
Options.CacheOptions.DefaultRoutineCache?.AddOrUpdate(endpoint, cacheKeys?.ToString()!, cacheBuffer.ToString());
2248+
Options.CacheOptions.DefaultRoutineCache?.AddOrUpdate(endpoint, cacheKeyString!, cacheBuffer.ToString());
22702249
}
22712250
}
22722251
return;

benchmark_results_optimized.txt

Lines changed: 0 additions & 149 deletions
This file was deleted.

0 commit comments

Comments
 (0)