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(core): Manager::emit_filter and optimize serialization #7512

Merged
merged 4 commits into from
Aug 8, 2023

Conversation

icambron
Copy link
Contributor

@icambron icambron commented Jul 28, 2023

I emit a lot of events to multiple--but specific--windows, and one of the bottlenecks there is serialization. There are two problems:

  1. There is no public API for emitting events to specific, multiple windows
  2. The private implementation of emit_filter serializes on a per-window basis

This fixes both of those. A couple of design choices:

  1. I figured emit_filter was the most flexible option, compared to taking a HashSet of window ids or similar. It's easy enough to pass |w| some_window_ids.contains(w.label()).
  2. There's a small potential perf downside if none of the windows match filter, because we've already serialized the payload. I think this is a minor edge case but I could be wrong.

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Docs
  • New Binding issue #___
  • Code style update
  • Refactor
  • Build-related changes
  • Other, please describe:

Does this PR introduce a breaking change?

  • Yes, and the changes were approved in issue #___
  • No

Checklist

  • When resolving issues, they are referenced in the PR's title (e.g fix: remove a typo, closes #___, #___)
  • A change file is added if any packages will require a version bump due to this PR per the instructions in the readme.
  • I have added a convincing reason for adding this feature, if necessary

Other information

@icambron icambron requested a review from a team as a code owner July 28, 2023 00:39
@icambron icambron changed the title add Manager::emit_filter and optimize serialization (feat) Manager::emit_filter and optimize serialization Jul 28, 2023
assert_event_name_is_valid(event);
self
.windows_lock()
.values()
.filter(|&w| filter(w))
.try_for_each(|window| window.emit_internal(event, source_window_label, payload.clone()))
Copy link
Contributor Author

@icambron icambron Jul 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this clone was ever needed. Even when we serialized once per window, it's just creating a string, so using &payload would have been better.

@@ -1102,12 +1102,13 @@ impl<R: Runtime> WindowManager<R> {
S: Serialize + Clone,
F: Fn(&Window<R>) -> bool,
{
let emit_args = WindowEmitArgs::from(event, source_window_label, payload)?;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

serialize once, emit many times

@icambron icambron changed the title (feat) Manager::emit_filter and optimize serialization (feat: core) Manager::emit_filter and optimize serialization Jul 28, 2023
@icambron icambron changed the title (feat: core) Manager::emit_filter and optimize serialization feat(core) Manager::emit_filter and optimize serialization Jul 28, 2023
@icambron icambron changed the title feat(core) Manager::emit_filter and optimize serialization feat(core): Manager::emit_filter and optimize serialization Jul 28, 2023
@icambron icambron changed the base branch from 1.x to dev July 28, 2023 02:15
@icambron icambron changed the base branch from dev to 1.x July 28, 2023 16:15
 * added Manager::emit_filter
 * changed serialization so that it only happens once when sending to
   multiple window
@@ -1102,12 +1102,13 @@ impl<R: Runtime> WindowManager<R> {
S: Serialize + Clone,
F: Fn(&Window<R>) -> bool,
{
let emit_args = WindowEmitArgs::from(event, source_window_label, payload)?;
assert_event_name_is_valid(event);
self
.windows_lock()
Copy link
Contributor Author

@icambron icambron Aug 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One question I had here was whether it made more sense to call windows() instead of windows_lock(). This would have the effect of cloning all the windows (bad) but also of not holding the lock open (good). That seems to matter for two reasons. First, plausibly someone could write a filter that needs the lock, which would immediately cause a painful and unintuitive deadlock. Second, emitting might take a while, and ideally you could do other window things while that was happening.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When triggering events we usually clone the windows, it's safer. We should change this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lucasfernog
lucasfernog previously approved these changes Aug 7, 2023
@lucasfernog lucasfernog merged commit eeb6be5 into tauri-apps:1.x Aug 8, 2023
18 checks passed
lucasfernog-crabnebula added a commit to crabnebula-dev/tauri that referenced this pull request Sep 18, 2023
amrbashir pushed a commit that referenced this pull request Jan 16, 2024
* feat(tracing): add IPC tracing

* span for deserialization

* trace spans for IPC command handlers

* fix spans usage

* app tracing [skip ci]

* window tracing

* fix run never resolving all spans

* fix draw not entered

* change level

* feat(core): Manager::emit_filter and optimize serialization (#7512)

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>

* event spans

* lint & fix tests

* change eval to run sync

* fix instrument

* update wry

* change separator

* Update core/tauri/src/plugin.rs

Co-authored-by: Jonas Kruckenberg <118265418+CrabNejonas@users.noreply.github.com>

* Update core/tauri/src/window.rs

Co-authored-by: Jonas Kruckenberg <118265418+CrabNejonas@users.noreply.github.com>

* Update core/tauri/src/window.rs

Co-authored-by: Jonas Kruckenberg <118265418+CrabNejonas@users.noreply.github.com>

* Update core/tauri/src/window.rs

Co-authored-by: Jonas Kruckenberg <118265418+CrabNejonas@users.noreply.github.com>

* Update core/tauri/src/window.rs

Co-authored-by: Jonas Kruckenberg <118265418+CrabNejonas@users.noreply.github.com>

* instrument separators

* remove on_event trace

* skip all arguments on App::build tracing

* ipc spans adjustments

* delete change file

* improve how request and response are added as values (serialize)

* do not run evalScript sync on android :( freezes the app

* wry 0.35.2

* add change file

---------

Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
Co-authored-by: Jonas Kruckenberg <118265418+CrabNejonas@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🔎 In audit
Development

Successfully merging this pull request may close these issues.

None yet

2 participants