fix(iot-dev): Fix inefficient way of building strings in https batch messages#1335
fix(iot-dev): Fix inefficient way of building strings in https batch messages#1335timtay-microsoft merged 4 commits intomasterfrom
Conversation
…messages StringBuilder is much more efficient than just concatenating strings directly
|
/azp run |
|
Azure Pipelines successfully started running 4 pipeline(s). |
|
|
||
| if (atLeastOneMessage) | ||
| { | ||
| batchBodyBuilder.deleteCharAt(batchBodyBuilder.length() - 1); // remove the final comma from the list |
There was a problem hiding this comment.
Another approach to consider, that doesn't requiring removing a character, but still has the overhead of one boolean checked on each iteration in the loop above (but only requires setting once), is to conditionally add the comma only after the first time.
So something like:
boolean isSubsequentMessage = false;
for (HttpsSingleMessage message : messageList)
{
if (isSubsequentMessage) {
batchBodyBuilder.append(','); // comma to separate each object in the json array
} else {
isSubsequentMessage = true;
}
addJsonToStringBuilder(message, batchBodyBuilder);
this.numMsgs++;
}There was a problem hiding this comment.
Another thought to consider is String.join.
In this instance you'd move the JSON string creation to a method in the HttpsSingleMessage class, create the List<String> in sendMessage and create a List<String> constructor to be used.
A pro of this is we are using something built into the SDK and it makes it more readable; plus less lines of code. As well you're creating a method to be reused anytime you might want to spit out the JSON of a HttpsSingleMessage.
Some cons are you're moving the string creation up a level into sendMessage which may not want and you'd be adding a new method to the HttpsSingleMessage class and it may make the batch message creation too disjointed.
|
/azp run |
|
Azure Pipelines successfully started running 4 pipeline(s). |
| { | ||
| jsonMsg.append("\"").append(key).append("\":"); | ||
| jsonMsg.append("\"").append(allProperties.get(key)).append("\","); | ||
| jsonMsg.append("\"").append(allProperties.get(key)).append("\","); //TODO |
There was a problem hiding this comment.
I'll send a PR out for this later. I've removed this todo for now
|
/azp run |
|
Azure Pipelines successfully started running 4 pipeline(s). |
StringBuilder is much more efficient than just concatenating strings directly