Why `AbstractFunctionDecl::BodySynthesizer`?

What is the purpose of AbstractFunctionDecl::BodySynthesizer?

Body synthesizers seem used by derived conformances, but it's not clear to me whether it exists as a performance optimization (lazily synthesizing function body) or whether it's necessary for some reason.

Context: @burmako asked me and I don't know why.

cc @codafi

It's a compiler performance optimization. We save quite a bit of time during type checking if we avoid synthesizing bodies for methods that aren't really used by the program.

Thank you! That makes sense.

I'll leave the same message from another question thread: I wonder if this rationale is documented anywhere? If not, adding some code documentation sounds good. I care about documentation like this so I'm happy to write a short draft sometime.

More specifically, when type checking a derived conformance implementation, or an accessor method, or one of several other places where lazy body synthesizers are used, we don't need the body if the declaration was defined in a non-primary source file.

We also use lazy body synthesizers for various imported declarations as well. Name lookup can pull in a lot of imported declarations which are ultimately never referenced from generated code, and we don't need a body there either (in general, if you emit a reference to an imported declaration with a body, you do have to emit the body in the same translation unit though, since they're not exported from anywhere).

1 Like
Terms of Service

Privacy Policy

Cookie Policy