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

Clean up variables package #5411

Merged
merged 7 commits into from
Mar 24, 2023

Conversation

TPGamesNL
Copy link
Member

Description

Cleans up the variables package, a lot of reformatting and refactoring. Adds many Javadocs and regular code comments. Improved readability.
Some functional changes:

  • fix Variables.copyLocalVariables not perfectly copying
  • fix wrong variable being returned from Variables.getVariable when a lot of variable changes were being made
  • fix a wrong return case in the variable name comparator

This PR doesn't touch DatabaseStorage, to avoid more conflicts with #4873.


Target Minecraft Versions: any
Requirements: none
Related Issues: #5398

Fix VariablesMap copying not making a deep copy
Fix wrong variable being returned during peak variable changes
Fix variable name comparator returning wrong value in some cases
@TPGamesNL TPGamesNL added bug An issue that needs to be fixed. Alternatively, a PR fixing an issue. enhancement Feature request, an issue about something that could be improved, or a PR improving something. variables Related to variables and/or storing them. labels Jan 29, 2023
Copy link
Member

@Moderocky Moderocky left a comment

Choose a reason for hiding this comment

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

Looks like good improvements.

@Override
protected boolean save(String name, @Nullable String type, @Nullable byte[] value) {
synchronized (connectionLock) {
synchronized (changesWriter) {
Copy link
Member

Choose a reason for hiding this comment

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

I'm concerned we might deadlock here if something asks for connection lock in order to wake the writer, but since this is old code it doesn't seem to have been a problem.

Copy link
Member Author

Choose a reason for hiding this comment

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

Does Java deadlock when trying to acquire a lock which has already been acquired somewhere up the stack? I thought it had sth for that

Copy link
Member

Choose a reason for hiding this comment

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

I can't remember if the outer lock will become available for the purposes of notifying when the inner one is waiting, I'll have to check.

} catch (IOException e) {
Skript.error("Unable to make a final save of the database '" + databaseName +
"' (no variables are lost): " + ExceptionUtils.toString(e));
// FIXME happens at random - check locks/threads
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if this is related to the aforementioned problem of obtaining one lock without the other? I will look into this later.


// Get the amount of variables loaded by this variables storage object
int newVariablesLoaded;
synchronized (TEMP_VARIABLES) {
Copy link
Member

Choose a reason for hiding this comment

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

Ideally we would switch to something in main memory rather than synchronizing on all of these to save time.

Copy link
Member Author

Choose a reason for hiding this comment

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

What exactly do you mean?
I'm not sure if anything related to the way we synchronize here saves any relevant time, this code is only called once when loading all variables.

Copy link
Member

Choose a reason for hiding this comment

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

If that's the only time it's used it's probably fine, though if that's the case does it need to be synchronized at all? When would this be accessed by another process?

Copy link
Member Author

Choose a reason for hiding this comment

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

As far as I can see, it's accessed by 2 threads: the main Bukkit thread, and the logger thread (that reports the status updates, i.e. Loaded X variables so far...)

Copy link
Contributor

@kiip1 kiip1 left a comment

Choose a reason for hiding this comment

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

Very nice PR, just a few questions/suggestions.

@Pikachu920 Pikachu920 added the 2.7 Targeting a 2.7.X version release label Mar 23, 2023
@Pikachu920 Pikachu920 merged commit d1839ac into SkriptLang:master Mar 24, 2023
@TPGamesNL TPGamesNL deleted the enhancement/variables-cleanup branch April 10, 2023 10:30
kiip1 pushed a commit to mlummh/mlum-skript that referenced this pull request Oct 9, 2023
* Reformat variables, add comments and Javadocs
Fix VariablesMap copying not making a deep copy
Fix wrong variable being returned during peak variable changes
Fix variable name comparator returning wrong value in some cases

* Fix tests, my bad

* Some reformatting, added comment on saveTask changes concurrency

* Make SerializedVariable (and inner class) fields final

---------

Co-authored-by: Pikachu920 <28607612+Pikachu920@users.noreply.github.com>

(cherry picked from commit d1839ac)
ALOBugTea pushed a commit to ALOBugTea/Skript that referenced this pull request Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.7 Targeting a 2.7.X version release bug An issue that needs to be fixed. Alternatively, a PR fixing an issue. enhancement Feature request, an issue about something that could be improved, or a PR improving something. variables Related to variables and/or storing them.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants