Skip to content

Commit 7a11700

Browse files
committed
[stdlib] Avoid retaining storage in _StringGuts.updateNativeStorage
1 parent 2086313 commit 7a11700

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

stdlib/public/core/StringGutsRangeReplaceable.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ extension _StringGuts {
7070
internal mutating func updateNativeStorage<R>(
7171
_ body: (__StringStorage) -> R
7272
) -> R {
73-
let storage = self._object.nativeStorage
74-
self = _StringGuts()
75-
defer {
76-
// We re-initialize from the modified storage to pick up new count, flags,
77-
// etc.
78-
self = _StringGuts(storage)
73+
let (r, cf) = self._object.withNativeStorage {
74+
let r = body($0)
75+
let cf = $0._countAndFlags
76+
return (r, cf)
7977
}
80-
return body(storage)
78+
// We need to pick up new count/flags from the modified storage.
79+
self._object._setCountAndFlags(to: cf)
80+
return r
8181
}
8282

8383
@inlinable

stdlib/public/core/StringObject.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,17 @@ internal struct _StringObject {
172172
_internalInvariant(!isSmall)
173173
return CountAndFlags(rawUnchecked: _countAndFlagsBits)
174174
}
175+
176+
// FIXME: This ought to be the setter for property `_countAndFlags`.
177+
@_alwaysEmitIntoClient
178+
internal mutating func _setCountAndFlags(to value: CountAndFlags) {
179+
#if arch(i386) || arch(arm) || arch(arm64_32) || arch(wasm32)
180+
self._count = value.count
181+
self._flags = value.flags
182+
#else
183+
self._countAndFlagsBits = value._storage
184+
#endif
185+
}
175186
}
176187

177188
// Raw

0 commit comments

Comments
 (0)