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

singleton command arguments #4334

Merged

Conversation

RacoonDog
Copy link
Contributor

Type of change

  • Bug fix
  • New feature

Description

Since the command argument types are stateless, we can re-use singleton instances to lower memory usage a tiny bit.
(760 bytes to 376 bytes, profiled with regular meteor)

Checklist:

  • My code follows the style guidelines of this project.
  • I have added comments to my code in more complex areas.
  • I have tested the code in both development and production environments.

@@ -18,16 +18,19 @@
import static net.minecraft.nbt.StringNbtReader.EXPECTED_VALUE;

public class CompoundNbtTagArgumentType implements ArgumentType<NbtCompound> {
private static final CompoundNbtTagArgumentType INSTANCE = new CompoundNbtTagArgumentType();
Copy link

@JAXPLE JAXPLE Jan 31, 2024

Choose a reason for hiding this comment

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

private static class CompoundNbtTagArgumentTypeHolder {
    private static final CompoundNbtTagArgumentType INSTANCE = new CompoundNbtTagArgumentType();
}

private static final Collection<String> EXAMPLES = Arrays.asList("{foo:bar}", "{foo:[aa, bb],bar:15}");

public static CompoundNbtTagArgumentType create() {
return new CompoundNbtTagArgumentType();
return INSTANCE;
Copy link

Choose a reason for hiding this comment

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

public static CompoundNbtTagArgumentType create() {
    return CompoundNbtTagArgumentTypeHolder.INSTANCE;
}

@JAXPLE
Copy link

JAXPLE commented Jan 31, 2024

These functions can be problematic when they run as a single instance.
Single tone should always be considered before using, and if you use it, I recommend the pattern I reviewed

@RacoonDog
Copy link
Contributor Author

These functions can be problematic when they run as a single instance.
Single tone should always be considered before using, and if you use it, I recommend the pattern I reviewed

in this case, using singletons is safe because the object is stateless and immutable. i dont understand the difference between my proposed changes and your review suggestion though?

@JAXPLE
Copy link

JAXPLE commented Feb 5, 2024

These functions can be problematic when they run as a single instance.
Single tone should always be considered before using, and if you use it, I recommend the pattern I reviewed

in this case, using singletons is safe because the object is stateless and immutable. i dont understand the difference between my proposed changes and your review suggestion though?

My code and your code do the same thing
The difference is that my code will create instances more lazily and run reliably
Considering the side effects, there's no problem with the current code

@Wide-Cat Wide-Cat merged commit e5fc8e6 into MeteorDevelopment:master Feb 5, 2024
1 check passed
@RacoonDog
Copy link
Contributor Author

These functions can be problematic when they run as a single instance.
Single tone should always be considered before using, and if you use it, I recommend the pattern I reviewed

in this case, using singletons is safe because the object is stateless and immutable. i dont understand the difference between my proposed changes and your review suggestion though?

My code and your code do the same thing The difference is that my code will create instances more lazily and run reliably Considering the side effects, there's no problem with the current code

Ah, since these are all initialized on startup via command registration, there’s no need to have lazy loading

@JAXPLE
Copy link

JAXPLE commented Feb 6, 2024

These functions can be problematic when they run as a single instance.
Single tone should always be considered before using, and if you use it, I recommend the pattern I reviewed

in this case, using singletons is safe because the object is stateless and immutable. i dont understand the difference between my proposed changes and your review suggestion though?

My code and your code do the same thing The difference is that my code will create instances more lazily and run reliably Considering the side effects, there's no problem with the current code

Ah, since these are all initialized on startup via command registration, there’s no need to have lazy loading

This was an init instance.
Then it doesn't matter lol

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