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-11983] KeyPath performance below expectation compared to alternative (see test) #54416

Closed
swift-ci opened this issue Dec 31, 2019 · 2 comments

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-11983
Radar rdar://problem/58455062
Original Reporter uros09 (JIRA User)
Type Improvement
Status Resolved
Resolution Duplicate

Attachment: Download

Additional Detail from JIRA
Votes 0
Component/s
Labels Improvement
Assignee uros09 (JIRA)
Priority Medium

md5: 0857d78cb9f33ab6cab8467269658cce

duplicates:

Issue Description:

Let's say we have a struct Thing, which has a member number of type Int

I performed two tests designed to convert an array [Thing] to a dictionary [Int : Thing], where the keys are number values for the corresponding struct. Basically, indexing things by number.

The first method performs the indexing by accessing struct members with a closure passed as an argument.

The second method performs the indexing by accessing struct members with a keyPath passed as an argument.

extension Sequence {
   func indexed<V: Hashable>(by callback: (Element) -> V) -> [V: Element] {...}       
   func indexed<V: Hashable>(by keyPath: KeyPath<Element, V>) -> [V : Element] {...}
}

Turns out the keypath method takes nearly double the time to execute for 100000 Things.

indexing array with closureExecution time: 0.082 s
    Number of tests: 20

indexing array with keypathExecution time: 0.143 s
    Number of tests: 20

This seems slower than expected. I wanted to bring the results of my test to light, as I found it surprising. Perhaps there is room to optimize the implementation of KeyPaths?

@beccadax
Copy link
Contributor

beccadax commented Jan 9, 2020

@swift-ci create

@jckarter
Copy link
Member

There's existing discussion of key path performance in SR-9323.

@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
Projects
None yet
Development

No branches or pull requests

3 participants