Generic functions, compile time

  1. Suppose we have the following situation:
crate A:

  pub fn foo<T> (...) { ... }

crate B:

  foo<i32>(...);
  foo<Cat>(...);
  foo<Dog>(..);

Now, in terms of "compile time spent on foo" -- how much of it is spent during compile of crateA, and how much of it is spent during crateB?

My current intuition is that for generic functions, almost no work is done during compile time for crateA besides type checking, and almost all the work is done during compilation of crateB.

1 Like

AFAIK your understanding is correct. And if I am not wrong, it is to do with mono-morphisation.

2 Likes

Common advice for "speeding up compile time" is:

  1. break big crate into many small crates
  2. have a "shell crate" that imports all the small crates

However, based on the above, if the functions we put into the small crates are GENERIC functions ... then there's little/no compile time benefit right?

When you compile crate A, the compiler checks types and lifetime based on the functions generic bound, and store into .rlib as a serialized AST. So no machine code is generated in this time.

4 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.