-
Notifications
You must be signed in to change notification settings - Fork 117
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
Removing unnecessary lazy calls. #605
base: main
Are you sure you want to change the base?
Removing unnecessary lazy calls. #605
Conversation
Can one of the admins verify this patch? |
6 similar comments
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
Can one of the admins verify this patch? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this PR! However, it's not the case that the .lazy
has no use because the call is immediately consumed without overhead. Without .lazy
, .map
will allocate and return an Array
. That Array is a performance cost, and we don't want to materialise it.
You have changed lazy in two places. Lets first talk about about the Lines 606 to 611 in 2adca4b
I have slightly modified the test as I wasn't sure how you have created @inline(never)
func blackHole(_ value: some Any) {}
func lazy() {
let allobjects = Array(repeating: 1, count: 100)
var counter = 0
let duration = ContinuousClock().measure {
for _ in 0..<1_000_000 {
counter += Dictionary(allobjects[0..<40].lazy.map({ ($0, 1) }), uniquingKeysWith: +).values.first!
}
}
blackHole(counter)
print ("lazy", duration)
}
func eager() {
let allobjects = Array(repeating: 1, count: 100)
var counter = 0
let duration = ContinuousClock().measure {
for _ in 0..<1_000_000 {
counter += Dictionary(allobjects[0..<40].map({ ($0, 1) }), uniquingKeysWith: +).values.first!
}
}
blackHole(counter)
print ("eager", duration)
}
lazy()
eager() which prints in release mode with swift-5.7-DEVELOPMENT-SNAPSHOT-2022-07-17-a:
This makes perfect sense as no intermediate array is allocated because However, I think you are right about the second place where you have changed lazy: Lines 612 to 623 in 2adca4b
We have manually specified the return type as being an Array , we sadly do allocate an intermediate Array .But flatMap has an overload which actually accepts some Sequence as the return type from the closure and hence eliminates the intermediate allocation too: https://developer.apple.com/documentation/swift/sequence/flatmap(_:)-jo2yUsing this isn't straight forward though because .lazy.map(_:) will now take an escaping closure and we make a mutating call to self.
The solution to this is a bit ugly as we need to use var connectionToCreate = withoutActuallyEscaping({ (eventLoop: EventLoop) -> HTTPConnectionPool.Connection.ID in
self.createNewOverflowConnection(on: eventLoop)
}) { createNewOverflowConnectionOnEventLoop in
requiredEventLoopOfPendingRequests
.flatMap { eventLoop, requestCount -> LazyMapSequence<LazySequence<StrideTo<Int>>.Elements, (HTTPConnectionPool.Connection.ID, EventLoop)> in
// We need a connection for each queued request with a required event loop.
// Therefore, we look how many request we have queued for a given `eventLoop` and
// how many connections we are already starting on the given `eventLoop`.
// If we have not enough, we will create additional connections to have at least
// on connection per request.
let connectionsToStart = requestCount - startingRequiredEventLoopConnectionCount[eventLoop.id, default: 0]
return stride(from: 0, to: connectionsToStart, by: 1).lazy.map { _ in
(createNewOverflowConnectionOnEventLoop(eventLoop), eventLoop)
}
}
} I haven't tested the performance but I'm quite confident that this will improve performance too as we do not allocate an intermediate array. As this makes the code a bit harder to follow I ended up not using the |
In the second case you're right that However, in the case of Dictionary construction there remains insufficient evidence to make the case that this flow optimises the intermediate Array away. That allocation is extraordinarily painful, and it's worth removing even though |
As a sidebar, if we really wanted to make this faster the way to do it is to replace this functional code that feels like it wants to use |
/Sources/AsyncHTTPClient/ConnectionPool/State Machine/HTTPConnectionPool+HTTP1Connections.swift had unnecessary lazy calls in it. Since the results of the lazy calls are immediately consumed, using lazy here introduces an unnecessary performance overhead!