0

I have project that build with cargo workspace with including lot of crates.

One of the lower level crates contains generic data-structure with a lot of serde-code involved.

In order to reduce the compile time , I tried to crate objects with monomorphized instances on the the data-struct in a crate that is lower in the compilation hierarchy and use those in the higher-level crates. My goal to compile the lower-level crate only once, and then work on the higher level crate - without generating the monomorphized instances every time.

example:

lower-level crate
----------------- 

pub struct MyCache<T> {
    //generic implementation of cache  
}  

pub struct MyCacheString {
    cache: MyCache<String> 
} 

higher-level crate
------------------
use MyCacheString; 

but the problem is that the compiler generated that monomorphized in the higer-level crate (according to "cargo llvm-lines")

Is there is a way to ask/force the compiler to generate the monorphized code while it's compile the lower level crate?

Eyal leshem
  • 995
  • 2
  • 10
  • 21
  • Are you sure that this would even help you? I assume you want to reduce compilation times. Did you look output of `cargo --timings` and realized that this would indeed speed things up? – Aleksander Krauze Oct 31 '22 at 09:59
  • well , I Don't sure about it - but according to `cargo llvm-line` in the higher level crate has around 300,000 lines that generate by serde code , that probably generate by multiple instance of this data-struct (according to `cargo --timing` this higher level crate is definitely the bottle-neck of the compilation) – Eyal leshem Oct 31 '22 at 10:04

1 Answers1

0

ok, according to rustc_monomorphize::collector docstring:

Lazy mode means that items will only be instantiated when actually referenced

So in order to tigger the compiler to monomorphized the code in the low-level crate I need to call the object functions from public non-generic function lower-level crate. (not sure why - but it's not working from async function).

(If someone know about macro that tell the compiler to use the 'Eager Mode' it's will be better )

Eyal leshem
  • 995
  • 2
  • 10
  • 21