From 7773d6400491df32a58bd6bb3462c4fd46c2f79e Mon Sep 17 00:00:00 2001 From: Robert Pieta Date: Fri, 15 Jun 2018 00:56:12 -0500 Subject: [PATCH 1/2] Improved performance of NSIndexSet.isEqual --- Foundation/NSIndexSet.swift | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/Foundation/NSIndexSet.swift b/Foundation/NSIndexSet.swift index 50a2f4fbb2..0d071ef44c 100644 --- a/Foundation/NSIndexSet.swift +++ b/Foundation/NSIndexSet.swift @@ -111,16 +111,19 @@ open class NSIndexSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding { } open func isEqual(to indexSet: IndexSet) -> Bool { - - let otherRanges = indexSet.rangeView.map { NSRange(location: $0.lowerBound, length: $0.upperBound - $0.lowerBound) } - if _ranges.count != otherRanges.count { + // Exit early if the IndexSets do not have the same number of intervals + if _ranges.count != indexSet.rangeView.count { return false } - for (r1, r2) in zip(_ranges, otherRanges) { - if r1.length != r2.length || r1.location != r2.location { + + // Iterate over indexes to compare each + for (r1, r2) in zip(_ranges, indexSet.rangeView) { + // Return false if the ranges do not match + if r1.location != r2.lowerBound || r1.length != r2.upperBound - r2.lowerBound { return false } } + return true } From 01fa634277c9e691496b45c3e8d58ef330463c31 Mon Sep 17 00:00:00 2001 From: Robert Pieta Date: Fri, 15 Jun 2018 10:44:41 -0500 Subject: [PATCH 2/2] Improved performance of NSIndexSet.isEqual --- Foundation/NSIndexSet.swift | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/Foundation/NSIndexSet.swift b/Foundation/NSIndexSet.swift index 0d071ef44c..035e6da591 100644 --- a/Foundation/NSIndexSet.swift +++ b/Foundation/NSIndexSet.swift @@ -115,11 +115,13 @@ open class NSIndexSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding { if _ranges.count != indexSet.rangeView.count { return false } - + // Iterate over indexes to compare each - for (r1, r2) in zip(_ranges, indexSet.rangeView) { + for (range, element) in zip(_ranges, indexSet.rangeView) { + let elementLength = element.upperBound - element.lowerBound + // Return false if the ranges do not match - if r1.location != r2.lowerBound || r1.length != r2.upperBound - r2.lowerBound { + if range.location != element.lowerBound || range.length != elementLength { return false } }