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

defaults.lua: add an exit() function #15235

Merged
merged 3 commits into from
Nov 2, 2024
Merged

Conversation

guidocella
Copy link
Contributor

commit 1: defaults.lua: add an exit() function

Scripts can terminate execution by setting mp.keep_running = false. Add an exit() function to wrap setting mp.keep_running and properly expose this feature. It can be used e.g. by a thumbnail script to spawn workers with load-script and then let them quit.

It is not added to the mp namespace as mp.exit because that would make it look like it terminates mpv.

commit 2: auto_profiles.lua: actually exit when no auto profiles are defined

Unsetting _G.mp_event_loop at the top level quits the script, but not within callbacks. Use the new exit() function instead. Fixes e2284fb.

This actually has an edge case since e2284fb where you can add auto profiles only later with load-config-file and the script stays unloaded, but it's still reasonable to quit if mpv.conf has no conditional profiles. You could always explicitly set --load-auto-profiles=yes in this case.

Copy link

github-actions bot commented Nov 1, 2024

Download the artifacts for this pull request:

Windows
macOS

@guidocella guidocella force-pushed the lua-exit branch 2 times, most recently from 6668eb2 to e2547ca Compare November 1, 2024 15:52
@kasper93
Copy link
Contributor

kasper93 commented Nov 1, 2024

Scripts can terminate execution by setting mp.keep_running = false. Add an exit() function to wrap setting mp.keep_running and properly expose this feature.

What is the use-case for that?

It can be used e.g. by a thumbnail script to spawn workers with load-script and then let them quit.

Worker scripts should never exit or be reloaded, it is wasteful. They should register script-message and wait for that. See for example how gallery-thumbgen.lua is implemented.

If the script added key bindings, you should remove them before calling

That's why exit() is not a good way to close script. People will forget to unbind everything. Scripts should generally unbind their hooks and do nothing after. Forcefully closing their event loop is not expected by all the bookkeeping in mpv.

@guidocella guidocella force-pushed the lua-exit branch 2 times, most recently from 0fa0c7a to 44f2ad2 Compare November 1, 2024 16:13
@avih
Copy link
Member

avih commented Nov 1, 2024

It can be used e.g. by a thumbnail script to spawn workers with load-script and then let them quit.

Worker scripts should never exit or be reloaded, it is wasteful. They should register script-message and wait for that. See for example how gallery-thumbgen.lua is implemented.

No. It's wasteful to keep them running doing nothing. Each has a scripting VM which just idles. It's nicer to launch when needed and terminate when no longer needed.

If the script added key bindings, you should remove them before calling

That's why exit() is not a good way to close script.

That note was added originally before mpv added that functionality (even if the commit dates show otherwise), but it's no longer true, and removed in the followup force-push. Good catch by @guidocella .

@guidocella
Copy link
Contributor Author

Worker scripts should never exit or be reloaded, it is wasteful. They should register script-message and wait for that. See for example how gallery-thumbgen.lua is implemented.

That's literally the script we discussed in #mpv, isn't it more wasteful to always keep 6+ threads of worker scripts running? auto_profiles.lua also uses it.

That's why exit() is not a good way to close script. People will forget to unbind everything. Scripts should generally unbind their hooks and do nothing after. Forcefully closing their event loop is not expected by all the bookkeeping in mpv.

Actually key bindings were already removed automatically, I updated the commits.

@kasper93
Copy link
Contributor

kasper93 commented Nov 1, 2024

That's literally the script we discussed in #mpv, isn't it more wasteful to always keep 6+ threads of worker scripts running? auto_profiles.lua also uses it.

No. It's wasteful to keep them running doing nothing. Each has a scripting VM which just idles. It's nicer to launch when needed and terminate when no longer needed.

Do you have profiling that would confirm this claim? Idling sleeping threads are almost free and you have hundreds if not thousands of them on your system. It is common pattern to have thread pool with worker threads waiting for a tasks to process. Even mpv has thread pool implemented.

Latency of starting thread / creating client context / starting VM / parsing script each time we want to do anything will increase latency of all operations on it.

@avih
Copy link
Member

avih commented Nov 1, 2024

Do you have profiling that would confirm this claim?

You don't need a profiler for that. They don't consume meaningful CPU resources, but each scripting VM uses between hunderds of KB to many MB of RAM. It's wasteful to keep them running when they're no longer needed.

(they do consume a bit CPU, because all mpv events reach all scripts by default, and GC runs once in a while due to that, but it is negligible)

It is common pattern to have thread pool with worker threads waiting for a tasks to process. Even mpv has thread pool implemented.

Sure. and a very valid use case. But the system doesn't enforce keeping things in memory after they're used. The code should do that explicitly (or in our case, decide whether it wants to quit the worker scripts or keep them running).

This is the same as in any other kind of environment. Except for caching purposes (which the code should decide for itself what it wants to cache), you don't keep stuff around once it's no longer needed.

Latency of starting thread / creating client context / starting VM / parsing script each time we want to do anything will increase latency of all operations on it.

This is for the script to decide. They're not required to quit the worker scripts. Launching a scripting VM is low single-digit ms at most. I'm guessing that in the case of the thumbnails script it's negligible compared to generating even a single thumnail image.

@kasper93
Copy link
Contributor

kasper93 commented Nov 1, 2024

Launching a scripting VM is low single-digit ms at most.

Again, do you have any profiling to support your claims? You make a lot of statements, but none of them are backed up with evidence. I won’t discuss this further; throwing unverifiable claims back and forth doesn’t make sense.

@avih
Copy link
Member

avih commented Nov 1, 2024

Again, do you have any profiling to support your claims?

I tested that in the past.

I measured launching a mujs JS VM in mpv about 10 years ago, and it was about ~ 4 ms (lanching the VM, parsing and running defaults.js, and then continuing to parsing and running a minimal user script).

It should be faster with more modern systems. Lua should launch quicker because it has a lot less thing to init on startup (JS has many internal objects which are initialized unconditionally in mujs, like Date, Math, etc, which I believe lua doesn't have, or at least a lot less).

That being said, even if it was 100ms - and it's definitely definitely orders of magnitude less, it's still for the script to decide at which point pooling/cachine makes sense. Maybe it's always, maybe it's never. The script is not required to quit, and they behave the same as before.

We're simply adding an option which allows them to quit, if they so decide.

You make a lot of statements, but none of them are backed up with evidence.

Even if irrelevant because it doesn't matter how long it takes, only the "low-digit ms" needs evidence as far as I can tell - which I've given, unless you mean other things too.

The other things they don't need evidence as far as I can tell. Not keeping resources around once they're no longer needed, except for caching - which the code should decide for itself what it wants to cache/pool, and for how long, is a generally correct statement without requiring any evidence to back it up.

Are you actually arguing that we should not allow scripts to exit once they decide that they have nothing else to do?

if yes, then just expplain why we shouldn't allow it instead of requestig evidence to things which need none.

If no, then what else do you want?

@guidocella guidocella force-pushed the lua-exit branch 2 times, most recently from 79f1c74 to f4dd579 Compare November 1, 2024 18:13
@avih
Copy link
Member

avih commented Nov 1, 2024

Recent docs changes LGTM. Thanks.

I'll merge it a bit later if there are no objections.

Scripts can terminate execution by setting mp.keep_running = false. Add
an exit() function to wrap setting mp.keep_running and properly expose
this feature. It can be used e.g. by a thumbnail script to spawn workers
with load-script and then let them quit.

It is not added to the mp namespace as mp.exit because that would make
it look like it terminates mpv.

This mirrors the exit() function which already exists in js.

The note in javascript.rst about having to remove key bindings before
exit is not kept because they are actually removed automatically since
bf385e1 (though it was accurate when the JS backend was developed
before upstreaming it).
Unsetting _G.mp_event_loop at the top level quits the script, but not
within callbacks. Use the new exit() function instead. Fixes e2284fb.

This actually has an edge case since e2284fb where you can add auto
profiles only later with load-config-file and the script stays unloaded,
but it's still reasonable to quit if mpv.conf has no conditional
profiles. You could always explicitly set --load-auto-profiles=yes in
this case.
It's a commonly used function so group it with the documented functions.
@avih avih merged commit a9d5793 into mpv-player:master Nov 2, 2024
26 checks passed
@avih
Copy link
Member

avih commented Nov 2, 2024

Thanks.

@guidocella guidocella deleted the lua-exit branch November 2, 2024 12:03
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.

3 participants