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

Immutable state #81

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open

Immutable state #81

wants to merge 14 commits into from

Conversation

CogentRedTester
Copy link
Owner

@CogentRedTester CogentRedTester commented Nov 4, 2022

This is an experiment to make the main browser state variable immutable via metatables.
This means that any change to the state table results in the creation of a new state table.

The __index metamethod is used to layer multiple state tables on top of each other to
optimise this so we aren't duplicating everything when moving the cursor. It also
opens up the ability for default or fallback state values and will potentially
make some cacheing work better.

This is a very significant refactor, and will require changes to huge areas of code. It may
not be worth it and may never be merged. However, there are numerous areas of the script
where state values need to be cached to prevent changes; the directory cache obviously,
but also custom keybind execution and playlist loading. This is due to the asynchronous
nature of parse operations and keybinds, which means that the global state table can change
in the middle of these operations. Making state immutable opens up numerous possible ways
to simplify this fragile code and prevent any accidental changes during runtime.

these are properties that we don't really want to be immutable
this will be what enforces immutability at runtime
This is where the magic happens. Allowing for different levels of state
changes while ensuring any specific state reference is immutable.

Not yet tested for bugs.
This makes it much easier to make incremental changes to the current
state.

Also added a print function to maker debugging state values easier.
Only the most basic operations have been ported, there is still a LOT
of broken functionality.
Since the readonly tables contain a reference to the original table we
can't make the keys weak, at least not in Lua5.1. In Lua 5.2 weakly
keyed tables act as ephemeron tables, so they would probably work
there. I don't have a Lua5.2 build of mpv, so I can't test, but it might
be worth using ephemeral tables when Lua5.2 is detected.
String manipulation is quite costly in Lua, this significantly reduces
the number of garbage string produced and should help to speed up
scrolling and reduce garbage collection.

Reducing garbage collection is quite important since it directly impacts
the performance of read-only tables lookups by clearing the cache.
This prevents the sub-tables from being garbage collected from the cache
until the base read-only table has been garbage collected.

This should significantly reduce the number of new read-only tables that
are created while scrolling.
--this is to handle cyclic table references
return copy_table_recursive(t, {}, depth or math.huge)
return copy_table_recursive(t, {}, depth or math.huge, store_original or true)
Copy link
Owner Author

Choose a reason for hiding this comment

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

this is a bug, if store_original is false then it will fail the or check and true will be passed anyway

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.

1 participant