Skip to content
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

[SR-7952] Performance of lazy compactMap is terrible #50487

Closed
TellowKrinkle opened this issue Jun 10, 2018 · 2 comments
Closed

[SR-7952] Performance of lazy compactMap is terrible #50487

TellowKrinkle opened this issue Jun 10, 2018 · 2 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression SILOptimizer Area → compiler: SIL optimization passes swift 4.1

Comments

@TellowKrinkle
Copy link

Previous ID SR-7952
Radar rdar://problem/41005326
Original Reporter @TellowKrinkle
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, 4.1Regression, Optimizer
Assignee @eeckstein
Priority Medium

md5: 472a6fc38d023d4100ea2188bc387c7d

Issue Description:

In the following code, the flatMap (used for compatibility with swift 4.0.3) performs much worse than the filter + map under a -O build.

import Dispatch

func benchmark<T>(_ function: () -> T) -> (T, Double) {
    let start = DispatchTime.now()
    let res = function()
    let end = DispatchTime.now()
    return (res, Double(end.uptimeNanoseconds - start.uptimeNanoseconds) / 1_000_000_000)
}

let base = 0..<1_000_000

let (fmRes, flatMapTime) = benchmark { base.lazy.flatMap({ $0 % 4 == 0 ? nil : $0 }).reduce(0, +) }
let (mapRes, mapTime) = benchmark { base.lazy.filter({ $0 % 4 != 0 }).map({ $0 }).reduce(0, +) }

print("Flat Map: \(fmRes), took \(flatMapTime)\n     Map: \(mapRes), took \(mapTime)")

Output under swift 4.1 as far back as the 2017 October 25 snapshot (oldest I could find):

Flat Map: 375000000000, took 0.876876712
     Map: 375000000000, took 0.002207212

Output under swift 4.0.3 release:

Flat Map: 375000000000, took 0.002158283
     Map: 375000000000, took 0.002088703
@jckarter
Copy link
Member

@swift-ci create

@eeckstein
Copy link
Member

Fixed in #17885

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added SILOptimizer Area → compiler: SIL optimization passes and removed Optimizer labels Nov 2, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself regression SILOptimizer Area → compiler: SIL optimization passes swift 4.1
Projects
None yet
Development

No branches or pull requests

4 participants