mojira.dev
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;
        }
    }
}
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;
        }
    }
}

Comments 0

No comments.

intsuc

(Unassigned)

Plausible

Platform

Low

Commands, Performance

nbt

1.16.1, 1.16.2 Pre-release 1, 1.16.2 Pre-release 2, 1.16.2 Release Candidate 1, 1.16.2 Release Candidate 2, ..., 24w33a, 1.21.4, 25w04a, 1.21.5, 25w16a

Retrieved