Skip to content

Large BTreeMap key or value types cause avoidable stack overflow #81444

Open
@ssomers

Description

@ssomers

I tried this code:

use std::collections::BTreeSet;

fn main() {
    let mut s = BTreeSet::new();
    s.insert([0u8; 19580]);
}

built with plain rustc on 64 bit Windows (it happens in release code too but not with this example).
I expected to see the program finishing silently.

Instead, this happened:

thread 'main' has overflowed its stack
fatal runtime error: stack overflow
Aborted

This becomes sporadic if you decrease the size of the key and disappears below 19540 bytes (on my system).

On my Linux box, and on playground, the size can be increased to somewhere between 160_000 and 170_000 bytes before the stack overflows.

rustc --version --verbose:

rustc 1.51.0-nightly (d1aed50ab 2021-01-26)
binary: rustc
commit-hash: d1aed50ab81df3140977c610c5a7d00f36dc519f
commit-date: 2021-01-26
host: x86_64-unknown-linux-gnu
release: 1.51.0-nightly
LLVM version: 11.0.1

This is not necessarily a bug, since there has to be some system-dependent limit. And it's not smart to inline big chunks of data as key or value, because usually about half of the key-value space allocated by BTreeMap remains unused. But the limit on Windows is much lower than I expected (and before #81494 can apparently easily be lifted improved by using box instead of Box::new for the construction of btree nodes).

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-collectionsArea: `std::collections`C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-libsRelevant to the library 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