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

Add TrackedEvents for better event cleanup #2617

Closed
wants to merge 2 commits into from
Closed

Conversation

ffreyer
Copy link
Collaborator

@ffreyer ffreyer commented Jan 19, 2023

Description

Events are hard to clean up because every child scene has the same event struct, i.e. the same observables, containing all callbacks.
This is an attempt to solve this with two wrappers TrackedObservable and TrackedEvents. Each child scene gets its own TrackedEvents(parent.events) which mirrors Events except that every observable is wrapped in a TrackedObservble. When calling on etc on any of those, the registered callback is tracked and added to the shared event observable. This means that every child scenes keeps track of its "local" event callbacks, which can then be cleaned up on empty!(scene).

I tested this with the same Menu deleting code from https://discourse.julialang.org/t/removing-row-from-gridlayout/93196 and it seems to do its job.

See also: #2614

Type of change

  • Bug fix?

Checklist

  • Cleanup
  • Added an entry in NEWS.md (for new features and breaking changes)
  • Added or changed relevant sections in the documentation
  • Added unit tests for new algorithms, conversion methods, etc.
  • Added reference image tests for new plotting functions, recipes, visual options, etc.

Comment on lines +160 to +171
# # Compat only
# function Base.getproperty(e::Events, field::Symbol)
# if field === :mousebuttons
# error("`events.mousebuttons` is deprecated. Use `events.mousebutton` to react to `MouseButtonEvent`s instead.")
# elseif field === :keyboardbuttons
# error("`events.keyboardbuttons` is deprecated. Use `events.keyboardbutton` to react to `KeyEvent`s instead.")
# elseif field === :mousedrag
# error("`events.mousedrag` is deprecated. Use `events.mousebutton` or a mouse state machine (`addmouseevents!`) instead.")
# else
# return getfield(e, field)
# end
# end
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This was just there to give people a useful error message when updating from an older version of the Events struct. I think it's fine to delete this now?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

2 years since those fieldnames were changed, 8 months since the warning was turned into an error

@MakieBot
Copy link
Collaborator

MakieBot commented Jan 19, 2023

Compile Times benchmark

Note, that these numbers may fluctuate on the CI servers, so take them with a grain of salt. All benchmark results are based on the mean time and negative percent mean faster than the base branch. Note, that GLMakie + WGLMakie run on an emulated GPU, so the runtime benchmark is much slower. Results are from running:

using_time = @ctime using Backend
# Compile time
create_time = @ctime fig = scatter(1:4; color=1:4, colormap=:turbo, markersize=20, visible=true)
display_time = @ctime Makie.colorbuffer(display(fig))
# Runtime
create_time = @benchmark fig = scatter(1:4; color=1:4, colormap=:turbo, markersize=20, visible=true)
display_time = @benchmark Makie.colorbuffer(display(fig))
using create display create display
GLMakie 31.52s (31.34, 31.68) 0.11+- 16.40s (16.24, 16.49) 0.08+- 15.35s (15.17, 15.66) 0.16+- 11.82ms (11.65, 11.96) 0.10+- 132.95ms (131.40, 133.61) 0.84+-
master 31.42s (31.15, 31.78) 0.20+- 16.31s (16.17, 16.40) 0.09+- 15.20s (15.06, 15.30) 0.09+- 11.58ms (11.44, 11.68) 0.09+- 132.10ms (128.94, 133.50) 1.58+-
evaluation +0.32%, 0.1s invariant (0.62d, 0.27p, 0.16std) +0.57%, 0.09s invariant (1.13d, 0.06p, 0.08std) +1.01%, 0.15s slower X (1.22d, 0.05p, 0.12std) +2.01%, 0.24ms slower X (2.60d, 0.00p, 0.09std) +0.63%, 0.84ms invariant (0.67d, 0.24p, 1.21std)
CairoMakie 32.36s (32.03, 32.58) 0.20+- 19.74s (19.66, 19.84) 0.06+- 3.01s (2.93, 3.09) 0.06+- 13.67ms (13.30, 14.06) 0.25+- 5.41ms (5.12, 5.51) 0.13+-
master 32.41s (31.63, 32.91) 0.44+- 19.61s (18.81, 20.07) 0.39+- 3.01s (2.90, 3.07) 0.06+- 13.49ms (13.19, 13.84) 0.25+- 5.46ms (5.25, 5.70) 0.14+-
evaluation -0.15%, -0.05s invariant (-0.14d, 0.79p, 0.32std) +0.66%, 0.13s invariant (0.47d, 0.41p, 0.23std) -0.02%, -0.0s invariant (-0.01d, 0.98p, 0.06std) +1.38%, 0.19ms invariant (0.76d, 0.18p, 0.25std) -0.94%, -0.05ms invariant (-0.37d, 0.50p, 0.14std)
WGLMakie 36.03s (35.71, 36.35) 0.21+- 19.90s (19.59, 20.12) 0.21+- 23.36s (23.12, 23.76) 0.26+- 14.55ms (13.74, 15.24) 0.57+- 2.47s (2.39, 2.56) 0.07+-
master 36.07s (35.81, 36.34) 0.21+- 19.82s (19.70, 19.92) 0.09+- 23.41s (23.10, 23.83) 0.29+- 14.05ms (13.41, 15.43) 0.70+- 2.37s (2.25, 2.53) 0.11+-
evaluation -0.11%, -0.04s invariant (-0.18d, 0.74p, 0.21std) +0.43%, 0.09s invariant (0.52d, 0.36p, 0.15std) -0.23%, -0.05s invariant (-0.20d, 0.72p, 0.27std) +3.45%, 0.5ms invariant (0.79d, 0.17p, 0.64std) +4.06%, 0.1s invariant (1.09d, 0.07p, 0.09std)

@jkrumbiegel
Copy link
Member

I think this is not necessary anymore with the changes in #2731 @SimonDanisch? You introduce some kind of tracking there, too.

@SimonDanisch
Copy link
Member

I don't really like the dependence on a new Observable type (it's somewhat breaking because we haven't really used AbstractObservable anymore since I've been determined not to use too many subtypes), so I'll close this for now! @ffreyer, let me know if there's something missing in #2731, besides, that users have to take care of this by using the on/map(f, scene/plot, ...) functions...
I think we should soon update our event api, to improve the decoupling from the scene even more anyways!
E.g.:

on(plot, :projection) do projection 
...
end
on(plot, :mouseposition) do position
end

Which will then gets only connected when displayed on a certain scene...

@ffreyer ffreyer deleted the ff/local_events branch March 15, 2023 15:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants