Skip to content

Using include_bytes! on large binary blobs compiles more slowly than expected #65818

Open
@alexcrichton

Description

@alexcrichton

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:

  1. 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.
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-compiletimeIssue: Problems and improvements with respect to compile times.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions