This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
Suggest a possible optimization for static Color GetNamedColor(ReadOnlySpan<char> value)
.
#495
Comments
Additional runs shows that the performance of the switch-statement one is unstable at my PC: sometimes it spends 4 us, sometimes - 8 us.
The method: static Color GetNamedColor(ReadOnlySpan<char> value)
{
// the longest built-in Color's name is much lower than this check, so we should not allocate here in a typical usage
Span<char> loweredValue = value.Length <= 128 ? stackalloc char[value.Length] : new char[value.Length];
int charsWritten = value.ToLowerInvariant(loweredValue);
Debug.Assert(charsWritten == value.Length);
switch (loweredValue)
{
case "default":
return default;
case "aliceblue":
return Colors.AliceBlue;
case "antiquewhite":
return Colors.AntiqueWhite;
case "aqua":
return Colors.Aqua;
case "aquamarine":
return Colors.Aquamarine;
case "azure":
return Colors.Azure;
case "beige":
return Colors.Beige;
case "bisque":
return Colors.Bisque;
case "black":
return Colors.Black;
case "blanchedalmond":
return Colors.BlanchedAlmond;
case "blue":
return Colors.Blue;
case "blueviolet":
return Colors.BlueViolet;
case "brown":
return Colors.Brown;
case "burlywood":
return Colors.BurlyWood;
case "cadetblue":
return Colors.CadetBlue;
case "chartreuse":
return Colors.Chartreuse;
case "chocolate":
return Colors.Chocolate;
case "coral":
return Colors.Coral;
case "cornflowerblue":
return Colors.CornflowerBlue;
case "cornsilk":
return Colors.Cornsilk;
case "crimson":
return Colors.Crimson;
case "cyan":
return Colors.Cyan;
case "darkblue":
return Colors.DarkBlue;
case "darkcyan":
return Colors.DarkCyan;
case "darkgoldenrod":
return Colors.DarkGoldenrod;
case "darkgray":
return Colors.DarkGray;
case "darkgreen":
return Colors.DarkGreen;
case "darkgrey":
return Colors.DarkGrey;
case "darkkhaki":
return Colors.DarkKhaki;
case "darkmagenta":
return Colors.DarkMagenta;
case "darkolivegreen":
return Colors.DarkOliveGreen;
case "darkorange":
return Colors.DarkOrange;
case "darkorchid":
return Colors.DarkOrchid;
case "darkred":
return Colors.DarkRed;
case "darksalmon":
return Colors.DarkSalmon;
case "darkseagreen":
return Colors.DarkSeaGreen;
case "darkslateblue":
return Colors.DarkSlateBlue;
case "darkslategray":
return Colors.DarkSlateGray;
case "darkslategrey":
return Colors.DarkSlateGrey;
case "darkturquoise":
return Colors.DarkTurquoise;
case "darkviolet":
return Colors.DarkViolet;
case "deeppink":
return Colors.DeepPink;
case "deepskyblue":
return Colors.DeepSkyBlue;
case "dimgray":
return Colors.DimGray;
case "dimgrey":
return Colors.DimGrey;
case "dodgerblue":
return Colors.DodgerBlue;
case "firebrick":
return Colors.Firebrick;
case "floralwhite":
return Colors.FloralWhite;
case "forestgreen":
return Colors.ForestGreen;
case "fuchsia":
return Colors.Fuchsia;
case "gainsboro":
return Colors.Gainsboro;
case "ghostwhite":
return Colors.GhostWhite;
case "gold":
return Colors.Gold;
case "goldenrod":
return Colors.Goldenrod;
case "gray":
return Colors.Gray;
case "green":
return Colors.Green;
case "grey":
return Colors.Grey;
case "greenyellow":
return Colors.GreenYellow;
case "honeydew":
return Colors.Honeydew;
case "hotpink":
return Colors.HotPink;
case "indianred":
return Colors.IndianRed;
case "indigo":
return Colors.Indigo;
case "ivory":
return Colors.Ivory;
case "khaki":
return Colors.Khaki;
case "lavender":
return Colors.Lavender;
case "lavenderblush":
return Colors.LavenderBlush;
case "lawngreen":
return Colors.LawnGreen;
case "lemonchiffon":
return Colors.LemonChiffon;
case "lightblue":
return Colors.LightBlue;
case "lightcoral":
return Colors.LightCoral;
case "lightcyan":
return Colors.LightCyan;
case "lightgoldenrodyellow":
return Colors.LightGoldenrodYellow;
case "lightgrey":
return Colors.LightGrey;
case "lightgray":
return Colors.LightGray;
case "lightgreen":
return Colors.LightGreen;
case "lightpink":
return Colors.LightPink;
case "lightsalmon":
return Colors.LightSalmon;
case "lightseagreen":
return Colors.LightSeaGreen;
case "lightskyblue":
return Colors.LightSkyBlue;
case "lightslategray":
return Colors.LightSlateGray;
case "lightslategrey":
return Colors.LightSlateGrey;
case "lightsteelblue":
return Colors.LightSteelBlue;
case "lightyellow":
return Colors.LightYellow;
case "lime":
return Colors.Lime;
case "limegreen":
return Colors.LimeGreen;
case "linen":
return Colors.Linen;
case "magenta":
return Colors.Magenta;
case "maroon":
return Colors.Maroon;
case "mediumaquamarine":
return Colors.MediumAquamarine;
case "mediumblue":
return Colors.MediumBlue;
case "mediumorchid":
return Colors.MediumOrchid;
case "mediumpurple":
return Colors.MediumPurple;
case "mediumseagreen":
return Colors.MediumSeaGreen;
case "mediumslateblue":
return Colors.MediumSlateBlue;
case "mediumspringgreen":
return Colors.MediumSpringGreen;
case "mediumturquoise":
return Colors.MediumTurquoise;
case "mediumvioletred":
return Colors.MediumVioletRed;
case "midnightblue":
return Colors.MidnightBlue;
case "mintcream":
return Colors.MintCream;
case "mistyrose":
return Colors.MistyRose;
case "moccasin":
return Colors.Moccasin;
case "navajowhite":
return Colors.NavajoWhite;
case "navy":
return Colors.Navy;
case "oldlace":
return Colors.OldLace;
case "olive":
return Colors.Olive;
case "olivedrab":
return Colors.OliveDrab;
case "orange":
return Colors.Orange;
case "orangered":
return Colors.OrangeRed;
case "orchid":
return Colors.Orchid;
case "palegoldenrod":
return Colors.PaleGoldenrod;
case "palegreen":
return Colors.PaleGreen;
case "paleturquoise":
return Colors.PaleTurquoise;
case "palevioletred":
return Colors.PaleVioletRed;
case "papayawhip":
return Colors.PapayaWhip;
case "peachpuff":
return Colors.PeachPuff;
case "peru":
return Colors.Peru;
case "pink":
return Colors.Pink;
case "plum":
return Colors.Plum;
case "powderblue":
return Colors.PowderBlue;
case "purple":
return Colors.Purple;
case "red":
return Colors.Red;
case "rosybrown":
return Colors.RosyBrown;
case "royalblue":
return Colors.RoyalBlue;
case "saddlebrown":
return Colors.SaddleBrown;
case "salmon":
return Colors.Salmon;
case "sandybrown":
return Colors.SandyBrown;
case "seagreen":
return Colors.SeaGreen;
case "seashell":
return Colors.SeaShell;
case "sienna":
return Colors.Sienna;
case "silver":
return Colors.Silver;
case "skyblue":
return Colors.SkyBlue;
case "slateblue":
return Colors.SlateBlue;
case "slategray":
return Colors.SlateGray;
case "slategrey":
return Colors.SlateGrey;
case "snow":
return Colors.Snow;
case "springgreen":
return Colors.SpringGreen;
case "steelblue":
return Colors.SteelBlue;
case "tan":
return Colors.Tan;
case "teal":
return Colors.Teal;
case "thistle":
return Colors.Thistle;
case "tomato":
return Colors.Tomato;
case "transparent":
return Colors.Transparent;
case "turquoise":
return Colors.Turquoise;
case "violet":
return Colors.Violet;
case "wheat":
return Colors.Wheat;
case "white":
return Colors.White;
case "whitesmoke":
return Colors.WhiteSmoke;
case "yellow":
return Colors.Yellow;
case "yellowgreen":
return Colors.YellowGreen;
default:
return null;
}
} |
as a last note I have to say here, looks like
|
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
I write this into issue, because I'm not sure we want such optimizations at all.
Now, the method looks like
I found that there is the room for optimization. On my machine the data looks like (PTAL on allocations too):
The benchmark method looks like:
The optimized method:
(the idea taken from from dotnet/roslyn#56374)
Few issues to mention:
If maintainers decides that this approach is fine, few additional points:
Any thoughts? Thanks.
The text was updated successfully, but these errors were encountered: