From e5ea054f45b6e086a3ac3a1a29c81487a7daee3f Mon Sep 17 00:00:00 2001 From: Taras Tsugrii Date: Thu, 7 Sep 2023 18:11:16 -0700 Subject: [PATCH] [minmax] Do nothing for empty suffix/prefix. It's a semantical noop but slightly more efficient for the case when max/min is requested for an empty Collection. --- Sources/Algorithms/MinMax.swift | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Sources/Algorithms/MinMax.swift b/Sources/Algorithms/MinMax.swift index 3bce4af5..ea6b5a7a 100644 --- a/Sources/Algorithms/MinMax.swift +++ b/Sources/Algorithms/MinMax.swift @@ -256,14 +256,14 @@ extension Collection { """ ) + // Make sure we are within bounds. + let prefixCount = Swift.min(count, self.count) + // Do nothing if we're prefixing nothing. - guard count > 0 else { + guard prefixCount > 0 else { return [] } - // Make sure we are within bounds. - let prefixCount = Swift.min(count, self.count) - // If we're attempting to prefix more than 10% of the collection, it's // faster to sort everything. guard prefixCount < (self.count / 10) else { @@ -310,14 +310,14 @@ extension Collection { """ ) + // Make sure we are within bounds. + let suffixCount = Swift.min(count, self.count) + // Do nothing if we're suffixing nothing. - guard count > 0 else { + guard suffixCount > 0 else { return [] } - // Make sure we are within bounds. - let suffixCount = Swift.min(count, self.count) - // If we're attempting to prefix more than 10% of the collection, it's // faster to sort everything. guard suffixCount < (self.count / 10) else {