Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deeply nested structs consume quadratic stack space #61

Open
dtolnay opened this issue Aug 1, 2022 · 1 comment
Open

Deeply nested structs consume quadratic stack space #61

dtolnay opened this issue Aug 1, 2022 · 1 comment
Labels

Comments

@dtolnay
Copy link
Owner

dtolnay commented Aug 1, 2022

use serde::Serialize;

#[derive(Serialize)]
struct Node {
    k: Option<Box<dyn erased_serde::Serialize>>,
}

fn main() {
    std::thread::Builder::new()
        .stack_size(2 * 1024 * 1024)
        .name("test".to_string())
        .spawn(move || {
            let mut node = None::<Box<dyn erased_serde::Serialize>>;
            for _ in 0..100 {
                node = Some(Box::new(Node { k: node }));
            }
            let json = serde_json::to_string(&node).unwrap();
            println!("{json}");
        })
        .unwrap()
        .join()
        .unwrap();
}

Backtrace according to gdb

@dtolnay
Copy link
Owner Author

dtolnay commented Aug 1, 2022

Cleaner backtrace

use serde::Serialize;

#[derive(Serialize)]
struct Node<'a> {
    k: &'a dyn erased_serde::Serialize,
}

fn main() {
    std::thread::Builder::new()
        .stack_size(2 * 1024 * 1024)
        .name("test".to_string())
        .spawn(move || {
            let mut node = Node { k: &() };
            for _ in 0..100 {
                node = Node { k: Box::leak(Box::new(node)) };
            }
            let json = serde_json::to_string(&node).unwrap();
            println!("{json}");
        })
        .unwrap()
        .join()
        .unwrap();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant