Mojira Archive
MC-196890

"/data merge" deep-copies target compound tag

The bug

/data merge ... and /data modify ... merge ... deep-copy the target compound tag only to check the failure of merging. This causes a significant performance penalty compared to other NBT-modifying commands that count successes incrementally.

Code analysis

net.minecraft.server.commands.data.DataCommands.java
private static int mergeData(CommandSourceStack stack, DataAccessor accessor, CompoundTag source) throws CommandSyntaxException {
    CompoundTag data = accessor.getData();
    if (NbtPathArgument.NbtPath.isTooDeep(source, 0)) {
        throw NbtPathArgument.ERROR_DATA_TOO_DEEP.create();
    } else {
        CompoundTag merged = data.copy().merge(source); // The entire data is deep-copied here.
        if (data.equals(merged)) {
            throw ERROR_MERGE_UNCHANGED.create();
        } else {
            accessor.setData(merged);
            stack.sendSuccess(accessor.getModifiedSuccess(), true);
            return 1;
        }
    }
}