Skip to content

Improve performance of String.concat for seq of type array or list#9483

Merged
cartermp merged 8 commits into
dotnet:masterfrom
abelbraaksma:String.concat-perf
Jul 25, 2020
Merged

Improve performance of String.concat for seq of type array or list#9483
cartermp merged 8 commits into
dotnet:masterfrom
abelbraaksma:String.concat-perf

Conversation

@abelbraaksma

@abelbraaksma abelbraaksma commented Jun 18, 2020

Copy link
Copy Markdown
Contributor

This follows the findings and timings shown in this comment: #9390 (comment)

Basically: special-casing the input seq for array gives 2x perf gain and less 2-3x less mem use, and special-casing for list gives 20% gain and 30% less mem and GC pressure.

@KevinRansom: this is the one we discussed that shows no improvement by using array-based manipulation if the input is a seq, and since String.Join uses buffered-SB internally, that may well be the reason it already performed so well.

It's also a showcase of the internal ArrayEnumerator of arrays-turned-into-sequences, however fast it is, still adding a lot of time for the indirection, hence the success of the special-casing of array. Try this new method with Array.toSeq x and with implicit x :> seq<_> and watch the difference (a factor 2).

This PR is dependent on #9481.

@cartermp cartermp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some conflicts and stylistic change, but otherwise this looks good!

Comment thread src/fsharp/FSharp.Core/string.fs Outdated
@abelbraaksma

abelbraaksma commented Jun 18, 2020

Copy link
Copy Markdown
Contributor Author

Since we now have three execution paths (for list, seq and array), I changed the tests to execute all three paths for the existing tests 9c6ba3f.

@abelbraaksma abelbraaksma requested a review from cartermp June 18, 2020 18:32
@abelbraaksma

Copy link
Copy Markdown
Contributor Author

Strange, it keeps saying "Changes requested" even though all requests have been resolved. Maybe I'm doing something wrong...

@KevinRansom

Copy link
Copy Markdown
Contributor

@abelbraaksma , the requester has to acknowledge that they have been handled I believe.

@abelbraaksma abelbraaksma requested a review from KevinRansom June 19, 2020 17:14
@abelbraaksma

abelbraaksma commented Jun 24, 2020

Copy link
Copy Markdown
Contributor Author

All requested changes have been implemented and build is green. @cartermp / @KevinRansom, is it ok like this?

@abelbraaksma

Copy link
Copy Markdown
Contributor Author

@cartermp, you merged master into this, I think this is now ready?

@cartermp

cartermp commented Jul 5, 2020

Copy link
Copy Markdown
Contributor

@abelbraaksma I think this is ready, but this will require one other review (preferably @KevinRansom since he previously requested changes)

@abelbraaksma

Copy link
Copy Markdown
Contributor Author

/cc @KevinRansom

@cartermp cartermp merged commit 62b0140 into dotnet:master Jul 25, 2020
@abelbraaksma

Copy link
Copy Markdown
Contributor Author

Thanks @KevinRansom!

@abelbraaksma abelbraaksma deleted the String.concat-perf branch July 25, 2020 20:33
nosami pushed a commit to xamarin/visualfsharp that referenced this pull request Feb 23, 2021
…otnet#9483)

* Move `String.length` to the top of its module so that the `length` function is in scope

* Improve performance of String.concat for arrays and lists

* Make concatArray local to String.concat

* Testing String.concat, make sure the new three paths are covered

* Remove "foo", "bax", "bar" in tests, making them more readable

Co-authored-by: Phillip Carter <pcarter@fastmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants