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

[feat] improve performances of invoke by discarding use of serde_json::Value #5641

Open
LucasJavaudin opened this issue Nov 17, 2022 · 1 comment

Comments

@LucasJavaudin
Copy link
Contributor

Describe the problem

I analysed the code of tauri because I wanted to improve the performances of the invoke function (on my project, it takes 30s to read 500Mb of data with a call to invoke while simply reading and writing it in JSON takes only 5s).

From what I understood by reading the code, here is what happen when we call the invoke function:

  1. The Rust function is called [tauri-macros/src/command/wrapper.rs:158]
  2. Its output is serialized as a serde_json::Value [tauri/src/command.rs:197]
  3. The serde_json::Value is serialized as a serde_json::Value (see fix double serialize on invoke #5639) [tauri/src/hooks.rs:196]
  4. The serde_json::Value is serialized as a Json String [tauri/src/api/ipc.rs:69]
  5. The Json String is converted to RawValue, potentially copying all the String in memory [tauri/src/api/ipc.rs:70]
  6. The RawValue is converted to an escaped String [tauri/src/api/ipc.rs:85]
  7. The escaped String is put in a larger JS callback function String [tauri/src/api/ipc.rs:174]
  8. The String is evaluated in JS [tauri/src/window.rs:1289]

I think that three serialization is overkill, the serialization in 3. can be avoided (see #5639) and probably the serialization in 4. too (also it requires a bit more work).

String allocation might be optimized but this is not what takes the longest running time in my opinion.

Describe the solution you'd like

Instead of serializing the result of the invoked function to a serde_json::Value, it should be serialized directly to a Json String.

Alternatives considered

No response

Additional context

Issue #4026 discusses memory leaks when using the invoke command. My tests also showed memory leaks. These two problems (performance issues and memory leaks) are related.

@wusyong
Copy link
Member

wusyong commented Nov 25, 2022

I opened a discussion in #5690 to briefly summarize current status and some workaround we can choose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 📬Proposal
Development

No branches or pull requests

2 participants