-
-
Notifications
You must be signed in to change notification settings - Fork 938
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
singleton command arguments #4334
Conversation
@@ -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(); |
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.
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; |
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.
public static CompoundNbtTagArgumentType create() {
return CompoundNbtTagArgumentTypeHolder.INSTANCE;
}
These functions can be problematic when they run as a single instance. |
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 |
Ah, since these are all initialized on startup via command registration, there’s no need to have lazy loading |
This was an init instance. |
Type of change
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: