# Huge performance hit from exclusive memory access checks

**URL:** <https://forums.swift.org/t/huge-performance-hit-from-exclusive-memory-access-checks/54374>\
**Category:** Using Swift\
**Tags:** performance, memory-safety\
**Created:** [December 31, 2021, 5:41pm UTC](https://forums.swift.org/t/huge-performance-hit-from-exclusive-memory-access-checks/54374 "2021-12-31T17:41:11Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![vanvoorden](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/vanvoorden/32/33619_2.png) [@vanvoorden](https://forums.swift.org/u/vanvoorden)\
**Post date:** [June 13, 2025, 6:14pm UTC](https://forums.swift.org/t/huge-performance-hit-from-exclusive-memory-access-checks/54374/12 "2025-06-13T18:14:31Z")

</div>

> [@Jon\_Shier](#):
>
> Isn’t it the opposite? The code migrates from state held in an internal class to direct properties of the struct so it no longer has to pay those costs.

Hmm… the WWDC talk starts with something like this:

```swift
struct S1 {
  class C {
    var x = 0
    var y = 0
  }
  
  var c = C()
  
  func increment() {
    c.x += 1
    c.y += 1
  }
}

```

And migrates to something like this:

```swift
struct S2 {
  var x = 0
  var y = 0
  
  mutating func increment() {
    x += 1
    y += 1
  }
}

```

To reduce the calls to `swift_beginAccess`. My question was more along the lines of if we wanted to preserve the nested class as a reference type do we have any more options to help? Something like:

```swift
struct S3 {
  private final class C {
    var x = 0
    var y = 0
  }
  
  private let c = C()
  
  func increment() {
    c.x += 1
    c.y += 1
  }
}

```

Do we know to what extent could `private` and `final` help the compiler optimize out those `swift_beginAccess` calls?

---

_[View the full topic](https://forums.swift.org/t/huge-performance-hit-from-exclusive-memory-access-checks/54374)._
