8000 remove bunch of allocations caused by static arrays, fix ineffective … · dotnet/wpf@3fc9475 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3fc9475

Browse files
committed
remove bunch of allocations caused by static arrays, fix ineffective codegens
1 parent e8e3875 commit 3fc9475

File tree

24 files changed

+150
-199
lines changed

24 files changed

+150
-199
lines changed

src/Microsoft.DotNet.Wpf/src/Common/Graphics/wgx_exports.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ static MILGuidData()
344344

345345
internal static readonly Guid GUID_ContainerFormatWmp = new Guid(0x57a37caa, 0x367a, 0x4540, 0x91, 0x6b, 0xf1, 0x83, 0xc5, 0x09, 0x3a, 0x4b);
346346

347-
internal static readonly byte[] GUID_VendorMicrosoft = new byte[] { 0xca, 0x49, 0xe7, 0xf0, 0xef, 0xed, 0x89, 0x45, 0xa7, 0x3a, 0xee, 0xe, 0x62, 0x6a, 0x2a, 0x2b };
347+
internal static ReadOnlySpan<byte> GUID_VendorMicrosoft => [0xca, 0x49, 0xe7, 0xf0, 0xef, 0xed, 0x89, 0x45, 0xa7, 0x3a, 0xee, 0xe, 0x62, 0x6a, 0x2a, 0x2b];
348348
}
349349
#endregion // Guids
350350
}

src/Microsoft.DotNet.Wpf/src/PresentationBuildTasks/Microsoft/Build/Tasks/Windows/GenerateTemporaryTargetAssembly.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ private static void ReplaceImplicitImports(XmlDocument xmlProjectDoc)
820820
XmlNode previousNodeImportProps = null;
821821
XmlNode previousNodeImportTargets = null;
822822

823-
foreach (string sdk in sdks.Split(_semicolonChar).Select(i => i.Trim()))
823+
foreach (string sdk in sdks.Split(SemicolonChar).Select(i => i.Trim()))
824824
{
825825
// <Project Sdk="Microsoft.NET.Sdk">
826826
// <Project Sdk="My.Custom.Sdk/1.0.0">
@@ -952,7 +952,7 @@ static XmlNode CreateImportProjectSdkNode(XmlDocument xmlProjectDoc, string proj
952952

953953
private const string WPFTMP = "wpftmp";
954954

955-
private static readonly char[] _semicolonChar = new char[] { ';' };
955+
private const char SemicolonChar = ';';
956956

957957
#endregion Private Fields
958958

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/AlgoModule.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ private LZCodec LZCodec
457457
internal const byte DefaultBAACount = 8;
458458
internal const byte MaxBAACount = 10;
459459

460+
private static ReadOnlySpan<double> DefaultFirstSquareRoot => [1, 1, 1, 4, 9, 16, 36, 49];
460461

461-
private static readonly double[] DefaultFirstSquareRoot = { 1, 1, 1, 4, 9, 16, 36, 49};
462462
}
463463
}

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/InkSerializer.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -281,14 +281,13 @@ private void ExamineStreamHeader(Stream inkdata, out bool fBase64, out bool fGif
281281
return;
282282
}
283283

284-
private static readonly byte[] Base64HeaderBytes
285-
= new byte[]{(byte)'b',
286-
(byte)'a',
287-
(byte)'s',
288-
(byte)'e',
289-
(byte)'6',
290-
(byte)'4',
291-
(byte)':'};
284+
private static ReadOnlySpan<byte> Base64HeaderBytes => [(byte)'b',
285+
(byte)'a',
286+
(byte)'s',
287+
(byte)'e',
288+
(byte)'6',
289+
(byte)'4',
290+
(byte)':'];
292291

