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-110] Passing a struct to a function copies it for each loop iteration #42732

Closed
swift-ci opened this issue Dec 7, 2015 · 4 comments
Closed
Labels
compiler The Swift compiler in itself improvement performance

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Dec 7, 2015

Previous ID SR-110
Radar None
Original Reporter kpickett (JIRA User)
Type Improvement
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Improvement, Performance
Assignee None
Priority Medium

md5: 21b9097ec1fefa85821a505929641c74

Issue Description:

re: https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20151207/000172.html

I have a struct Foo and this code:

func test() {
precondition(sizeof(Foo) == 128)

let s = Foo()
for _ in 0..<100_000_000 {
doSomething(s)
}
}

The asm (on LInux, with -O) is showing me that s is being re-initialized on every iteration of the loop. I was hoping that thanks to swift's strict constness rules on structs, it wouldn't have to do this - and just pass the same pointer to doSomething() each time.

When I use an inout param, that is 2x as fast and doesn't re-initialize each time. However I don't see why passing something immutably wouldn't be as fast.

  • Karl

asm from perf:

2.71 │50:┌─→xorps %xmm0,%xmm0 ▒
8.06 │ │ movaps %xmm0,-0x20(%rbp) ▒
2.71 │ │ movaps %xmm0,-0x30(%rbp) ▒
7.41 │ │ movaps %xmm0,-0x40(%rbp) ▒
10.59 │ │ movaps %xmm0,-0x50(%rbp) ▒
10.00 │ │ movaps %xmm0,-0x60(%rbp) ▒
9.53 │ │ movaps %xmm0,-0x70(%rbp) ▒
10.65 │ │ movaps %xmm0,-0x80(%rbp) ▒
11.24 │ │ movaps %xmm0,-0x90(%rbp) ▒
12.06 │ │ mov %r14,%rdi ▒
3.41 │ │→ callq TF4main11doSomethingFVS_3FooT
2.82 │ │ dec %rbx ▒
8.82 │ └──jne 50

main.swift:

struct Vec4 {
var a: Int64 = 0
var b: Int64 = 0
var c: Int64 = 0
var d: Int64 = 0
}

struct Foo {
var x: Vec4 = Vec4()
var y: Vec4 = Vec4()
var z: Vec4 = Vec4()
var u: Vec4 = Vec4()
}

func test() {
precondition(sizeof(Foo) == 128)

let s = Foo()
for _ in 0..<100_000_000 {
doSomething(s)
}
}

test()

lib.swift:

func doSomething(s: Foo) {
precondition(s.x.a != 1)
}

@swift-ci
Copy link
Collaborator Author

swift-ci commented Dec 9, 2015

Comment by Karl Pickett (JIRA)

Identical problem on mac.

swiftc -v
Apple Swift version 2.1.1 (swiftlang-700.1.101.13 clang-700.1.81)
Target: x86_64-apple-darwin15.0.0

0x100000860 <+80>: xorps %xmm0, %xmm0
0x100000863 <+83>: movaps %xmm0, -0x20(%rbp)
0x100000867 <+87>: movaps %xmm0, -0x30(%rbp)
0x10000086b <+91>: movaps %xmm0, -0x40(%rbp)
0x10000086f <+95>: movaps %xmm0, -0x50(%rbp)
0x100000873 <+99>: movaps %xmm0, -0x60(%rbp)
0x100000877 <+103>: movaps %xmm0, -0x70(%rbp)
0x10000087b <+107>: movaps %xmm0, -0x80(%rbp)
0x10000087f <+111>: movaps %xmm0, -0x90(%rbp)
0x100000886 <+118>: movq %r14, %rdi
0x100000889 <+121>: callq 0x100000ef0 ; main.doSomething (main.Foo) -> ()
0x10000088e <+126>: decq %rbx
0x100000891 <+129>: jne 0x100000860 ; <+80>

@jepers
Copy link

jepers commented Dec 20, 2015

did you try compiling with
-O -whole-module-optimization
?

@swift-ci
Copy link
Collaborator Author

Comment by Hitster GTD (JIRA)

Karl,

I have added the Performance label to this SR.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis
Copy link
Collaborator

-emit-sil -O shows that we now inline doSomething and extract the value of s.x.a once. As for the original example with properties having initial values, s.x.a != 1 is recognized to always be true, and the body of test() gets optimized away.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself improvement performance
Projects
None yet
Development

No branches or pull requests

3 participants