Skip to content

Slow compilation of async fn that creates a vector with many Strings created from string literals, even with no .await #115327

Open
@shepmaster

Description

@shepmaster
fn main() {
    make_menagerie();
}

async fn make_menagerie() -> Vec<String> {
    vec![
        "monster".to_string(), // <=== repeat this line 1000 to 10000 times.
    ]
}

Build with cargo build. I'm using Rust 1.72 on aarch64-apple-darwin.

In addition to the slow compilation time, I also see large memory usage. With 2500 lines, my OS reported rustc as consuming 20GB.

Exploration

Different versions

There was no obvious correlation.

Version Time (sec)
1.63 18.984
1.64 19.224
1.65 18.797
1.66 18.993
1.67 18.917
1.68 17.994
1.69 18.349
1.70 21.379
1.71 20.655
1.72 22.587
image

Different numbers of lines

This appears to show time is proportional to (number of lines)^2

Lines Time
500 0.133
1000 2.831
1500 6.512
2000 13.545
2500 22.404
3000 35.894
image

String slices

Switching to returning a &'static str instead of String resulted in fast compilation (<250 ms).

Using a function

Creating a helper function and calling it inside of vec![] resulted in fast compilation (<250 ms).

fn make_string() -> String { "monster".to_string() }

Using another method

Switching to returning a usize instead of a String has reduced compilation time but still surprisingly slow (2.5 sec).

vec![
    "monster".len(),
]

Avoiding async fn

Removing async from make_menagerie has reduced compilation time (<750 ms)

Test case generation

For trying different things, I used two files

main.rs

fn main() {
    make_menagerie();
}

async fn make_menagerie() -> Vec<String> {
    include!("repeat.rs")
}

And to generate 2500 lines:

(echo 'vec!['; yes '"monster".to_string(),' | head -n 2500; echo ']') > src/repeat.rs

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-async-awaitArea: Async & AwaitAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.C-optimizationCategory: An issue highlighting optimization opportunities or PRs implementing suchI-compilememIssue: Problems and improvements with respect to memory usage during compilation.I-compiletimeIssue: Problems and improvements with respect to compile times.P-mediumMedium priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-asyncWorking group: Async & await

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions