I've been playing around recently with compiling a crate that simply contains:
pub const BYTES: &[u8] = include_bytes!("large-binary-blob");
and I've been surprised that the compile time of this crate is pretty nontrivial!
For example this crate:
pub const BYTES: &[u8] = &[];
takes 0.060 seconds to compile on nightly for me. If a multi-megabyte binary (such as cargo itself) is included:
pub const BYTES: &[u8] = include_bytes!("/path/to/.cargo/bin/cargo");
this crate takes 0.729 seconds to compile!
There appear to be at least two major sources of slowdown:
In the expansion of include_bytes! the expr_lit function calls from_lit_kind which calls to_lit_token that runs an operation per-byte, which is pretty expensive for multi-megabyte files.
- Later in compilation while emitting metadata when we're processing constants we'll end up pretty printing them and pretty printing the byte string literal for a multi-megabyte file takes quite some time!
There may be a few other sources of slowdown, but ideally usage of include_bytes! and large byte blobs in general should never iterate over the bytes and perform expensive operations, but rather the bytes should just be transferred as a whole in various contexts if absolutely necessary.
I've been playing around recently with compiling a crate that simply contains:
and I've been surprised that the compile time of this crate is pretty nontrivial!
For example this crate:
takes 0.060 seconds to compile on nightly for me. If a multi-megabyte binary (such as
cargoitself) is included:this crate takes 0.729 seconds to compile!
There appear to be at least two major sources of slowdown:
In the expansion ofinclude_bytes!theexpr_litfunction callsfrom_lit_kindwhich callsto_lit_tokenthat runs an operation per-byte, which is pretty expensive for multi-megabyte files.There may be a few other sources of slowdown, but ideally usage of
include_bytes!and large byte blobs in general should never iterate over the bytes and perform expensive operations, but rather the bytes should just be transferred as a whole in various contexts if absolutely necessary.