GithubHelp home page GithubHelp logo

Comments (2)

Xudong-Huang avatar Xudong-Huang commented on May 18, 2024

rand is using a big stack size in it's implementation.
for debug version stack usage

coroutine name = None, stack size = 40959,  used size = 9205

for release version

coroutine name = None, stack size = 40959,  used size = 2740

here is the modified test

#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate may;
extern crate rand;

use std::io::Read;
use std::collections::HashMap;

// the ThreadRng is using a big stack!
lazy_static! {
    static ref DATA: Vec<u8> = vec![rand::random::<u8>()];
}

// the OsRng is not using a big stack, you can safely use it in coroutine context
/*
lazy_static! {
    static ref DATA: Vec<u8> = {
        use rand::Rng;
        let mut rand = rand::OsRng::new().unwrap();
        vec![rand.gen()]
    };
}
*/
fn main() {
    may::config().set_stack_size(4096*10 - 1);
    let mut entries: HashMap<usize, Vec<u8>> = HashMap::new();

    // force init DATA in the main thread
    // let _ = DATA[0];

    let task = go!(move || {
        entries.insert(0, DATA.clone());
    });

    task.join().unwrap();
    std::io::stdin().read(&mut [0]).ok();
}

the solution is init the DATA in main thread (not in the coroutine context, just like what you did to pass the test), or use the os rand interface which doesn't use a huge stack.

from may.

alanhoff avatar alanhoff commented on May 18, 2024

Now it makes perfect sense. Static values declared with lazy_static! only get evaluated once the value is used, that's why lazy (d'oh), and if it's being used inside the coroutine context it also affect's it's stack. I'm such a noob.

Thanks for your time and patience!

from may.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.