What makes this simple function slow to Type-Check?

  extension Float {
      var cgFloat:CGFloat {
          return CGFloat.init(self)

I turned on -Xfrontend -warn-long-expression-type-checking=100
This expression generates a warning

Getter 'cgFloat' took 117ms to type-check (limit: 100ms)

It seems like a really super-simple function to me. What am I missing?


100ms doesn't seem long to me.

Anyway, one potential culprit might be all the init variants. We surely have a few variants CGFloat(_:), and CGFloat.init(Float) matches 2 of them. It's just my speculation though.


3 if you count the deprecated .init(NSNumber)

Are you sure? If you have 100 functions at that length your Type checker will take 10 seconds just for functions...

@ConfusedVorlon Do you have any additional CGFloat.init overloads?

1 Like

I do suspect a lot of costs associated to it to be heavily sub-additive. There's a lot that could be cached like class/protocol hierarchies, though frankly, that's beyond me.

1 Like

I'm not a fan of -warn-long-expression-type-checking for this reason: the first function to use a particular type or a particular member of that type "pays" for the cost of loading the type or its members. Should the compiler be faster? Yeah, probably! But it's not necessarily this function's fault specifically.

You can observe this behavior by having two copies of the property, one below the other, and then reordering them. I'd expect the first one to take longer than the second.


I don't have any additional CGFloat.init overloads

re the 100ms, that was just the suggested value I came across in an article on speeding up swift compilation. Fwiw - it is only triggered by a dozen or so methods in my project.

this particular one just seemed odd as it is (seemingly) so innocuous.

Ah that makes sense. Bugs in diagnostics aren't usually on my radar!

Terms of Service

Privacy Policy

Cookie Policy