procyon-rs / vega_lite_4.rs

rust api for vega-lite v4
Apache License 2.0
26 stars 7 forks source link

RuntimeError: memory access out of bounds at vega_lite_4::schema::VegaliteBuilder::build #12

Closed villasv closed 3 years ago

villasv commented 3 years ago

First of all, thanks for this library. By reading the create's docs.rs I've been mostly able to sort things out by myself. I've been stuck on a problem though:

Uncaught (in promise) RuntimeError: memory access out of bounds
    at core::ptr::mut_ptr::<impl *mut T>::write::hbd79f37a0f1914eb (http://localhost:8000/index.wasm:wasm-function[7714]:0x398878)
    at alloc::boxed::Box<T,A>::new_in::h0250b8b8d4a54e23 (http://localhost:8000/index.wasm:wasm-function[7116]:0x38b0dd)
    at <alloc::boxed::Box<T,A> as core::clone::Clone>::clone::h25ea53d373a203c1 (http://localhost:8000/index.wasm:wasm-function[6276]:0x376381)
    at <core::option::Option<T> as core::clone::Clone>::clone::h2b95bcd3023e7cfe (http://localhost:8000/index.wasm:wasm-function[5751]:0x3678ba)
    at vega_lite_4::schema::VegaliteBuilder::build::h4d179f9881c378f5 (http://localhost:8000/index.wasm:wasm-function[103]:0x11d2e3)
    at b4_site::build_graph::h30e51edf5139e78d (http://localhost:8000/index.wasm:wasm-function[168]:0x182a12)
    at <b4_site::Main as yew::html::Component>::view::hc7155747ddf75f9e (http://localhost:8000/index.wasm:wasm-function[455]:0x203e03)
    at <COMP as yew::html::Renderable>::render::h29c2b45f513b97e6 (http://localhost:8000/index.wasm:wasm-function[11576]:0x3db7cd)
    at <yew::html::scope::UpdateComponent<COMP> as yew::scheduler::Runnable>::run::hd519d19d79ee1594 (http://localhost:8000/index.wasm:wasm-function[216]:0x1af228)
    at yew::scheduler::Scheduler::start::h999c83794cd54047 (http://localhost:8000/index.wasm:wasm-function[773]:0x23ed57)

The issue happens if I uncomment the axis related lines in the following snippet:

    let chart = VegaliteBuilder::default()
        .title("Stock price")
        .mark(DefBuilder::default()
            .def_type(Mark::Line)
            .tooltip(true)
            .build()?)
        // .data(get_historical_quotes())
        .transform(vec![
            // vega expects epoch in milliseconds
            TransformBuilder::default()
                .calculate("datum.data[0] * 1000")
                .transform_as("data.0.ms")
                .build()?,
        ])
        .encoding(EdEncodingBuilder::default()
            .x(XClassBuilder::default()
                .field("data.0.ms")
                .title("Date")
                .position_def_type(Type::Temporal)
                .time_unit(TimeUnit::Utcyearmonthdate)
                .build()?)
            .y(YClassBuilder::default()
                .field("data.1")
                .position_def_type(Type::Quantitative)
                // .axis(AxisBuilder::default()
                    // .title("Quote")
                    // .format(".2e")
                    // .build()?)
                .build()?)
            .build()?)
        .build()?

The complete project is here: https://github.com/villasv/b4/tree/15627e85080ad4daeb427b2fae079ad61f329bb5/b4-site

Am I missing something? Can you give me directions to fix this or just achieve what I want (axis & tooltip formatting)?

davidB commented 3 years ago

Hi,

We never try the lib into wasm context. FYI the data model is very large (it's why it's so long to build). Maybe there is also a runtime issue about memory. From my quick search, you can try to increase the memory allowed for wasm, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WebAssembly/Memory/grow.

I'm interested if it fixed the issue or not (in this case I'll investigate deeper tomorrow).

villasv commented 3 years ago

It seems that there is indeed a problem of memory consumption, which I confirmed by just taking a Vegalite spec that successfully builds and building it twice and the same error will show up. Kinda not sure what to do next... either go back to using JSON specs or find out exactly how much memory I'll have to pay for this lib.

davidB commented 3 years ago

I tried several approaches (I didn't know wasm), including made some changes in the vegalite_4 api (more Box to allocate on the heap).

what is working (with 2 graphs with axis enabled) is to increase the stack size to 1.5mb:

Add file b4-site\.cargo\config with:

[target.wasm32-unknown-unknown]
rustflags = [
  "-C", "link-args=-z stack-size=1500000",
]
villasv commented 3 years ago

Oooooh that solves it for me and indeed searching for that configuration I can see it popping up in situations similar to mine.

Thanks @davidB!

davidB commented 3 years ago

Thanks you for taking time to report the issue, providing a not too long reproducible case, and for the feedback about integration with wasm. I release a new version 0.6.0 that should be use less stack (and more heap) but behave like the previous one on wasm (always need to increase the stack size).