swiftlang/swift-foundation

Large performance difference between two seemingly equivalent ways to create `IndexSet`

Closed this issue · 2 comments

While investigating a performance issue in a system I work on, I discovered the following:

let range = NSRange(0..<1_000_000)

// this is very slow
_ = IndexSet(range)

// this is very fast
_ = NSIndexSet(indexesIn: range) as IndexSet

It's easy for me to fix this now that I know about it, but does seem like something worth looking into.

@mattmassicotte I don't believe that IndexSet(NSRange) initializer is coming from Foundation AFAICT. The following code fails to compile on both macOS and Linux:

import Foundation
let range = NSRange(0..<1_000_000)
_ = IndexSet(range)

Is it possible that this IndexSet(NSRange) initializer is coming from another dependency that you have in your project / from somewhere else that might have a suboptimal implementation?

@jmschonfeld thank you so much for looking, and I'm sorry for wasting your time! You are, of course, correct. This is coming from a different place.