Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Significantly improve the performance of `Collection<FutureType>.flatten #199

Merged
merged 3 commits into from
Apr 3, 2019

Conversation

MrMage
Copy link
Contributor

@MrMage MrMage commented Apr 1, 2019

This further halves the runtime in vapor/template-kit#50 from 0.1s to 50ms (Release mode), because submitting several blocks to the run loop for every single array element can be fairly inefficient.

I'd expect this to tremendously speed up template rendering as well (results forthcoming).

Example Profile result before this optimization:

Screen Shot 2019-04-01 at 11 06 08

And afterwards:

Screen Shot 2019-04-01 at 11 06 29

…ten`.

This further halves the runtime in vapor/template-kit#50 from 0.1s to 50ms.
@MrMage
Copy link
Contributor Author

MrMage commented Apr 1, 2019

Added a benchmark:

Screen Shot 2019-04-01 at 11 51 34

@MrMage
Copy link
Contributor Author

MrMage commented Apr 1, 2019

Sorry, that benchmark was compiled in Debug mode. Here's the Release variant:

Screen Shot 2019-04-01 at 12 57 06

@MrMage
Copy link
Contributor Author

MrMage commented Apr 1, 2019

I just noticed that this is essentially reverting 439d6dc#diff-945d1a38da3eef0446f42a82a64b7ead, however all tests still pass. This PR should not be affected by the issue described in #184 because we are ensuring that each element is acted upon on the same event loop.

In addition, the 3x performance gain from this PR is too important to pass up, given that flatten is actually becoming a bottle neck in several almost-synchronous parts (i.e. we use futures, but almost all of them are fulfilled right away) of Vapor (e.g. vapor/template-kit#50). (We still need to maintain correctness, of course.)

@adtrevor, would you mind taking a look at this PR?

@calebkleveter
Copy link
Member

The .flatten method was recently changed in this PR because it was found that it had some threading issue. You might want to make sure this PR doesn't re-introduce them.

@PopFlamingo
Copy link
Contributor

@MrMage Everything looks correct to me! :)

@MrMage
Copy link
Contributor Author

MrMage commented Apr 1, 2019

The .flatten method was recently changed in this PR because it was found that it had some threading issue. You might want to make sure this PR doesn't re-introduce them.

See #199 (comment) :-)

@calebkleveter
Copy link
Member

@MrMage I guess we posted at about the same time 😄. Sounds good!

Copy link
Member

@tanner0101 tanner0101 left a comment

Choose a reason for hiding this comment

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

This LGTM, thanks for looking into this.

Just to double check, would it be possible for us to use whenAllSucceeded like we are doing for Vapor 4 in NIOKit?

https://github.com/vapor/nio-kit/blob/master/Sources/NIOKit/EventLoop/EventLoop%2BFlatten.swift#L12

If not, then let's merge this.

@MrMage
Copy link
Contributor Author

MrMage commented Apr 3, 2019

Just to double check, would it be possible for us to use whenAllSucceeded like we are doing for Vapor 4 in NIOKit?

No idea about the performance of whenAllSucceed. I think we should merge this PR now, and when upgrading Core to NIO2, benchmark the performance of whenAllSucceed vs. this implementation before using whenAllSucceed.

Or will this Core.flatten method simply be removed with Vapor 4 in favor of NIOKit.flatten? If so, we should still benchmark the performance of NIOKit.flatten vs. Core.flatten.

@calebkleveter
Copy link
Member

@MrMage Core is being remove from Vapor 4, so you would go with the second option. But in reality, we won't be updating Core to NIO 2 because Vapor 3 uses NIO 1.

@MrMage
Copy link
Contributor Author

MrMage commented Apr 3, 2019

@MrMage Core is being remove from Vapor 4, so you would go with the second option. But in reality, we won't be updating Core to NIO 2 because Vapor 3 uses NIO 1.

Thanks for the info! That makes sense. I think we should benchmark NIOKit's flatten implementation then.

@MrMage
Copy link
Contributor Author

MrMage commented Apr 3, 2019

Update: no need for changes in NIOKit; I hope to upstream these into NIO with apple/swift-nio#943.

Copy link
Member

@tanner0101 tanner0101 left a comment

Choose a reason for hiding this comment

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

Thanks @MrMage !

@tanner0101 tanner0101 merged commit 02f1d0d into vapor:master Apr 3, 2019
@penny-coin
Copy link

Hey @MrMage, you just merged a pull request, have a coin!

You now have 19 coins.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants