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

Slow UI Render #7112

Open
wanglunhui2012 opened this issue Dec 16, 2024 · 3 comments
Open

Slow UI Render #7112

wanglunhui2012 opened this issue Dec 16, 2024 · 3 comments
Labels
bug Something isn't working need triaging Issue that the owner of the area still need to triage needs info Further information from the reporter is requested

Comments

@wanglunhui2012
Copy link

Bug Description

I wan't to display http request result, but ui become very slow.

Reproducible Code (if applicable)

export component AppWindow inherits Window {
    in-out property <string> url: "https://github.com/slint-ui/slint/issues/747";
    in-out property <string> method: "GET";
    in-out property <string> header: "";
    in-out property <string> body: "";
    in-out property <string> response: "";
    callback send_request();
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let ui = AppWindow::new()?;

    ui.on_send_request({
        let ui_handle = ui.as_weak();
        move || {
            let ui = ui_handle.unwrap();

            let url = ui.get_url();
            let method = ui.get_method();
            let header = ui.get_header();
            let body = ui.get_body();

            let response_display = ui.as_weak();
            tokio::spawn(async move {
                let client = Client::new();

            let mut request = match method.as_str() {
                "GET" => client.get(url.as_str()),
                "POST" => client.post(url.as_str()),
                _ => return,
            };

            if method == "POST" && !body.is_empty() {
                request = request.body(body);
            }
            let response = request.send().await;
                if let Ok(res) = response {
                    let status = res.status().to_string();
                    let headers = res
                        .headers()
                        .iter()
                        .map(|(k, v)| format!("{}: {}", k, v.to_str().unwrap_or_default()))
                        .collect::<Vec<_>>()
                        .join("\n");
                    let body = res.text().await.unwrap_or_default();

                    let result = format!("Status: {}\n\nHeaders:\n{}\n\nBody:\n{}", status, headers, body);
                    println!("{result}");
                    slint::invoke_from_event_loop(move || {
                        println!("1");
                        response_display.unwrap().set_response(SharedString::from(body));
                        println!("2");
                    });
                    println!("3");
                } else {
                    response_display.upgrade_in_event_loop(move |handle| {
                        handle.set_response(SharedString::from("Request Failed"));
                    });
                }
}
    });

    ui.run()?;

    Ok(())
}

Environment Details

  • Slint Version: 1.8.0
  • Platform/OS: MacOS Arm64
  • Programming Language: Rust
  • Backend/Renderer: Default

Product Impact

No response

@wanglunhui2012 wanglunhui2012 added bug Something isn't working need triaging Issue that the owner of the area still need to triage labels Dec 16, 2024
@tronical
Copy link
Member

I suspect that your main thread ends up hanging if Tokio doesn't hand off the task to a thread. Could you try to apply the workaround mentioned at the bottom of this page to see if that fixes it?

https://docs.slint.dev/latest/docs/rust/slint/fn.spawn_local#compatibility-with-tokio-and-other-runtimes

@tronical tronical added the needs info Further information from the reporter is requested label Dec 16, 2024
@wanglunhui2012
Copy link
Author

I suspect that your main thread ends up hanging if Tokio doesn't hand off the task to a thread. Could you try to apply the workaround mentioned at the bottom of this page to see if that fixes it?

https://docs.slint.dev/latest/docs/rust/slint/fn.spawn_local#compatibility-with-tokio-and-other-runtimes

But when I replace response_display.unwrap().set_response(SharedString::from(body)); to response_display.unwrap().set_response(SharedString::from("success"));, it work well. I think it is because the response is too long.

@ogoffart
Copy link
Member

ogoffart commented Jan 2, 2025

How big is the response?
is it measured in kB or in MB?
In your code i see you print "1" and "2" and things like that. At what point does it slow down.
I see that you also print the body to the console. That could also be the slow part.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working need triaging Issue that the owner of the area still need to triage needs info Further information from the reporter is requested
Projects
None yet
Development

No branches or pull requests

3 participants