# Observation optimizes away unnecessary callbacks for Equatable properties… sometimes

**URL:** <https://forums.swift.org/t/observation-optimizes-away-unnecessary-callbacks-for-equatable-properties-sometimes/89358>\
**Category:** Using Swift\
**Tags:** equality, observation, observable\
**Created:** [September 3, 2026, 10:57am UTC](https://forums.swift.org/t/observation-optimizes-away-unnecessary-callbacks-for-equatable-properties-sometimes/89358 "2026-09-03T10:57:56Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![marco.masser](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/marco.masser/32/6010_2.png) [@marco.masser](https://forums.swift.org/u/marco.masser)\
**Post date:** [September 3, 2026, 10:57am UTC](https://forums.swift.org/t/observation-optimizes-away-unnecessary-callbacks-for-equatable-properties-sometimes/89358/1 "2026-09-03T10:57:56Z")

</div>

It seems like changing a property in an `@Observable` class triggers observation callbacks, but that is optimized away the value does not actually change and it is of an `Equatable` type. This makes a lot of sense and is a nice optimization.

But it seems like this optimization does not always work (edit: for value types like structs).

```swift
struct WrappedString: Equatable {
    var value: String
}

@Observable
final class Model {

    var wrappedName: WrappedString

    init(name: String) {
        self.wrappedName = .init(value: name)
    }
}

```

Specifically, assigning the whole `WrappedString` value does not trigger a callback (i.e. this uses the optimization):

```swift
model.wrappedName = .init(value: sameNameAsBefore)

```

…but assigning something inside the `WrappedString` value _does_ trigger the callback (i.e. no optimization):

```swift
model.wrappedName.value = sameNameAsBefore

```

My guess would be that some kind of optimization that takes advantage of `Equatable` is bypassed when not assigning the whole property value, but by assigning something inside it. Maybe the `@ObservationTracked` macro uses the new `mutate` accesssor instead of using `get`+`set` and only `set` has that optimization?

I looked in the Swift Observation macro source code and I think the comment above this [implementation of the generated `_modify` accessor](https://github.com/swiftlang/swift/blob/a0e9642726f4e76df5734a1d7e267b0152c9694f/lib/Macros/Sources/ObservationMacros/ObservableMacro.swift#L472) explains the reason, right?

```swift
// Note: this accessor cannot test the equality since it would incur
// additional CoW's on structural types. Most mutations in-place do
// not leave the value equal so this is "fine"-ish.
// Warning to future maintence: adding equality checks here can make
// container mutation O(N) instead of O(1).
// e.g. observable.array.append(element) should just emit a change
// to the new array, and NOT cause a copy of each element of the
// array to an entirely new array.
let modifyAccessor: AccessorDeclSyntax =
  """
  _modify {
    access(keyPath: \\.\(identifier))
    \(raw: ObservableMacro.registrarVariableName).willSet(self, keyPath: \\.\(identifier))
    defer { \(raw: ObservableMacro.registrarVariableName).didSet(self, keyPath: \\.\(identifier)) }
    yield &_\(identifier)
  }
  """

```

I saw [a previous thread](https://forums.swift.org/t/observation-with-equatable/66855) and [related PR (closed but unmerged)](https://github.com/swiftlang/swift/pull/68107), but those talk about implementing this kind of optimization, not this specific issue with it.

It would be nice if somebody could chime in on whether this is something that might be further optimized in the future. I can post more code to show this issue if necessary.

---

<div class="post-metadata">

**Author:** ![mbrandonw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mbrandonw/32/349_2.png) [@mbrandonw](https://forums.swift.org/u/mbrandonw)\
**Post date:** [September 3, 2026, 2:41pm UTC](https://forums.swift.org/t/observation-optimizes-away-unnecessary-callbacks-for-equatable-properties-sometimes/89358/2 "2026-09-03T14:41:06Z")

</div>

What you are asking for _does_ work as long as you are using reference types all the way down:

```swift
@Observable class ModelA {
  var modelB = ModelB()
}
@Observable class ModelB {
  var value = 0
}

```

Then an access like this:

```swift
modelA.modelB.value

```

…would trigger observation only if `value` is changed, or if `modelB` is wholly replaced by a new `ModelB` object. But mutations to other fields in `modelB` would not cause observation to be triggered.

It is not currently possible to extend this kind of behavior to value types, if that is what you are looking for. The PR you linked to does not try to solve this problem, and instead is a precursor to the functionality that [skips observation](https://github.com/swiftlang/swift/blob/a0e9642726f4e76df5734a1d7e267b0152c9694f/lib/Macros/Sources/ObservationMacros/ObservableMacro.swift#L454-L457) when values don't change (see PR [#78151](https://github.com/swiftlang/swift/pull/78151)).

---

<div class="post-metadata">

**Author:** ![marco.masser](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/marco.masser/32/6010_2.png) [@marco.masser](https://forums.swift.org/u/marco.masser)\
**Post date:** [September 4, 2026, 7:03am UTC](https://forums.swift.org/t/observation-optimizes-away-unnecessary-callbacks-for-equatable-properties-sometimes/89358/3 "2026-09-04T07:03:13Z")

</div>

Thanks for the reply! Yes, I was asking about this behavior in the context of value types – sorry for not being clearer about this, I’ll edit my original post.

I don’t have a problem with this behavior in the code I’m working on. We had equality checks in place before assigning properties on an `@Observable` type to avoid unnecessary observations and when checking whether this is actually necessary, I noticed that it seems to behave inconsistently – now there’s an explanation 🙂

---

<div class="post-metadata">

**Author:** ![marco.masser](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/marco.masser/32/6010_2.png) [@marco.masser](https://forums.swift.org/u/marco.masser)\
**Post date:** [September 4, 2026, 7:08am UTC](https://forums.swift.org/t/observation-optimizes-away-unnecessary-callbacks-for-equatable-properties-sometimes/89358/4 "2026-09-04T07:08:48Z")

</div>

> [@mbrandonw](#):
>
> It is not currently possible to extend this kind of behavior to value types, if that is what you are looking for.

Do you have any further information on why this is currently not possible? Thanks!

---

<div class="post-metadata">

**Author:** ![mbrandonw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mbrandonw/32/349_2.png) [@mbrandonw](https://forums.swift.org/u/mbrandonw)\
**Post date:** [September 4, 2026, 2:34pm UTC](https://forums.swift.org/t/observation-optimizes-away-unnecessary-callbacks-for-equatable-properties-sometimes/89358/5 "2026-09-04T14:34:14Z")

</div>

There was quite a bit of back-and-forth in the original review, starting around [here](https://forums.swift.org/t/second-review-se-0395-observability/65261/42) and ending [here](https://forums.swift.org/t/second-review-se-0395-observability/65261/119).

---

<div class="post-metadata">

**Author:** ![marco.masser](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/marco.masser/32/6010_2.png) [@marco.masser](https://forums.swift.org/u/marco.masser)\
**Post date:** [September 4, 2026, 2:45pm UTC](https://forums.swift.org/t/observation-optimizes-away-unnecessary-callbacks-for-equatable-properties-sometimes/89358/6 "2026-09-04T14:45:09Z")

</div>

Thanks, much appreciated!
