Skip to content

Commit 6c42d51

Browse files
authored
Optimize cast check for NpgsqlBatchCommandCollection and NpgsqlParame… (#4820)
1 parent 957c8de commit 6c42d51

3 files changed

Lines changed: 32 additions & 14 deletions

File tree

src/Npgsql/Internal/NpgsqlReadBuffer.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Buffers;
33
using System.Buffers.Binary;
44
using System.Diagnostics;
5-
using System.Diagnostics.CodeAnalysis;
65
using System.IO;
76
using System.Net.Sockets;
87
using System.Runtime.CompilerServices;
@@ -519,16 +518,16 @@ public int Read(Span<byte> output)
519518

520519
public ValueTask<int> ReadAsync(Memory<byte> output, CancellationToken cancellationToken = default)
521520
{
522-
if (output.Length == 0)
523-
return new ValueTask<int>(0);
524-
525521
var readFromBuffer = Math.Min(ReadBytesLeft, output.Length);
526522
if (readFromBuffer > 0)
527523
{
528524
new Span<byte>(Buffer, ReadPosition, readFromBuffer).CopyTo(output.Span);
529525
ReadPosition += readFromBuffer;
530526
return new ValueTask<int>(readFromBuffer);
531527
}
528+
529+
if (output.Length == 0)
530+
return new ValueTask<int>(0);
532531

533532
return ReadAsyncLong(this, output, cancellationToken);
534533

src/Npgsql/NpgsqlBatchCommandCollection.cs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Data.Common;
4+
using System.Diagnostics.CodeAnalysis;
5+
using System.Runtime.CompilerServices;
46

57
namespace Npgsql;
68

@@ -81,7 +83,7 @@ NpgsqlBatchCommand IList<NpgsqlBatchCommand>.this[int index]
8183
set => _list[index] = value;
8284
}
8385

84-
/// <inheritdoc/>
86+
/// <inheritdoc cref="IList{T}.this" />
8587
public new NpgsqlBatchCommand this[int index]
8688
{
8789
get => _list[index];
@@ -97,8 +99,16 @@ protected override void SetBatchCommand(int index, DbBatchCommand batchCommand)
9799
=> _list[index] = Cast(batchCommand);
98100

99101
static NpgsqlBatchCommand Cast(DbBatchCommand? value)
100-
=> value is NpgsqlBatchCommand c
101-
? c
102-
: throw new InvalidCastException(
103-
$"The value \"{value}\" is not of type \"{nameof(NpgsqlBatchCommand)}\" and cannot be used in this batch command collection.");
102+
{
103+
var castedValue = value as NpgsqlBatchCommand;
104+
if (castedValue is null)
105+
ThrowInvalidCastException(value);
106+
107+
return castedValue;
108+
}
109+
110+
[DoesNotReturn]
111+
static void ThrowInvalidCastException(DbBatchCommand? value) =>
112+
throw new InvalidCastException(
113+
$"The value \"{value}\" is not of type \"{nameof(NpgsqlBatchCommand)}\" and cannot be used in this batch command collection.");
104114
}

src/Npgsql/NpgsqlParameterCollection.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Data.Common;
66
using System.Diagnostics;
77
using System.Diagnostics.CodeAnalysis;
8+
using System.Runtime.CompilerServices;
89
using Npgsql.TypeMapping;
910
using NpgsqlTypes;
1011

@@ -565,7 +566,7 @@ public override void AddRange(Array values)
565566
throw new ArgumentNullException(nameof(values));
566567

567568
foreach (var parameter in values)
568-
Add(Cast(parameter) ?? throw new ArgumentException("Collection contains a null value.", nameof(values)));
569+
Add(Cast(parameter));
569570
}
570571

571572
/// <inheritdoc />
@@ -748,10 +749,18 @@ internal void ProcessParameters(TypeMapper typeMapper, bool validateValues, Comm
748749
internal PlaceholderType PlaceholderType { get; set; }
749750

750751
static NpgsqlParameter Cast(object? value)
751-
=> value is NpgsqlParameter p
752-
? p
753-
: throw new InvalidCastException(
754-
$"The value \"{value}\" is not of type \"{nameof(NpgsqlParameter)}\" and cannot be used in this parameter collection.");
752+
{
753+
var castedValue = value as NpgsqlParameter;
754+
if (castedValue is null)
755+
ThrowInvalidCastException(value);
756+
757+
return castedValue;
758+
}
759+
760+
[DoesNotReturn]
761+
static void ThrowInvalidCastException(object? value) =>
762+
throw new InvalidCastException(
763+
$"The value \"{value}\" is not of type \"{nameof(NpgsqlParameter)}\" and cannot be used in this parameter collection.");
755764
}
756765

757766
enum PlaceholderType

0 commit comments

Comments
 (0)