-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
[API Proposal]: Add method TryFormat for Enum #57881
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
These were added as instance TryFormat methods, implementing the ISpanFormattable interface. Enum could do that as well, but without assistance from the JIT it'll incur some of the same overheads you're seeing in your STR_Parse_text benchmark: those allocations are for boxing the enum in order to call ToString (for the known enum values, ToString itself won't allocate). |
Tagging subscribers to this area: @dotnet/area-system-runtime Issue DetailsBackground and motivationSince I also would like to submit a PR for this. BenchmarkDotNet=v0.13.1, OS=Windows 10.0.18363.1440 (1909/November2019Update/19H2)
Intel Core i7-8650U CPU 1.90GHz (Kaby Lake R), 1 CPU, 8 logical and 4 physical cores
.NET SDK=6.0.100-preview.6.21355.2
[Host] : .NET 6.0.0 (6.0.21.35212), X64 RyuJIT
DefaultJob : .NET 6.0.0 (6.0.21.35212), X64 RyuJIT
API Proposalnamespace System {
public abstract class Enum : ValueType, IComparable, IConvertible, IFormattable {
// new method
public static bool TryFormat<TEnum>(TEnum value, Span<char> destination, out int charsWritten)
where TEnum : struct, Enum;
}
} API UsageSpan<char> destination = new char[50];
var isSuccess = Enum.TryFormat(Color.LightBlue, destination, out var charsWritten);
Debug.Assert(isSuccess);
Debug.Assert(destination.Slice(0, charsWritten).SequenceEqual(nameof(Color.LightBlue))); RisksI dont see any risk
|
While the JIT assistance does not happen, are you interested in a PR with an initial implementation? IMHO in this case is nice to provide the API (since it offers significante performance improvements) and improve implementation itself when possible. |
Thanks. A few thoughts:
|
@tannergooding Is assisting to implement ISpanFormattable (efficiently) for System.Enum on the radar for the JIT team? |
This hasn't even gone to API review yet so its not been considered. That would also be a question for the JIT/codegen owners. As for whether this should goto API review, I'd like to hear weigh in from @jkotas since this would be exposing yet another generic overload to |
We have
You would want to add instance TryFormat method for that and then depend on the JIT to eliminate the boxing. It would one of those unreliable optimizations that depends on the JIT not running out of the optimization budget, etc. |
@jkotas String interpolation with enums values would, for example, automatically benefit from an efficient implementation of ISpanFormattable |
Right. Do you have any specific ideas for how the efficient implementation of ISpanFormattable should work? |
@jkotas No, I was just echoing other comments and giving an example of an area which would benefit. I'm well aware of my limits. |
namespace System
{
public partial class Enum : ISpanFormattable
{
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider);
public static bool TryFormat<TEnum>(TEnum value, Span<char> destination, out int charsWritten)
where TEnum : struct, Enum;
}
} |
@leandromoh are you interested in working on this one? |
I have no previous experience about coding runtime/CLR @danmoseley. |
@leandromoh my bad, I missed your comment. Many of our long time contributors started by contributing some small changes. Perhaps you would like to do that first? If you are interested, you could look through https://github.com/dotnet/runtime/issues?q=is%3Aissue+is%3Aopen+label%3Aup-for-grabs for something that seems easy (there is actually an 'easy' label, but not applied to all easy things). There are good instructions in this repo explaining how to clone and build the repo, how to make changes and test them. I am happy to help you as you go along, if the change is in my knowledge area. |
@danmoseley #69343 (comment) mentions that the API proposal is incorrect as it is missing the format specifier argument. |
I edited the current propostal to add |
Background and motivation
Since
Span
type was created a lot of types addedTryFormat
method to write the value as text into a destination span. In .NET 6 these methods were added forchar
andbool
types, however for enum type I could not find anything. I suggest to add it.I also would like to submit a PR for this.
My current implementation got good results:
API Proposal
API Usage
Risks
I dont see any risk
The text was updated successfully, but these errors were encountered: