Skip to content

adding a method writePayload(String src) #667

Closed
nathan-sigsci wants to merge 2 commits intomsgpack:mainfrom
nathan-sigsci:write-payload-string
Closed

adding a method writePayload(String src) #667
nathan-sigsci wants to merge 2 commits intomsgpack:mainfrom
nathan-sigsci:write-payload-string

Conversation

@nathan-sigsci
Copy link

This new method copies the lower 8-bits of the characters. A separate writePayload(String src) method would be more performant because we're able to directly copy the bytes from the string to the right location in MessageBuffer. Introducing encoding in an attempt to re-use the existing writePayload(byte[] src) creates unnecessary overhead which in turn will affect performance.


public MessagePacker writePayload(String src) throws IOException
{
int len = src.length();
Copy link
Member

Choose a reason for hiding this comment

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

If the input string contains non-ascii characters, src.getBytes() will produce a longer array than src.length(), so using this method can easily create corrupted data.

if (buffer == null || buffer.size() - position < len || len > bufferFlushThreshold) {
flush(); // call flush before write
// Directly write payload to the output without using the buffer
out.write(src.getBytes(), 0, len);
Copy link
Member

Choose a reason for hiding this comment

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

If calling src.getBytes() is allowed here, you should be able to use addPayload(byte[]) instead.

@xerial
Copy link
Member

xerial commented Sep 24, 2023

Hi @nathan-sigsci,

Thank you for creating a PR, but the intended optimization is already available as .addPayload method. You can use these methods instead.

I'll close the PR.

@xerial xerial closed this Sep 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants