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

Avoid copying of MqttPublishMessage.payload byte buffer #13939

Open
wants to merge 2 commits into
base: 4.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ private static ByteBuf encodePublishMessage(
MqttVersion mqttVersion = getMqttVersion(ctx);
MqttFixedHeader mqttFixedHeader = message.fixedHeader();
MqttPublishVariableHeader variableHeader = message.variableHeader();
ByteBuf payload = message.payload().duplicate();
ByteBuf payload = message.payload();

String topicName = variableHeader.topicName();
int topicNameBytes = utf8Bytes(topicName);
Expand Down Expand Up @@ -453,6 +453,7 @@ private static ByteBuf encodePublishMessage(
return buf;
} finally {
propertiesBuf.release();
payload.release();
Copy link
Member

Choose a reason for hiding this comment

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

I will need to check in details but if this is needed here it was also needed in the original code

Copy link
Author

Choose a reason for hiding this comment

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

Yeah, there is a back comparability issue. Maybe you have some ideas to avoid it?

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public MqttPublishMessage build() {
MqttFixedHeader mqttFixedHeader = new MqttFixedHeader(MqttMessageType.PUBLISH, false, qos, retained, 0);
MqttPublishVariableHeader mqttVariableHeader =
new MqttPublishVariableHeader(topic, messageId, mqttProperties);
return new MqttPublishMessage(mqttFixedHeader, mqttVariableHeader, Unpooled.buffer().writeBytes(payload));
return new MqttPublishMessage(mqttFixedHeader, mqttVariableHeader, payload);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ public void testConnAckMessage() throws Exception {
@Test
public void testPublishMessage() throws Exception {
final MqttPublishMessage message = createPublishMessage();
ByteBuf payload = message.payload().copy();

ByteBuf byteBuf = MqttEncoder.doEncode(ctx, message);

mqttDecoder.channelRead(ctx, byteBuf);
Expand All @@ -285,7 +287,7 @@ public void testPublishMessage() throws Exception {
final MqttPublishMessage decodedMessage = (MqttPublishMessage) out.get(0);
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validatePublishVariableHeader(message.variableHeader(), decodedMessage.variableHeader());
validatePublishPayload(message.payload(), decodedMessage.payload());
validatePublishPayload(payload, decodedMessage.payload());
}

@Test
Expand Down Expand Up @@ -600,6 +602,8 @@ public void testPublishMessageForMqtt5() throws Exception {
assertEquals(3,
((MqttProperties.UserProperties) props.getProperty(USER_PROPERTY.value())).value.size());
final MqttPublishMessage message = createPublishMessage(props);
ByteBuf payload = message.payload().copy();

ByteBuf byteBuf = MqttEncoder.doEncode(ctx, message);

mqttDecoder.channelRead(ctx, byteBuf);
Expand All @@ -609,7 +613,7 @@ public void testPublishMessageForMqtt5() throws Exception {
final MqttPublishMessage decodedMessage = (MqttPublishMessage) out.get(0);
validateFixedHeaders(message.fixedHeader(), decodedMessage.fixedHeader());
validatePublishVariableHeader(message.variableHeader(), decodedMessage.variableHeader());
validatePublishPayload(message.payload(), decodedMessage.payload());
validatePublishPayload(payload, decodedMessage.payload());
}

@Test
Expand Down