From 099ecbe0b03a740abd4dd0e6da5b83cdf6c85277 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Thu, 18 Jun 2020 15:29:45 -0400 Subject: [PATCH 1/5] Improve performance of Collection.removeFirst(_:) where Self == SubSequence --- stdlib/public/core/Collection.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stdlib/public/core/Collection.swift b/stdlib/public/core/Collection.swift index 1db572b65d06c..94fba31664a7d 100644 --- a/stdlib/public/core/Collection.swift +++ b/stdlib/public/core/Collection.swift @@ -1664,7 +1664,7 @@ extension Collection where SubSequence == Self { public mutating func removeFirst(_ k: Int) { if k == 0 { return } _precondition(k >= 0, "Number of elements to remove should be non-negative") - _precondition(count >= k, + _precondition(index(startIndex, offsetBy: k, limitedBy: endIndex) != nil, "Can't remove more items from a collection than it contains") self = self[index(startIndex, offsetBy: k).. Date: Thu, 18 Jun 2020 15:41:35 -0400 Subject: [PATCH 2/5] Update stdlib/public/core/Collection.swift Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com> --- stdlib/public/core/Collection.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/stdlib/public/core/Collection.swift b/stdlib/public/core/Collection.swift index 94fba31664a7d..eb1ea18184f9d 100644 --- a/stdlib/public/core/Collection.swift +++ b/stdlib/public/core/Collection.swift @@ -1664,7 +1664,10 @@ extension Collection where SubSequence == Self { public mutating func removeFirst(_ k: Int) { if k == 0 { return } _precondition(k >= 0, "Number of elements to remove should be non-negative") - _precondition(index(startIndex, offsetBy: k, limitedBy: endIndex) != nil, + guard let idx = index(startIndex, offsetBy: k, limitedBy: endIndex) else { + _preconditionFailure("Can't remove more items from a collection than it contains") + } + self = self[idx.. Date: Thu, 18 Jun 2020 15:42:22 -0400 Subject: [PATCH 3/5] Update Collection.swift --- stdlib/public/core/Collection.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/stdlib/public/core/Collection.swift b/stdlib/public/core/Collection.swift index eb1ea18184f9d..7754c4db742e3 100644 --- a/stdlib/public/core/Collection.swift +++ b/stdlib/public/core/Collection.swift @@ -1665,7 +1665,8 @@ extension Collection where SubSequence == Self { if k == 0 { return } _precondition(k >= 0, "Number of elements to remove should be non-negative") guard let idx = index(startIndex, offsetBy: k, limitedBy: endIndex) else { - _preconditionFailure("Can't remove more items from a collection than it contains") + _preconditionFailure( + "Can't remove more items from a collection than it contains") } self = self[idx.. Date: Thu, 18 Jun 2020 15:44:33 -0400 Subject: [PATCH 4/5] Update Collection.swift --- stdlib/public/core/Collection.swift | 2 -- 1 file changed, 2 deletions(-) diff --git a/stdlib/public/core/Collection.swift b/stdlib/public/core/Collection.swift index 7754c4db742e3..8badbf020b404 100644 --- a/stdlib/public/core/Collection.swift +++ b/stdlib/public/core/Collection.swift @@ -1669,7 +1669,5 @@ extension Collection where SubSequence == Self { "Can't remove more items from a collection than it contains") } self = self[idx.. Date: Fri, 19 Jun 2020 08:07:25 -0400 Subject: [PATCH 5/5] Others --- .../public/core/RangeReplaceableCollection.swift | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/stdlib/public/core/RangeReplaceableCollection.swift b/stdlib/public/core/RangeReplaceableCollection.swift index 729849b7b1d40..737f3e7ed3d2d 100644 --- a/stdlib/public/core/RangeReplaceableCollection.swift +++ b/stdlib/public/core/RangeReplaceableCollection.swift @@ -590,9 +590,10 @@ extension RangeReplaceableCollection { public mutating func removeFirst(_ k: Int) { if k == 0 { return } _precondition(k >= 0, "Number of elements to remove should be non-negative") - _precondition(count >= k, - "Can't remove more items from a collection than it has") - let end = index(startIndex, offsetBy: k) + guard let end = index(startIndex, offsetBy: k, limitedBy: endIndex) else { + _preconditionFailure( + "Can't remove more items from a collection than it has") + } removeSubrange(startIndex..= 0, "Number of elements to remove should be non-negative") - _precondition(count >= k, - "Can't remove more items from a collection than it contains") - self = self[index(startIndex, offsetBy: k)..