Rust v8 isolate at < 10MB / isolate possible?

At rusty_v8 - Rust there is sample code for creating Rust v8 isolates. On a machine with 100+ GB of RAM, I can't even create 100 of these isolates without hitting out of memory. It is not obvious to me how to tweak the heap sizes of the isolates.

Is it possible to modify the sample code to create isolates where each takes memory < 10MB ? (I'd like to be able to run 1_000 on 10GB and 10_000 on 100G).

Thanks!

Does this work? heap_limits

I played with that parameter, could not find values to make it work. If you have sample code, I'll be happy to try.

What's your code for creating the isolates?

Also that crate says to use v8 instead: https://crates.io/crates/rusty_v8

1 Like
use std::rc::Rc;
use std::time::Instant;

pub fn main() {
    let mut out = vec![];

    let platform = v8::new_default_platform(32, false).make_shared();
    v8::V8::initialize_platform(platform);
    v8::V8::initialize();

    println!("default: {:?}", v8::CreateParams::default());

    let mut start = Instant::now();
    for i in 0..10_000 {
        let mut isolate = v8::Isolate::new(v8::CreateParams::default().heap_limits(1000, 1_000_000));
        {
            let scope = &mut v8::HandleScope::new(&mut isolate);
            let context = v8::Context::new(scope);
            let scope2 = &mut v8::ContextScope::new(scope, context);

            let code = v8::String::new(scope2, "'Hello' + ' World!'").unwrap();
            // println!("javascript code: {}", code.to_rust_string_lossy(scope2));

            let script = v8::Script::compile(scope2, code, None).unwrap();
            let result = script.run(scope2).unwrap();
            let result = result.to_string(scope2).unwrap();
            // println!("result: {}", result.to_rust_string_lossy(scope2));
        }

        if i % 100 == 0 && i != 0 {
            let now = Instant::now();
            println!("index: {:?}; avg: {:?}", i, (now - start) / 100);
            start = now;
        }

        out.push(Rc::new(isolate));
    }
}

output:

index: 200; avg: 2.574939ms
index: 300; avg: 3.149126ms
index: 400; avg: 3.761651ms
index: 500; avg: 4.355249ms
index: 600; avg: 4.871013ms
index: 700; avg: 5.428162ms
index: 800; avg: 6.016724ms
index: 900; avg: 6.574512ms
index: 1000; avg: 7.119134ms
index: 1100; avg: 7.821027ms
index: 1200; avg: 8.3833ms
index: 1300; avg: 8.744837ms

It seems, as we create more V8's, the time it takes to create moreV8's increases linearly ?

Also, according to top, it seems it is killing one CPU at 100% instead of using 32 cores.

Looks like it's logarithmic. When I run this, it levels off after 1000. It's really inconsistent what timing it levels off at, though.

You're only using one thread, so it's not surprising that only one core is doing anything.

I'm not sure how this method is supposed to work. It seems like only values between 2^28 and 2^32 affect the initial size. Here's the power of 2 and its resulting total_heap_size

27 1773568
28 1773568
29 4919296
30 9113600
31 17240064
32 17502208
33 17502208

It might be expecting some kind of bitmask.