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-14778] Standard Library swapAt: creates transient memory allocations for small value types #57128

Open
hassila opened this issue Jun 15, 2021 · 1 comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior.

Comments

@hassila
Copy link

hassila commented Jun 15, 2021

Previous ID SR-14778
Radar rdar://problem/79351572
Original Reporter @hassila
Type Bug
Environment

swift-driver version: 1.26 Apple Swift version 5.5 (swiftlang-1300.0.19.104 clang-1300.0.18.4)``Target: x86_64-apple-macosx11.0

Additional Detail from JIRA
Votes 0
Component/s
Labels Bug
Assignee None
Priority Medium

md5: e7f45b6162fac2aa35f8ffcfb2ac117b

Issue Description:

When analysing runtime performance of the new PriorityQueue implementation being proposed for Swift Collections, we could see that we had a massive amount of unexpected mallocs when running one of the benchmarks, which could be traced down to swapAt: in an array of Int:s.

I forked the relevant repo and have created an additional branch that fixes it by just doing the swap manually as an inlined function:

hassila/swift-collections-aqua@b5cc85f

This was also discussed at the Swift forums as it caused issues for reference types (excessive ARC traffic - https://forums.swift.org/t/unexpected-swapat-free-malloc-overhead/41109/13), but I think this case when small value types creates two memory allocations per swap is more problematic.

To reproduce, just clone:

https://github.com/hassila/swift-collections

checkout the 'priority-queue' branch and run the swift-collections-benchmark scheme with the arguments "run benchmark --cycles 1 -f "PriorityQueue<Int> insert""

run it in instruments with the allocations profiling template and look at the transient allocations (several millions) generated by swapAt:.

then checkout the 'priority-queue-optimizations' branch which has the trivial fix and rerun the profiling session and the transient allocations from swapAt are gone.

You can find more information here also in a couple of comments:

apple/swift-collections#51 (comment)

These unexpected mallocs basically doubled the runtime.

@typesanitizer
Copy link

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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.
Projects
None yet
Development

No branches or pull requests

2 participants