-
Notifications
You must be signed in to change notification settings - Fork 8
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
CogentRedTester
wants to merge
14
commits into
master
Choose a base branch
from
immutable-state
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Immutable state #81
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
The copies are not readonly.
Only the most basic operations have been ported, there is still a LOT of broken functionality.
CogentRedTester
force-pushed
the
immutable-state
branch
from
November 5, 2022 06:59
972f241
to
a051677
Compare
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.
CogentRedTester
commented
Feb 17, 2023
--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) |
There was a problem hiding this comment.
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 tooptimise 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.