-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Efficient Params and String Formatting #2302
Comments
🎆 🎉 🎆 🎉 🎆 🎉 🎆 🎉 🎆 |
Can the |
Great proposal! Is there a link to a proposal, spec, or even code file for |
@MgSam current variant code is here: dotnet/corefxlab#2595 Basically it is a wrapper around a union struct with some unsafe manipulation going on to get at the values without boxing as long as those values are from a small set of well known types. I'm not sure I like the conversion operators on it but otherwise it is reasonable. As @morganbr points out some ok looking code gives perhaps unexpected results like |
Why not use |
If it was guaranteed by the language that As a somewhat contrived example, consider: string Format(string format, params IEnumerable<string> args);
…
foreach (var format in formats)
{
Format(format, "foo", "bar", "baz");
} A smart compiler could allocate a single custom implementation of Though I'm not sure such optimization would ever be implemented in the compiler; it's certainly much less useful than the other optimizations that are proposed here. readonly struct ValueFormattableString Should ValueFormattableString.Create("hello {0}", new Variant(DateTime.UtcNow)) Since this API is not meant for human consumption, could you consider approaches that avoid the cost of parsing the format string? For example, the code: ReadOnlySpan<string> texts = stackalloc[] { "Weight: ", " kg" };
ReadOnlySpan<Variant> args = stackalloc[] { (Variant)weight };
ReadOnlySpan<int?> alignments = stackalloc[] { (int?)7 };
ReadOnlySpan<string> formats = stackalloc[] { "f1" };
var vfs = ValueFormattableString.Create(texts, args, alignments, formats); |
Assuming the callee doesn't store the |
Why would storing it be an issue? Iterating it will always return the same values, so multiple pieces of code (even multiple threads) iterating that |
Oh right, because it's all constant values. |
Is this related to #535 ? |
Closing as this proposal has been broken off into three different items that are being pursued. |
Proposal Document
This is issue is related to the following:
LDM history:
The text was updated successfully, but these errors were encountered: