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

Call unregister for garbage collected events made by AddHandler. #270

Open
Abastro opened this issue Feb 2, 2023 · 9 comments
Open

Call unregister for garbage collected events made by AddHandler. #270

Abastro opened this issue Feb 2, 2023 · 9 comments

Comments

@Abastro
Copy link

Abastro commented Feb 2, 2023

I found that unregister is not called when events are GC-ed.
In fact, the unregister functionality is discarded entirely in fromAddHandler call.
Could you make it so that it calls unregister on GC, so relevant widgets and signal generators could be properly garbage-collected?

@HeinrichApfelmus
Copy link
Owner

That's a good point. 🤔 I will look into it.

@Abastro
Copy link
Author

Abastro commented Feb 8, 2023

I have a question regarding reactimate.
Consider the code

reactimate (foo bar <$> eBaz)

Suppose there is no other references to bar. When eBaz provably becomes never, does bar get GC-ed?
If it does not, I am worried of explosion in memory footprint.
(Dynamic widgets require reactimate calls during execution, so memory would leak if the reactimate is not taken care of)

@HeinrichApfelmus
Copy link
Owner

When eBaz provably becomes never, does bar get GC-ed?

Not at the moment, unfortunately, but eliminating events that are provably never is an optimization that I want to implement in the future.

@Abastro
Copy link
Author

Abastro commented Feb 9, 2023

I see, how about event once eBaz? Does it get GC-ed after eBaz invoked once?
EDIT: I just tested it. It seems like act is GC-ed when

eAct <- switchE (act <$ eSignal) (never <$ eFree)
reactimate eAct

but it does not when

eAct <- act <$ switchE eSignal (never <$ eFree)
reactimate eAct

So, reactimate holds the output event in direct vicinity? While connected events are discarded.

@HeinrichApfelmus
Copy link
Owner

I see, how about event once eBaz? Does it get GC-ed after eBaz invoked once?

Unfortunately, not — the optimization about never is also the optimization that would make this work for once.

So, reactimate holds the output event

Yes, reactimate ensures that the output event is reachable and not garbage collected.

@Abastro
Copy link
Author

Abastro commented Feb 11, 2023

Can I ask why reactimate ensures that output events are not garbage-collected? I thought the event source would keep a reference to the event if it needs to emit more. Is there a reason to specifically keep the output event alive?

@HeinrichApfelmus
Copy link
Owner

HeinrichApfelmus commented Feb 11, 2023

Can I ask why reactimate ensures that output events are not garbage-collected? I thought the event source would keep a reference to the event if it needs to emit more. Is there a reason to specifically keep the output event alive?

That's a good question — there are two ways in which garbage collection could operate: Either the source keeps events alive or the sink keeps them alive. I felt that I had to decide for one, and picked the sink, as this plays better with dynamic event switching: We can create many events that stem from the same source, which are at some point in their life cycle connected to a sink, but then switched away from the sink forever. As these events eventually won't have any observable effect, we need to remove them. I think that the other direction can be fully captured by "optimizing never".

@Abastro
Copy link
Author

Abastro commented Feb 11, 2023

Somehow I thought it was opposite! I guess this means each reactimate adds extra overhead. Is this correct?

@HeinrichApfelmus
Copy link
Owner

I guess this means each reactimate adds extra overhead. Is this correct?

Depends on what you mean by "overhead". The evaluation of events still proceeds by pushing from the source; so if the event does not reach a sink, it does not add overhead in the evaluation. That said, the sink does keep events alive, so that in that sense, it adds overhead.

As said, implementing "optimizing never" is on my agenda and will make it possible to remove both sources and sinks.

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

No branches or pull requests

2 participants