[SR-14778] Standard Library swapAt: creates transient memory allocations for small value types #57128
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
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
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.
The text was updated successfully, but these errors were encountered: