# Expensive calls to "outlined init with take"

**URL:** <https://forums.swift.org/t/expensive-calls-to-outlined-init-with-take/52187>\
**Category:** Using Swift\
**Created:** [September 18, 2021, 6:58pm UTC](https://forums.swift.org/t/expensive-calls-to-outlined-init-with-take/52187 "2021-09-18T18:58:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [September 18, 2021, 6:58pm UTC](https://forums.swift.org/t/expensive-calls-to-outlined-init-with-take/52187/1 "2021-09-18T18:58:10Z")

</div>

While profiling my code, I've found a lot of calls to "outlined init with take" functions that are surprisingly expensive.

 ![image](https://global.discourse-cdn.com/swift/original/3X/e/e/ee356c95c45fde95ced9957c4f5f79854cdb9d86.png)

These calls really add up - for instance, the function shown writes a normalized URL string, including percent-encoding inputs, parsing and rewriting a path, etc, and instruments shows [this one line](https://github.com/karwa/swift-url/blob/d1022c83c3bedf3b88a89cb0166de50f3f28e32b/Sources/WebURL/Parser/Parser.swift#L355) amounts to around 8% of the time spent in the function (recorded in high frequency mode):

![image](https://global.discourse-cdn.com/swift/original/3X/c/1/c1c0abdabc5b6a4d493a7f69d263b62bc430380d.png)

And that's just one function.

Now, `info.schemeKind` is a stored property of type `Optional<WebURL.SchemeKind>`, and `WebURL.SchemeKind` is an enum with 7 cases. I would expect `Optional<WebURL.SchemeKind>` to occupy exactly 1 byte, and printing `MemoryLayout<Optional<WebURL.SchemeKind>>.size` confirms that. Copying a single byte which is a stored property shouldn't require runtime calls.

I've seen this with other small enums, as well. I have a `Sigil` enum with only 2 cases, and I'm seeing the same expensive "init with take" functions all around my code.

I can't understand what's causing this - does anybody have any information? How can I avoid them?

---

<div class="post-metadata">

**Author:** ![David\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_smith/32/37508_2.png) [@David\_Smith](https://forums.swift.org/u/David_Smith)\
**Post date:** [September 18, 2021, 7:17pm UTC](https://forums.swift.org/t/expensive-calls-to-outlined-init-with-take/52187/2 "2021-09-18T19:17:52Z")

</div>

If memory serves, what's happening here is that the amount of generated code to initialize a `WebURL.SchemeKind` exceeded a threshold, so it was extracted out into a non-inline init function to reduce code size. If, as I suspect, this automatic outlining (reverse-inlining) is using the same mechanism as some of the other places, it may be running into the issue described in [Reducing value witness size in struct composition - #3 by gmittertreiner](https://forums.swift.org/t/reducing-value-witness-size-in-struct-composition/52172/3) where the codegen for the outlined function is suboptimal.

---

<div class="post-metadata">

**Author:** ![David\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_smith/32/37508_2.png) [@David\_Smith](https://forums.swift.org/u/David_Smith)\
**Post date:** [September 18, 2021, 7:21pm UTC](https://forums.swift.org/t/expensive-calls-to-outlined-init-with-take/52187/3 "2021-09-18T19:21:03Z")

</div>

Hm. @scanon is it expected that we'd see the Nehalem and Ivybridge variants of memmove in the same process?

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [September 18, 2021, 8:29pm UTC](https://forums.swift.org/t/expensive-calls-to-outlined-init-with-take/52187/4 "2021-09-18T20:29:21Z")

</div>

Aha!

That link was helpful, thanks. After prodding around some of the PRs, I realised that it doesn't actually depend on the size or complexity of the enum (at least, not necessarily). I'm actually able to force an "outlined init with take" with the following code (nightly, -O):

```swift
func test(_ input: HasAnEnum) -> Int {
    input.readValue()
}

enum MyEnum {
    case four
}

struct HasAnEnum {
    var something: String
    var somethingTwo: String
    var value: MyEnum?

    func readValue() -> Int {
        let x = value!
        if case .four = x { return 4 }
        return -1
    }
}

```

(Yes, the enum has _one case_)

This generates the following assembly ([Godbolt](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:swift,selection:(endColumn:14,endLineNumber:5,positionColumn:14,positionLineNumber:5,selectionStartColumn:14,selectionStartLineNumber:5,startColumn:14,startLineNumber:5),source:'func+test(_+input:+HasAnEnum)+-%3E+Int+%7B%0A++++input.readValue()%0A%7D%0A%0Aenum+MyEnum+%7B%0A++++case+four%0A%7D%0A%0Astruct+HasAnEnum+%7B%0A++++var+something:+String%0A++++var+somethingTwo:+String%0A++++var+value:+MyEnum%3F%0A%0A++++func+readValue()+-%3E+Int+%7B%0A++++++++let+x+%3D+value!!%0A++++++++if+case+.four+%3D+x+%7B+return+4+%7D%0A++++++++return+-1%0A++++%7D%0A%7D'),l:'5',n:'0',o:'Swift+source+%231',t:'0')),k:48.13751087902524,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:swiftnightly,filters:(b:'0',binary:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:swift,libs:!(),options:'-O',selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1,tree:'1'),l:'5',n:'0',o:'x86-64+swiftc+nightly+(Swift,+Editor+%231,+Compiler+%231)',t:'0')),k:51.862489120974764,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4)):

```swift
output.test(output.HasAnEnum) -> Swift.Int:
        push rbx
        sub rsp, 16
        add rdi, 32
        lea rbx, [rsp + 8]
        mov rsi, rbx
        call (outlined init with take of output.MyEnum?)
        mov rsi, rsp
        mov rdi, rbx
        call (outlined init with take of output.MyEnum?)
        cmp byte ptr [rsp], 0
        jne .LBB1_2
        mov eax, 4
        add rsp, 16
        pop rbx
        ret
.LBB1_2:
        ud2

```

However, if you comment out the `somethingTwo` property from `HasAnEnum`, suddenly it becomes:

```swift
output.test(output.HasAnEnum) -> Swift.Int:
        test dl, 1
        jne .LBB1_2
        mov eax, 4
        ret
.LBB1_2:
        ud2

```

So... it depends on the type which holds the enum, rather than the enum itself. That's very strange.

It's really costing a lot in terms of performance, so it would be great to have some way to opt out of this. Perhaps forcing some simpler layout on `HasAnEnum` so it becomes easier to access the property.

---

<div class="post-metadata">

**Author:** ![David\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_smith/32/37508_2.png) [@David\_Smith](https://forums.swift.org/u/David_Smith)\
**Post date:** [September 18, 2021, 9:30pm UTC](https://forums.swift.org/t/expensive-calls-to-outlined-init-with-take/52187/5 "2021-09-18T21:30:57Z")

</div>

I'd hazard a guess that if you added a third String rather than an enum you might also get this. Each String there is going to require emiting a two word copy and a retain call.

(edit) you may have found an interesting missed optimization there though: in the inline case it manages to just delete the entire thing, and I think the outlining is preventing that from working

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [September 19, 2021, 9:05am UTC](https://forums.swift.org/t/expensive-calls-to-outlined-init-with-take/52187/6 "2021-09-19T09:05:51Z")

</div>

> [@David\_Smith](#):
>
> I'd hazard a guess that if you added a third String rather than an enum you might also get this. Each String there is going to require emiting a two word copy and a retain call.

It doesn't. It seems to be related to the optional (the force-unwrap also doesn't matter).

I changed the struct to hold 10 strings (and added a second case to the enum, so it doesn't get constant-folded). If value is a `MyEnum`, everything is fine; if it's a `MyEnum?`, we have to slog through runtime functions ([Godbolt](https://godbolt.org/#g:!((g:!((g:!((h:codeEditor,i:(filename:'1',fontScale:14,fontUsePx:'0',j:1,lang:swift,selection:(endColumn:54,endLineNumber:21,positionColumn:54,positionLineNumber:21,selectionStartColumn:54,selectionStartLineNumber:21,startColumn:54,startLineNumber:21),source:'func+test(_+input:+HasAnEnum)+-%3E+Int+%7B%0A++++input.readValue()%0A%7D%0A%0Aenum+MyEnum+%7B%0A++++case+one%0A++++case+four%0A%7D%0A%0Astruct+HasAnEnum+%7B%0A++++var+s1:+String%0A++++var+s2:+String%0A++++var+s3:+String%0A++++var+s4:+String%0A++++var+s5:+String%0A++++var+s6:+String%0A++++var+s7:+String%0A++++var+s8:+String%0A++++var+s9:+String%0A++++var+s10:+String%0A++++var+value:+MyEnum+//+Try+making+this+an+optional.%0A%0A++++func+readValue()+-%3E+Int+%7B%0A++++++++let+x+%3D+value%0A++++++++if+case+.four+%3D+x+%7B+return+4+%7D%0A++++++++return+-1%0A++++%7D%0A%7D'),l:'5',n:'0',o:'Swift+source+%231',t:'0')),k:48.13751087902524,l:'4',n:'0',o:'',s:0,t:'0'),(g:!((h:compiler,i:(compiler:swiftnightly,filters:(b:'0',binary:'1',commentOnly:'0',demangle:'0',directives:'0',execute:'1',intel:'0',libraryCode:'0',trim:'1'),flagsViewOpen:'1',fontScale:14,fontUsePx:'0',j:1,lang:swift,libs:!(),options:'-O',selection:(endColumn:1,endLineNumber:1,positionColumn:1,positionLineNumber:1,selectionStartColumn:1,selectionStartLineNumber:1,startColumn:1,startLineNumber:1),source:1,tree:'1'),l:'5',n:'0',o:'x86-64+swiftc+nightly+(Swift,+Editor+%231,+Compiler+%231)',t:'0')),k:51.862489120974764,l:'4',n:'0',o:'',s:0,t:'0')),l:'2',n:'0',o:'',t:'0')),version:4)):

```swift
enum MyEnum {
    case one
    case four
}

struct HasAnEnum {
    var s1: String
    var s2: String
    var s3: String
    var s4: String
    var s5: String
    var s6: String
    var s7: String
    var s8: String
    var s9: String
    var s10: String
    var value: MyEnum // try making this an optional

    func readValue() -> Int {
        let x = value
        if case .four = x { return 4 }
        return -1
    }
}

```

Non-optional:

```swift
output.test(output.HasAnEnum) -> Swift.Int:
        movzx eax, byte ptr [rdi + 160]
        lea rax, [rax + 4*rax]
        add rax, -1
        ret

```

Optional:

```swift
output.test(output.HasAnEnum) -> Swift.Int:
        push rbx
        sub rsp, 16
        add rdi, 160
        lea rbx, [rsp + 8]
        mov rsi, rbx
        call (outlined init with take of output.MyEnum?)
        mov rsi, rsp
        mov rdi, rbx
        call (outlined init with take of output.MyEnum?)
        movzx eax, byte ptr [rsp]
        mov ecx, eax
        and ecx, 1
        cmp rax, 2
        lea rcx, [rcx + 4*rcx - 1]
        mov rax, -1
        cmovne rax, rcx
        add rsp, 16
        pop rbx
        ret

```

I'm quite sure that this a bug now, so I've filed [SR-15215](https://bugs.swift.org/browse/SR-15215) about it.

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [September 19, 2021, 11:25am UTC](https://forums.swift.org/t/expensive-calls-to-outlined-init-with-take/52187/7 "2021-09-19T11:25:55Z")

</div>

I've found a workaround. It's super ugly...

```swift
@inlinable @inline(__always)
internal var schemeKind: Optional<WebURL.SchemeKind> {
  withUnsafeBytes(of: self) {
    $0.load(fromByteOffset: MemoryLayout.offset(of: \Self._schemeKind)!, as: Optional<WebURL.SchemeKind>.self)
  }
}

```

Basically I renamed the stored properties and wrote my own accessors for them. I did this for like 3 or 4 stored properties, all optionals. I really, really wish I didn't have to do this, but now my benchmarks are showing _the best results I have ever seen_ from this project, with common cases improving by 17-24%(!!!).

> **Summary**
>
> ```swift
> benchmark column results/path_9 results/path_10 %
> --------------------------------------------------------------------------------------------------------
> Constructor.SpecialNonFile.AverageURLs std 26.08 63.28 -142.65
> Constructor.SpecialNonFile.AverageURLs warmup 3829983.00 3466338.00 9.49
> Constructor.SpecialNonFile.AverageURLs iterations 37796.00 44360.00 -17.37
> Constructor.SpecialNonFile.AverageURLs time 33922.00 28055.00 17.30
> Constructor.SpecialNonFile.AverageURLs filtered std 35.28 33.86 4.03
> Constructor.SpecialNonFile.AverageURLs filtered warmup 4742413.00 5451983.00 -14.96
> Constructor.SpecialNonFile.AverageURLs filtered iterations 28011.00 28872.00 -3.07
> Constructor.SpecialNonFile.AverageURLs filtered time 45356.00 43094.50 4.99
> Constructor.SpecialNonFile.IPv4 host std 35.11 34.60 1.45
> Constructor.SpecialNonFile.IPv4 host warmup 3577949.00 2827990.00 20.96
> Constructor.SpecialNonFile.IPv4 host iterations 36648.00 42892.00 -17.04
> Constructor.SpecialNonFile.IPv4 host time 35112.00 27630.00 21.31
> Constructor.SpecialNonFile.IPv4 host filtered std 50.86 36.97 27.31
> Constructor.SpecialNonFile.IPv4 host filtered warmup 4221503.00 3779503.00 10.47
> Constructor.SpecialNonFile.IPv4 host filtered iterations 32093.00 33531.00 -4.48
> Constructor.SpecialNonFile.IPv4 host filtered time 39854.00 36601.00 8.16
> Constructor.SpecialNonFile.IPv6 host std 32.64 37.21 -14.00
> Constructor.SpecialNonFile.IPv6 host warmup 4345729.00 3213953.00 26.04
> Constructor.SpecialNonFile.IPv6 host iterations 32983.00 44068.00 -33.61
> Constructor.SpecialNonFile.IPv6 host time 37398.00 28309.50 24.30
> Constructor.SpecialNonFile.IPv6 host filtered std 24.86 38.78 -56.02
> Constructor.SpecialNonFile.IPv6 host filtered warmup 5475037.00 3436541.00 37.23
> Constructor.SpecialNonFile.IPv6 host filtered iterations 30296.00 37909.00 -25.13
> Constructor.SpecialNonFile.IPv6 host filtered time 41760.00 34307.00 17.85
> Constructor.SpecialNonFile.Percent-encoding components std 37.57 32.74 12.86
> Constructor.SpecialNonFile.Percent-encoding components warmup 1278889.00 1141999.00 10.70
> Constructor.SpecialNonFile.Percent-encoding components iterations 104477.00 119079.00 -13.98
> Constructor.SpecialNonFile.Percent-encoding components time 12318.00 11108.00 9.82
> Constructor.SpecialNonFile.Percent-encoded hostnames std 104.19 37.64 63.87
> Constructor.SpecialNonFile.Percent-encoded hostnames warmup 1214436.00 1039024.00 14.44
> Constructor.SpecialNonFile.Percent-encoded hostnames iterations 109287.00 131662.00 -20.47
> Constructor.SpecialNonFile.Percent-encoded hostnames time 11629.00 9915.00 14.74
> Constructor.SpecialNonFile.Long paths std 24.11 25.35 -5.14
> Constructor.SpecialNonFile.Long paths warmup 10246773.00 9702339.00 5.31
> Constructor.SpecialNonFile.Long paths iterations 12921.00 13470.00 -4.25
> Constructor.SpecialNonFile.Long paths time 94723.00 93464.50 1.33
> Constructor.SpecialNonFile.Complex paths 1 std 47.32 28.06 40.70
> Constructor.SpecialNonFile.Complex paths 1 warmup 2991180.00 2786803.00 6.83
> Constructor.SpecialNonFile.Complex paths 1 iterations 47141.00 48935.00 -3.81
> Constructor.SpecialNonFile.Complex paths 1 time 26821.00 26165.00 2.45
> Constructor.SpecialNonFile.Complex paths 2 std 35.28 26.18 25.79
> Constructor.SpecialNonFile.Complex paths 2 warmup 3751616.00 3253327.00 13.28
> Constructor.SpecialNonFile.Complex paths 2 iterations 37345.00 41286.00 -10.55
> Constructor.SpecialNonFile.Complex paths 2 time 32966.00 32142.00 2.50
> Constructor.SpecialNonFile.Long query 1 std 194.64 70.05 64.01
> Constructor.SpecialNonFile.Long query 1 warmup 362005.00 328641.00 9.22
> Constructor.SpecialNonFile.Long query 1 iterations 407376.00 476298.00 -16.92
> Constructor.SpecialNonFile.Long query 1 time 3089.00 2738.00 11.36
> Constructor.SpecialNonFile.Long query 2 std 34.20 30.88 9.71
> Constructor.SpecialNonFile.Long query 2 warmup 2504260.00 2733403.00 -9.15
> Constructor.SpecialNonFile.Long query 2 iterations 52335.00 54263.00 -3.68
> Constructor.SpecialNonFile.Long query 2 time 24316.00 23348.00 3.98
> --------------------------------------------------------------------------------------------------------
> std 14.02
> warmup 11.72
> iterations -13.05
> time 11.09
> 
> ```

Wow. I'm ecstatic. I can't begin to describe the countless hours I've spent, trying to figure out which algorithmic changes I could make, so I could bring these benchmarks anywhere close to this level. In particular the first one ("AverageURLs") - it's the most important, the most representative of real URLs on the web, and I'd set myself the goal of getting it under 30ms. But no matter how much I tried, I just couldn't get it reliably under that level.

With one change, it has steam-rolled past that. As you can imagine, I've ran them all again multiple times to confirm. The best I've seen is 27.6, and the worst is 28.8. It's an arbitrary benchmark and there's random variance and all that, but still - it's **huge**. Even now I can't really believe I've seen those numbers.

We need this fixed, badly. I hate to think how many other developers are out there, wondering why their code isn't meeting their performance targets, spending who-knows-how-much time trying to think of clever tricks or novel algorithms, when there are things like this lurking in the compiler.

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [September 19, 2021, 4:04pm UTC](https://forums.swift.org/t/expensive-calls-to-outlined-init-with-take/52187/8 "2021-09-19T16:04:58Z")

</div>

If memory serves right, those two implementations share some of their core copy loops, with separate entry points and thresholds. So that's just perf tools misattributing samples based on the heuristic of "scan backwards until you find an exported symbol".

---

<div class="post-metadata">

**Author:** ![mattcurtis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mattcurtis/32/18493_2.png) [@mattcurtis](https://forums.swift.org/u/mattcurtis)\
**Post date:** [February 14, 2025, 5:32pm UTC](https://forums.swift.org/t/expensive-calls-to-outlined-init-with-take/52187/9 "2025-02-14T17:32:01Z")

</div>

Really glad you found and shared this solution! I wrote a generic version of it:

> [@Swift tanking performance by excessively copying enums when pattern matching?](https://forums.swift.org/t/swift-tanking-performance-by-excessively-copying-enums-when-pattern-matching/77469/10):
>
> I wrote a property wrapper that works around this issue and the compiler is able to optimize into a faster set of instructions, based on [a solution discovered by](https://forums.swift.org/t/expensive-calls-to-outlined-init-with-take/52187/7) @Karl, assuming (like me) you're stuck working with the current version of Swift that has this issue and not the nightly, which seems to have largely fixed it. @propertyWrapper struct ExpensiveValue\<Value\> { var wrappedValue: Value var projectedValue: Value { optimizableFastGet() } func optimizableFastGet() -\>…
