Skip to content

Commit 74a7ed0

Browse files
committed
FindByName(ReadOnlySpan<char> name) for .Net9 + small optimization.
1 parent 147fa23 commit 74a7ed0

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

src/System.Windows.Forms.DataVisualization/General/BaseCollections.cs

+17
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,23 @@ public virtual T FindByName(string name)
491491
return null;
492492
}
493493

494+
#if NET9_0_OR_GREATER
495+
/// <summary>
496+
/// Finds the chart element by the name.<br/>
497+
/// This method approaches an O(1) operation.
498+
/// </summary>
499+
/// <param name="name">The name.</param>
500+
/// <returns></returns>
501+
public virtual T FindByName(ReadOnlySpan<char> name)
502+
{
503+
int idx = IndexOf(name);
504+
if (idx > -1)
505+
return this[idx];
506+
507+
return null;
508+
}
509+
#endif
510+
494511
/// <summary>
495512
/// Inserts the specified item in the collection at the specified index.
496513
/// </summary>

src/System.Windows.Forms.DataVisualization/Utilities/XmlSerializer.cs

+6-10
Original file line numberDiff line numberDiff line change
@@ -1028,19 +1028,14 @@ internal TypeConverter FindConverterByType(TypeConverterAttribute attr)
10281028
// throws SecurityException or MethodAccessException when the converter class is internal.
10291029
// Thats why we have this giant if - elseif here - to create type converters without reflection.
10301030
if (_converterDict.Contains(attr.ConverterTypeName))
1031-
{
10321031
return (TypeConverter)_converterDict[attr.ConverterTypeName];
1033-
}
1034-
1035-
string typeStr = attr.ConverterTypeName;
10361032

1037-
if (attr.ConverterTypeName.Contains(','))
1038-
{
1039-
typeStr = attr.ConverterTypeName.Split(',')[0];
1040-
}
1033+
ReadOnlySpan<char> typeStr = attr.ConverterTypeName.AsSpan();
1034+
int ind;
1035+
if ((ind = typeStr.IndexOf(',')) > -1)
1036+
typeStr = typeStr[..ind];
10411037

10421038
TypeConverter result = null;
1043-
10441039
if (typeStr.EndsWith(".CustomPropertiesTypeConverter", StringComparison.OrdinalIgnoreCase)) { result = new CustomPropertiesTypeConverter(); }
10451040
else if (typeStr.EndsWith(".DoubleNanValueConverter", StringComparison.OrdinalIgnoreCase)) { result = new DoubleNanValueConverter(); }
10461041
else if (typeStr.EndsWith(".DoubleDateNanValueConverter", StringComparison.OrdinalIgnoreCase)) { result = new DoubleDateNanValueConverter(); }
@@ -1072,7 +1067,8 @@ internal TypeConverter FindConverterByType(TypeConverterAttribute attr)
10721067
else if (typeStr.EndsWith(".AnchorPointValueConverter", StringComparison.OrdinalIgnoreCase)) { result = new AnchorPointValueConverter(); }
10731068
else if (typeStr.EndsWith(".AnnotationAxisValueConverter", StringComparison.OrdinalIgnoreCase)) { result = new AnnotationAxisValueConverter(); }
10741069

1075-
if (result != null) _converterDict[attr.ConverterTypeName] = result;
1070+
if (result is not null)
1071+
_converterDict[attr.ConverterTypeName] = result;
10761072

10771073
return result;
10781074
}

0 commit comments

Comments
 (0)