Skip to content

Swift += operator inserts extra ARC traffic and breaks uniqueness checks #66141

@saagarjha

Description

@saagarjha

Description
This thread describes the issue: https://twitter.com/cpatrascudev/status/1661340099190161409. Swift chooses to insert an extra retain/release pair in the loop and this causes the in-place append fast path to not get hit because the uniqueness check fails.

Steps to reproduce

var result = ""

/*
for _ in 1...1000000 {
	result += "abc"
}
*/

for _ in 1...1000000 {
	result = result + "abc"
}

Expected behavior
This should append in-place.

Screenshots
Screenshot 2023-05-25 at 11 57 32

Environment

  • swift-driver version: 1.75.2 Apple Swift version 5.8 (swiftlang-5.8.0.124.2 clang-1403.0.22.11.100)
    Target: arm64-apple-macosx13.0
  • Xcode 14.3
    Build version 14E222b
  • macOS 13.0

Additional context
Built with -O.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ARCFeature: automatic reference countingSILOptimizerArea → compiler: SIL optimization passesbugA deviation from expected or documented behavior. Also: expected but undesirable behavior.compilerThe Swift compiler itselfdeclarationsFeature: declarationsfor-in loopsFeature: for-in loopsfuncFeature → declarations: FunctionsmutatingFeature → declarations → functions: Mutating functionsstatementsFeature: statementsswift 5.9unexpected behaviorBug: Unexpected behavior or incorrect output

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions