-
Notifications
You must be signed in to change notification settings - Fork 21
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
Use builders to implement yield
expressions
#951
Comments
You can use Consider another case:
It is very cumbersome to generate ResizeArray here. |
@greatim An optimization where a minimum length can be detected before creating the builder (by counting definite future yields, calling .Length on Array/ImmutableArray instances that are about to be |
@charlesroddie There are actually two problems here:
I agree that it needs improvement. But the direction of improvement is inconsistent. Your code is actually reflected in |
If I understand your comments right (and I'm not sure I do) you are examining alternatives to the builder idea, which would incrementally improve the existing method, and the conclusion is that there are may be ways to improve it in some special cases subject to some technicalities but not in a general way. So that would support going with builders I think. |
@charlesroddie Excuse me, I didn't ask before now, does the
To:
This is not what we want. And what needs to be added is the It seems that the compiler may need to do more optimization work. And I don’t think it’s a good habit to optimize |
@greatim No those ideas are on different lines to this issue. The suggestion in this issue is that when code is seen with a yield expression - a collection with an explicit or implicit yield inside, e.g. The suggestion is not to replace just |
I have understood that something like this is enabled by resumable state machines. See Example: low-allocation list and array builders |
Fixed by dotnet/fsharp#6811 |
Actually was done in dotnet/fsharp#11592 |
Yield expressions are implemented using a
GeneratedSequenceBase<int>
class with e.g. SeqModule.ToArray afterwards: sharplabUsing a builder would perform much better, and might also allow removing syntactical restrictions, if
yield
can be used whereverb.Add
can be used. E.g. allowingOption.iter yield
, which addresses the main example in #705 .The above code would then compile down to:
The benchmark gives for Arrays:
For FSharpList, using
b.ToArray() |> List.ofArray
:Builders should apply to all non-lazy sequence types (FSharpList, Array, ImmutableArray).
The text was updated successfully, but these errors were encountered: