Skip to content

Commit 814e7da

Browse files
committed
address PR feedback from hiteshwpfmsft; cache the retrieved Length explicitly for optimal performance
1 parent dbafcd5 commit 814e7da

File tree

1 file changed

+10
-7
lines changed
  • src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging

1 file changed

+10
-7
lines changed

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Imaging/WriteableBitmap.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,8 @@ private void ValidateArrayAndGetInfo(Array sourceBuffer,
10851085

10861086
if (sourceBuffer.Rank == 1)
10871087
{
1088-
if (sourceBuffer.Length == 0)
1088+
int firstDimLength = sourceBuffer.GetLength(0);
1089+
if (firstDimLength == 0)
10891090
{
10901091
if (backwardsCompat)
10911092
{
@@ -1095,7 +1096,7 @@ private void ValidateArrayAndGetInfo(Array sourceBuffer,
10951096
}
10961097
else
10971098
{
1098-
throw new ArgumentException(SR.Image_InsufficientBuffer, "sourceBuffer");
1099+
throw new ArgumentException(SR.Image_InsufficientBuffer, nameof(sourceBuffer));
10991100
}
11001101
}
11011102
else
@@ -1104,14 +1105,16 @@ private void ValidateArrayAndGetInfo(Array sourceBuffer,
11041105
{
11051106
object exemplar = sourceBuffer.GetValue(0);
11061107
elementSize = Marshal.SizeOf(exemplar);
1107-
sourceBufferSize = sourceBuffer.Length * elementSize;
1108+
sourceBufferSize = firstDimLength * elementSize;
11081109
elementType = exemplar.GetType();
11091110
}
11101111
}
11111112
}
11121113
else if (sourceBuffer.Rank == 2)
11131114
{
1114-
if (sourceBuffer.GetLength(0) <= 0 || sourceBuffer.GetLength(1) <= 0)
1115+
int firstDimLength = sourceBuffer.GetLength(0);
1116+
int secondDimLength = sourceBuffer.GetLength(1);
1117+
if (firstDimLength == 0 || secondDimLength == 0)
11151118
{
11161119
if (backwardsCompat)
11171120
{
@@ -1121,16 +1124,16 @@ private void ValidateArrayAndGetInfo(Array sourceBuffer,
11211124
}
11221125
else
11231126
{
1124-
throw new ArgumentException(SR.Image_InsufficientBuffer, "sourceBuffer");
1127+
throw new ArgumentException(SR.Image_InsufficientBuffer, nameof(sourceBuffer));
11251128
}
11261129
}
11271130
else
11281131
{
11291132
checked
11301133
{
1131-
object exemplar = sourceBuffer.GetValue(0,0);
1134+
object exemplar = sourceBuffer.GetValue(0, 0);
11321135
elementSize = Marshal.SizeOf(exemplar);
1133-
sourceBufferSize = sourceBuffer.GetLength(0) * sourceBuffer.GetLength(1) * elementSize;
1136+
sourceBufferSize = (firstDimLength * secondDimLength) * elementSize;
11341137
elementType = exemplar.GetType();
11351138
}
11361139
}

0 commit comments

Comments
 (0)