-
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
API Proposal: Public interface ISpanFormattable #26374
Comments
We have a very similar interface in corefxlab and I am wondering if there is a way to consolidate the two: |
I am in favor of adding one of the interfaces to the platform. I think IBufferFormattable is more generally applicable. It can format in an arbitrary encoding. But, we would need to think what types would actually implement the interface. |
Why was this closed? |
I honestly can't remember, might have been a mistake or the assumption that |
Having this interface would potentially make adding |
What's the value of |
In the current implementations of the interfaces it's 0. There was a previous discussion about this, if I remember correctly, the assumption is that most of the time the buffer will be large enough and the overhead of calculating the actual necessary length isn't worth it. Calculating the length might in some cases mean, that the format operation, or a good part of it, is executed to obtain the length. |
dotnet/corefxlab#2595 has a usage for this on As part of this suggestion we would add this interface to any intrinsic types that we haven't already and additional common types such as: Additionally we would add this to |
DateTime, DateTimeOffset, Guid, and TimeSpan all already implement it 😉 |
Yep, just trying to set initial scoping expectations. 😁 |
From discussion at dotnet/corefxlab#2595 (comment) : It would be useful to have the overall no-allocation high-performance formatting story figured out before we start making parts of it public. |
In particular, I think we need to seriously look how this would work for UTF8 formatting. |
This interface would not. We would need a separate interface for that. |
I opened an issue some time ago about adding TryFormat to IFormattable as a DIM (#30547). I personally think that it wouldn't be too bad if we were to have it as part of the existing interface as behaviourally they do the same thing. |
Has this been API reviewed? Where are the comments regarding that and the update to the labels? |
@davidfowl suggested in #26373 that I make a formal proposal.
Motivation
There is an internal interface
ISpanFormattable
The only member is:
https://github.com/dotnet/corefx/blob/d7f120a01ac6ea09a2139c99e43d7811950d2f73/src/Common/src/CoreLib/System/ISpanFormattable.cs#L9
This interface is the
Span
version ofIFormattable
and is implemented by many of the primitive types to allow formatting into aSpan
. It would be useful make this interface public for users wanting to useTryFormat
with these types or implement their own formatting methods forSpan
.Example:
The Utf8Formatter for float currently is using ToString() to format float/double
https://github.com/dotnet/corefx/blob/d7f120a01ac6ea09a2139c99e43d7811950d2f73/src/System.Memory/src/System/Buffers/Text/Utf8Formatter/Utf8Formatter.Float.cs#L68
If the interface would be public it could replace the type constraint
IFormattable
in the method above and it would be possible to optimize the allocations in the method above, by using stackalloc andTryFormat
. As double/float formatting is a complex process the utf8 version formats the value into a string and then copies the ascii characters into the utf8 byte array.Alternatives
@davidfowl pointed out, that it could be possible to make the interface generic, e.g.
ISpanFormattable<T>
withISpanFormattable<char>
andISpanFormattable<byte>
This would add quite a bit of additional work, e.g. moving pretty much everything from Utf8Formatter into the relevant types and/or writing new formatter methods. This idea is currently not in my scope, it might be interesting to revisit the idea after
Utf8String
is finished.Having a public generic interface where
char
andbyte
(and maybeuint
for UTF32) are the onlyvalid
type constraints is a bit weird imho, this might change with future constraint possibilities.Intended Audience
Span
, e.g. serializers, logging libraries etc.Open points
The text was updated successfully, but these errors were encountered: