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-4937] DispatchQueue.sync much slower than dispatch_queue_sync due to unnecessary allocations #47514

Open
weissi opened this issue May 19, 2017 · 3 comments
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself performance runtime The Swift Runtime

Comments

@weissi
Copy link
Member

weissi commented May 19, 2017

Previous ID SR-4937
Radar None
Original Reporter @weissi
Type Bug

Attachment: Download

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, Performance, Runtime
Assignee None
Priority Medium

md5: 397ee4b6ea34e12ee215e25032316bfa

Issue Description:

Swift program

This Swift program

import Foundation

func main() -> Int {
    let q = DispatchQueue(label: "q")

    let iterations = 100000000

    for _ in 0..<iterations {
        q.sync {}
    }
    return 0
}

_ = main()

compiled with swiftc -O test_dispatch_sync_slow.swift takes about 14s to run on my machine (with Swift 3.1 and swift-4.0-DEVELOPMENT-SNAPSHOT-2017-05-17)

C program

This C program

#include <dispatch/dispatch.h>

int main() {
    dispatch_queue_t q = dispatch_queue_create("q", DISPATCH_QUEUE_SERIAL);

    int iterations = 100000000;

    for (int i=0; i<iterations; i++) {
        dispatch_sync(q, ^{});
    }

    dispatch_release(q);
    return 0;
}

however (which should be exactly the same) only takes about 2s to execute (compiled with clang -O3 test_dispatch_sync_fast.c -o test_dispatch_sync_fast).

Difference

The difference is that the Swift program spends most of the time calling Block_copy and Block_release which aren't necessary for a synchronous dispatch... See screenshot.

@belkadan
Copy link
Contributor

@jckarter: We could consider using stack blocks for functions marked noescape?

@weissi
Copy link
Member Author

weissi commented Sep 20, 2018

unchanged in Swift 4.2

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@weissi
Copy link
Member Author

weissi commented Feb 21, 2024

Unchanged on latest Swifts (but of course DispatchQueue.sync is much less important now) FWIW:

C

$ pbpaste | clang -x c -O2 -o /tmp/ctest - && time /tmp/ctest

real	0m1.650s
user	0m1.578s
sys	0m0.010s

vs Swift

$ pbpaste | swiftc -O -o /tmp/test - && time /tmp/test

real	0m10.531s
user	0m10.439s
sys	0m0.022s

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 performance runtime The Swift Runtime
Projects
None yet
Development

No branches or pull requests

2 participants