From 492da020cf08e28cbbf6386bc6dcb244fb9fe665 Mon Sep 17 00:00:00 2001 From: Lucy Satheesan Date: Tue, 23 May 2023 10:50:00 -0700 Subject: [PATCH 1/2] [gardening] remove superfluous #if --- stdlib/public/core/ArraySlice.swift | 4 ---- 1 file changed, 4 deletions(-) diff --git a/stdlib/public/core/ArraySlice.swift b/stdlib/public/core/ArraySlice.swift index be6363e3a9483..05eb45ea4ece1 100644 --- a/stdlib/public/core/ArraySlice.swift +++ b/stdlib/public/core/ArraySlice.swift @@ -195,11 +195,7 @@ extension ArraySlice { func _checkSubscript( _ index: Int, wasNativeTypeChecked: Bool ) -> _DependenceToken { -#if _runtime(_ObjC) _buffer._checkValidSubscript(index) -#else - _buffer._checkValidSubscript(index) -#endif return _DependenceToken() } From ff76106177df00aca1d11af8bdcef411117ecf01 Mon Sep 17 00:00:00 2001 From: Lucy Satheesan Date: Tue, 23 May 2023 10:51:49 -0700 Subject: [PATCH 2/2] [stdlib] don't copy array contents on `removeAll(keepingCapacity: true)` --- stdlib/public/core/Array.swift | 9 ++++++++- stdlib/public/core/ArraySlice.swift | 9 ++++++++- stdlib/public/core/ContiguousArray.swift | 5 ++++- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/stdlib/public/core/Array.swift b/stdlib/public/core/Array.swift index 667ccf932e51d..443d2227eeaa7 100644 --- a/stdlib/public/core/Array.swift +++ b/stdlib/public/core/Array.swift @@ -1378,9 +1378,16 @@ extension Array: RangeReplaceableCollection { if !keepCapacity { _buffer = _Buffer() } - else { + else if _buffer.isMutableAndUniquelyReferenced() { self.replaceSubrange(indices, with: EmptyCollection()) } + else { + let buffer = _ContiguousArrayBuffer( + _uninitializedCount: 0, + minimumCapacity: capacity + ) + _buffer = _Buffer(_buffer: buffer, shiftedToStartIndex: startIndex) + } } //===--- algorithms -----------------------------------------------------===// diff --git a/stdlib/public/core/ArraySlice.swift b/stdlib/public/core/ArraySlice.swift index 05eb45ea4ece1..369f42dd8301d 100644 --- a/stdlib/public/core/ArraySlice.swift +++ b/stdlib/public/core/ArraySlice.swift @@ -1067,9 +1067,16 @@ extension ArraySlice: RangeReplaceableCollection { if !keepCapacity { _buffer = _Buffer() } - else { + else if _buffer.isMutableAndUniquelyReferenced() { self.replaceSubrange(indices, with: EmptyCollection()) } + else { + let buffer = _ContiguousArrayBuffer( + _uninitializedCount: 0, + minimumCapacity: capacity + ) + _buffer = _Buffer(_buffer: buffer, shiftedToStartIndex: startIndex) + } } //===--- algorithms -----------------------------------------------------===// diff --git a/stdlib/public/core/ContiguousArray.swift b/stdlib/public/core/ContiguousArray.swift index 0d41e4b0f1592..c06d55c61ed7f 100644 --- a/stdlib/public/core/ContiguousArray.swift +++ b/stdlib/public/core/ContiguousArray.swift @@ -975,9 +975,12 @@ extension ContiguousArray: RangeReplaceableCollection { if !keepCapacity { _buffer = _Buffer() } - else { + else if _buffer.isMutableAndUniquelyReferenced() { self.replaceSubrange(indices, with: EmptyCollection()) } + else { + _buffer = _Buffer(_uninitializedCount: 0, minimumCapacity: capacity) + } } //===--- algorithms -----------------------------------------------------===//