293292
#if OLD_ISF
294293
/// <summary>
@@ -2113,15 +2112,14 @@ private bool IsBase64Data(Stream data)
21132112
long currentPosition = data.Position;
21142113
try
21152114
{
2116-
byte[] isfBase64PrefixBytes = Base64HeaderBytes;
2117-
if (data.Length < isfBase64PrefixBytes.Length)
2115+
if (data.Length < Base64HeaderBytes.Length)
21182116
{
21192117
return false;
21202118
}
21212119

2122-
for (int x = 0; x < isfBase64PrefixBytes.Length; x++)
2120+
for (int x = 0; x < Base64HeaderBytes.Length; x++)
21232121
{
2124-
if ((byte)data.ReadByte() != isfBase64PrefixBytes[x])
2122+
if ((byte)data.ReadByte() != Base64HeaderBytes[x])
21252123
{
21262124
return false;
21272125
}

src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Shaping/Positioning.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,12 +224,14 @@ internal struct ValueRecordTable
224224
const ushort XAdvanceDeviceFlag = 0x0040;
225225
const ushort YAdvanceDeviceFlag = 0x0080;
226226

227-
private static ushort[] BitCount =
228-
new ushort[16] { 0, 2, 2, 4, 2, 4, 4, 6, 2, 4, 4, 6, 4, 6, 6, 8 };
227+
private static ReadOnlySpan<ushort> BitCount => [0, 2, 2, 4,
228+
2, 4, 4, 6,
229+
2, 4, 4, 6,
230+
4, 6, 6, 8];
229231

230232
public static ushort Size(ushort Format)
231233
{
232-
return (ushort)(BitCount[Format&0x000F]+BitCount[(Format>>4)&0x000F]);
234+
return (ushort)(BitCount[Format & 0x000F] + BitCount[(Format >> 4) & 0x000F]);
233235
}
234236

235237
public void AdjustPos( FontTable Table,

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/Cursor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ private bool IsValidCursorType(CursorType cursorType)
354354

355355
private SafeHandle _cursorHandle;
356356

357-
private static readonly int[] CursorTypes = {
357+
private static ReadOnlySpan<int> CursorTypes => [
358358
0, // None
359359
NativeMethods.IDC_NO,
360360
NativeMethods.IDC_ARROW,
@@ -383,6 +383,6 @@ private bool IsValidCursorType(CursorType cursorType)
383383
NativeMethods.IDC_ARROW + 149, // ScrollSWCursor
384384
NativeMethods.IDC_ARROW + 150, // ScrollSECursor
385385
NativeMethods.IDC_ARROW + 151 // ArrowCDCursor
386-
};
386+
];
387387
}
388388
}

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/EllipseGeometry.cs

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ internal static Rect GetBoundsHelper(Pen pen, Matrix worldMatrix, Point center,
182182
Point * pPoints = stackalloc Point[(int)c_pointCount];
183183
EllipseGeometry.GetPointList(pPoints, c_pointCount, center, radiusX, radiusY);
184184

185-
fixed (byte *pTypes = EllipseGeometry.s_roundedPathTypes)
185+
fixed (byte *pTypes = RoundedPathTypes) //Merely retrieves the pointer to static PE data, no actual pinning occurs
186186
{
187187
rect = Geometry.GetBoundsHelper(
188188
pen,
@@ -209,7 +209,7 @@ internal override bool ContainsInternal(Pen pen, Point hitPoint, double toleranc
209209
Point *pPoints = stackalloc Point[(int)GetPointCount()];
210210
EllipseGeometry.GetPointList(pPoints, GetPointCount(), Center, RadiusX, RadiusY);
211211

212-
fixed (byte* pTypes = GetTypeList())
212+
fixed (byte* pTypes = RoundedPathTypes) //Merely retrieves the pointer to static PE data, no actual pinning occurs
213213
{
214214
return ContainsInternal(
215215
pen,
@@ -383,9 +383,8 @@ private unsafe static void GetPointList(Point * points, uint pointsCount, Point
383383
points[8].Y = points[9].Y = points[10].Y = center.Y - radiusY;
384384
}
385385

386-
private byte[] GetTypeList() { return s_roundedPathTypes; }
387-
private uint GetPointCount() { return c_pointCount; }
388-
private uint GetSegmentCount() { return c_segmentCount; }
386+
private static uint GetPointCount() { return c_pointCount; }
387+
private static uint GetSegmentCount() { return c_segmentCount; }
389388

390389
#region Static Data
391390

@@ -396,18 +395,16 @@ private unsafe static void GetPointList(Point * points, uint pointsCount, Point
396395
private const UInt32 c_pointCount = 13;
397396

398397
private const byte c_smoothBezier = (byte)MILCoreSegFlags.SegTypeBezier |
399-
(byte)MILCoreSegFlags.SegIsCurved |
400-
(byte)MILCoreSegFlags.SegSmoothJoin;
401-
402-
private static readonly byte[] s_roundedPathTypes = {
403-
(byte)MILCoreSegFlags.SegTypeBezier |
404-
(byte)MILCoreSegFlags.SegIsCurved |
405-
(byte)MILCoreSegFlags.SegSmoothJoin |
406-
(byte)MILCoreSegFlags.SegClosed,
407-
c_smoothBezier,
408-
c_smoothBezier,
409-
c_smoothBezier
410-
};
398+
(byte)MILCoreSegFlags.SegIsCurved |
399+
(byte)MILCoreSegFlags.SegSmoothJoin;
400+
401+
private static ReadOnlySpan<byte> RoundedPathTypes => [(byte)MILCoreSegFlags.SegTypeBezier |
402+
(byte)MILCoreSegFlags.SegIsCurved |
403+
(byte)MILCoreSegFlags.SegSmoothJoin |
404+
(byte)MILCoreSegFlags.SegClosed,
405+
c_smoothBezier,
406+
c_smoothBezier,
407+
c_smoothBezier];
411408

412409
#endregion
413410
}

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/LineGeometry.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ internal static Rect GetBoundsHelper(Pen pen, Matrix worldMatrix, Point pt1, Poi
118118
pPoints[0] = pt1;
119119
pPoints[1] = pt2;
120120

121-
fixed (byte *pTypes = LineGeometry.s_lineTypes)
121+
fixed (byte *pTypes = LineTypes) //Merely retrieves the pointer to static PE data, no actual pinning occurs
122122
{
123123
return Geometry.GetBoundsHelper(
124124
pen,
@@ -144,7 +144,7 @@ internal override bool ContainsInternal(Pen pen, Point hitPoint, double toleranc
144144
pPoints[0] = StartPoint;
145145
pPoints[1] = EndPoint;
146146

147-
fixed (byte* pTypes = GetTypeList())
147+
fixed (byte* pTypes = GetTypeList()) //Merely retrieves the pointer to static PE data, no actual pinning occurs
148148
{
149149
return ContainsInternal(
150150
pen,
@@ -185,13 +185,13 @@ public override double GetArea(double tolerance, ToleranceType type)
185185
return 0.0;
186186
}
187187

188-
private byte[] GetTypeList() { return s_lineTypes; }
188+
private static ReadOnlySpan<byte> GetTypeList() => LineTypes;
189189

190-
private static byte[] s_lineTypes = new byte[] { (byte)MILCoreSegFlags.SegTypeLine };
190+
private static ReadOnlySpan<byte> LineTypes => [(byte)MILCoreSegFlags.SegTypeLine];
191191

192-
private uint GetPointCount() { return c_pointCount; }
192+
private static uint GetPointCount() { return c_pointCount; }
193193

194-
private uint GetSegmentCount() { return c_segmentCount; }
194+
private static uint GetSegmentCount() { return c_segmentCount; }
195195

196196
/// <summary>
197197
/// GetAsPathGeometry - return a PathGeometry version of this Geometry

src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/RectangleGeometry.cs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ internal static Rect GetBoundsHelper(Pen pen, Matrix worldMatrix, Rect rect, dou
223223
Point * pPoints = stackalloc Point[(int)pointCount];
224224
RectangleGeometry.GetPointList(pPoints, pointCount, rect, radiusX, radiusY);
225225

226-
fixed (byte *pTypes = RectangleGeometry.GetTypeList(rect, radiusX, radiusY))
226+
fixed (byte *pTypes = GetTypeList(rect, radiusX, radiusY)) //Merely retrieves the pointer to static PE data, no actual pinning occurs
227227
{
228228
boundingRect = Geometry.GetBoundsHelper(
229229
pen,
@@ -262,7 +262,7 @@ internal override bool ContainsInternal(Pen pen, Point hitPoint, double toleranc
262262
Point *pPoints = stackalloc Point[(int)pointCount];
263263
RectangleGeometry.GetPointList(pPoints, pointCount, rect, radiusX, radiusY);
264264

265-
fixed (byte* pTypes = GetTypeList(rect, radiusX, radiusY))
265+
fixed (byte* pTypes = GetTypeList(rect, radiusX, radiusY)) //Merely retrieves the pointer to static PE data, no actual pinning occurs
266266
{
267267
return ContainsInternal(
268268
pen,
@@ -510,19 +510,19 @@ private unsafe static void GetPointList(Point * points, uint pointsCount, Rect r
510510
}
511511
}
512512

513-
private static byte[] GetTypeList(Rect rect, double radiusX, double radiusY)
513+
private static ReadOnlySpan<byte> GetTypeList(Rect rect, double radiusX, double radiusY)
514514
{
515515
if (rect.IsEmpty)
516516
{
517517
return null;
518518
}
519519
else if (IsRounded(radiusX, radiusY))
520520
{
521-
return s_roundedPathTypes;
521+
return RoundedPathTypes;
522522
}
523523
else
524524
{
525-
return s_squaredPathTypes;
525+
return SquaredPathTypes;
526526
}
527527
}
528528

@@ -610,30 +610,28 @@ public override bool MayHaveCurves()
610610

611611
static private byte smoothLine = (byte)MILCoreSegFlags.SegTypeLine | (byte)MILCoreSegFlags.SegSmoothJoin;
612612

613-
static private byte[] s_roundedPathTypes = {
614-
(byte)MILCoreSegFlags.SegTypeBezier |
613+
private static ReadOnlySpan<byte> RoundedPathTypes => new byte[] {
614+
(byte)MILCoreSegFlags.SegTypeBezier |
615615
(byte)MILCoreSegFlags.SegIsCurved |
616-
(byte)MILCoreSegFlags.SegSmoothJoin |
616+
(byte)MILCoreSegFlags.SegSmoothJoin |
617617
(byte)MILCoreSegFlags.SegClosed,
618-
smoothLine,
618+
smoothLine,
619619
smoothBezier,
620-
smoothLine,
620+
smoothLine,
621621
smoothBezier,
622-
smoothLine,
622+
smoothLine,
623623
smoothBezier,
624-
smoothLine
624+
smoothLine
625625
};
626626

627627
// Squared
628628
private const UInt32 c_squaredSegmentCount = 4;
629629
private const UInt32 c_squaredPointCount = 5;
630630

631-
private static readonly byte[] s_squaredPathTypes = {
632-
(byte)MILCoreSegFlags.SegTypeLine | (byte)MILCoreSegFlags.SegClosed,
633-
(byte)MILCoreSegFlags.SegTypeLine,
634-
(byte)MILCoreSegFlags.SegTypeLine,
635-
(byte)MILCoreSegFlags.SegTypeLine
636-
};
631+
private static ReadOnlySpan<byte> SquaredPathTypes => [(byte)MILCoreSegFlags.SegTypeLine | (byte)MILCoreSegFlags.SegClosed,
632+
(byte)MILCoreSegFlags.SegTypeLine,
633+
(byte)MILCoreSegFlags.SegTypeLine,
634+
(byte)MILCoreSegFlags.SegTypeLine];
637635

638636
#endregion
639637
}

src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/Anchoring/LocatorManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1370,7 +1370,7 @@ private class ResolvingLocatorState
13701370
private Hashtable _selectionProcessors;
13711371

13721372
// Potential separators for subtree processor class names
1373-
private static readonly Char[] Separators = new Char[] { ',', ' ', ';' };
1373+
private static ReadOnlySpan<char> Separators => [',', ' ', ';'];
13741374

13751375
// Optional store, used if passed in, otherwise we grab the service's store
13761376
private AnnotationStore _internalStore = null;

src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/PtsHost/ListMarkerSourceInfo.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,8 +398,7 @@ private static bool IsKnownIndexMarkerStyle(TextMarkerStyle markerStyle)
398398
new string[] { "M??", "CDM", "XLC", "IVX" }
399399
};
400400

401-
private static int[] RomanNumericSizeIncrements =
402-
new int[] { 1, 2, 3, 8, 18, 28, 38, 88, 188, 288, 388, 888 };
401+
private static ReadOnlySpan<int> RomanNumericSizeIncrements => [1, 2, 3, 8, 18, 28, 38, 88, 188, 288, 388, 888];
403402
}
404403
}
405404

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGridLengthConverter.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ private static DataGridLength ConvertFromString(string s, CultureInfo cultureInf
244244
if (goodString.EndsWith(unitString, StringComparison.Ordinal))
245245
{
246246
strLenUnit = unitString.Length;
247-
unitFactor = _pixelUnitFactors[i];
247+
unitFactor = PixelUnitFactors[i];
248248
break;
249249
}
250250
}
@@ -281,11 +281,11 @@ private static DataGridLength ConvertFromString(string s, CultureInfo cultureInf
281281
private static string[] _nonStandardUnitStrings = { "in", "cm", "pt" };
282282

283283
// These are conversion factors to transform other units to pixels
284-
private static double[] _pixelUnitFactors =
285-
{
284+
private static ReadOnlySpan<double> PixelUnitFactors =>
285+
[
286286
96.0, // Pixels per Inch
287287
96.0 / 2.54, // Pixels per Centimeter
288288
96.0 / 72.0, // Pixels per Point
289-
};
289+
];
290290
}
291291
}

src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/DataGridRow.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class DataGridRow : Control
4444
private const byte DATAGRIDROW_stateNullCode = 255;
4545

4646
// Static arrays to handle state transitions:
47-
private static byte[] _idealStateMapping = new byte[] {
47+
private static ReadOnlySpan<byte> IdealStateMapping => [
4848
DATAGRIDROW_stateNormalCode,
4949
DATAGRIDROW_stateNormalCode,
5050
DATAGRIDROW_stateMouseOverCode,
@@ -61,9 +61,9 @@ public class DataGridRow : Control
6161
DATAGRIDROW_stateNormalEditingFocusedCode,
6262
DATAGRIDROW_stateMouseOverEditingCode,
6363
DATAGRIDROW_stateMouseOverEditingFocusedCode
64-
};
64+
];
6565

66-
private static byte[] _fallbackStateMapping = new byte[] {
66+
private static ReadOnlySpan<byte> FallbackStateMapping => [
6767
DATAGRIDROW_stateNormalCode, //DATAGRIDROW_stateMouseOverCode's fallback
6868
DATAGRIDROW_stateMouseOverEditingFocusedCode, //DATAGRIDROW_stateMouseOverEditingCode's fallback
6969
DATAGRIDROW_stateNormalEditingFocusedCode, //DATAGRIDROW_stateMouseOverEditingFocusedCode's fallback
@@ -74,7 +74,7 @@ public class DataGridRow : Control
7474
DATAGRIDROW_stateSelectedFocusedCode, //DATAGRIDROW_stateNormalEditingFocusedCode's fallback
7575
DATAGRIDROW_stateSelectedFocusedCode, //DATAGRIDROW_stateSelectedCode's fallback
7676
DATAGRIDROW_stateNormalCode //DATAGRIDROW_stateSelectedFocusedCode's fallback
77-
};
77+
];
7878

7979
private static string[] _stateNames = new string[] {
8080
VisualStates.DATAGRIDROW_stateMouseOver,
@@ -226,7 +226,7 @@ internal override void ChangeVisualState(bool useTransitions)
226226
idealStateMappingIndex += 1;
227227
}
228228

229-
byte stateCode = _idealStateMapping[idealStateMappingIndex];
229+
byte stateCode = IdealStateMapping[idealStateMappingIndex];
230230
Debug.Assert(stateCode != DATAGRIDROW_stateNullCode);
231231

232232
string storyboardName;
@@ -254,7 +254,7 @@ internal override void ChangeVisualState(bool useTransitions)
254254
else
255255
{
256256
// The state wasn't implemented so fall back to the next one
257-
stateCode = _fallbackStateMapping[stateCode];
257+
stateCode = FallbackStateMapping[stateCode];
258258
}
259259
}
260260

0 commit comments

Comments
 (0)
0