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

tellowkrinkle opened this issue Jun 10, 2018 · 2 comments

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

tellowkrinkle opened this issue Jun 10, 2018 · 2 comments


Copy link

tellowkrinkle commented Jun 10, 2018

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 =
    let res = function()
    let end =
    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
Copy link

jckarter commented Jun 11, 2018

@swift-ci create

Copy link

eeckstein commented Jul 12, 2018

Fixed in #17885

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet

No branches or pull requests

3 participants