top | item 11650389

(no title)

austinz | 9 years ago

After a bit more investigation, I found that if you replace the following code:

  result.append(begin == eos ? "" : String(cs[begin..<end.successor()]))
with this:

  if begin == eos {
    result.append("")
  } else if let str = String(cs[begin..<end.successor()]) {
    result.append(str)
  }
runtime goes down from ~3 seconds to ~2.2 seconds.

This is due to a rather insidious API design decision:

  init?(_ view: String.UTF16View)
constructs a string out of a UTF16 view, but it can fail. If used in a context where its type is inferred to be non-nullable, the following generic reflection-related init is used instead:

  init<T>(_ instance: T)
I'm going to bring this up on the list and see if there are better ways of doing things.

As far as I can tell most of the rest of the time is spent in the Swift native Unicode --> UTF16 decoding machinery, and NSCharacterSet.

discuss

order

fauigerzigerk|9 years ago

I'm seeing a 23% increase in running time after making that same change. Strange.

That's with Xcode 7.3.1 (7D1014)

austinz|9 years ago

Weird. I'm using the same version of Xcode, running on a trashcan Mac Pro running OS X 10.11.4.