-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve enum parsing in config binder generator #88873
Comments
Tagging subscribers to this area: @dotnet/area-extensions-logging Issue DetailsLooking at your current generated code: public static ConsoleLoggerFormat ParseConsoleLoggerFormat(string value, Func<string?> getPath)
{
try
{
return (ConsoleLoggerFormat)Enum.Parse(typeof(ConsoleLoggerFormat), value, ignoreCase: true);
}
catch (Exception exception)
{
throw new InvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(ConsoleLoggerFormat)}'.", exception);
}
} There are a couple comments here:
using BenchmarkDotNet.Attributes;
namespace Benchmark;
public enum ConsoleLoggerFormat
{
Default,
Systemd,
}
[MemoryDiagnoser]
public class EnumParseBenchmark
{
[Benchmark]
public ConsoleLoggerFormat ParseConsoleLoggerFormatGeneric() => ParseConsoleLoggerFormatGeneric("Systemd", () => "");
[Benchmark]
public ConsoleLoggerFormat ParseConsoleLoggerFormat() => ParseConsoleLoggerFormat("Systemd", () => "");
public static ConsoleLoggerFormat ParseConsoleLoggerFormatGeneric(string value, Func<string?> getPath)
{
try
{
return Enum.Parse<ConsoleLoggerFormat>(value, ignoreCase: true);
}
catch (Exception exception)
{
throw new InvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(ConsoleLoggerFormat)}'.", exception);
}
}
public static ConsoleLoggerFormat ParseConsoleLoggerFormat(string value, Func<string?> getPath)
{
try
{
return (ConsoleLoggerFormat)Enum.Parse(typeof(ConsoleLoggerFormat), value, ignoreCase: true);
}
catch (Exception exception)
{
throw new InvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(ConsoleLoggerFormat)}'.", exception);
}
}
} on my machine I see:
The generic version is faster and doesn't allocate.
|
Tagging subscribers to this area: @dotnet/area-extensions-configuration Issue DetailsLooking at your current generated code: public static ConsoleLoggerFormat ParseConsoleLoggerFormat(string value, Func<string?> getPath)
{
try
{
return (ConsoleLoggerFormat)Enum.Parse(typeof(ConsoleLoggerFormat), value, ignoreCase: true);
}
catch (Exception exception)
{
throw new InvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(ConsoleLoggerFormat)}'.", exception);
}
} There are a couple comments here:
using BenchmarkDotNet.Attributes;
namespace Benchmark;
public enum ConsoleLoggerFormat
{
Default,
Systemd,
}
[MemoryDiagnoser]
public class EnumParseBenchmark
{
[Benchmark]
public ConsoleLoggerFormat ParseConsoleLoggerFormatGeneric() => ParseConsoleLoggerFormatGeneric("Systemd", () => "");
[Benchmark]
public ConsoleLoggerFormat ParseConsoleLoggerFormat() => ParseConsoleLoggerFormat("Systemd", () => "");
public static ConsoleLoggerFormat ParseConsoleLoggerFormatGeneric(string value, Func<string?> getPath)
{
try
{
return Enum.Parse<ConsoleLoggerFormat>(value, ignoreCase: true);
}
catch (Exception exception)
{
throw new InvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(ConsoleLoggerFormat)}'.", exception);
}
}
public static ConsoleLoggerFormat ParseConsoleLoggerFormat(string value, Func<string?> getPath)
{
try
{
return (ConsoleLoggerFormat)Enum.Parse(typeof(ConsoleLoggerFormat), value, ignoreCase: true);
}
catch (Exception exception)
{
throw new InvalidOperationException($"Failed to convert configuration value at '{getPath()}' to type '{typeof(ConsoleLoggerFormat)}'.", exception);
}
}
} on my machine I see:
The generic version is faster and doesn't allocate.
|
Will keep this assigned to me; but happy to take a community contribution here. |
from @eerhardt in #88067:
Looking at your current generated code:
There are a couple comments here:
Enum.Parse<T>
when possible. I did a quick benchmark:on my machine I see:
The generic version is faster and doesn't allocate.
The text was updated successfully, but these errors were encountered: