开发者

Efficiency problem of writing large messages with different header to multiple receivers

开发者 https://www.devze.com 2023-03-10 05:49 出处:网络
开发者_Go百科There is a string (message body) and 3 different headers to be sent to 3 users using java nio socket.

开发者_Go百科There is a string (message body) and 3 different headers to be sent to 3 users using java nio socket.

A way is to create a large byte buffer and put the message body in some postion and put header in front of message boday.

In this way, I still need to one copy for message body and rewrite headers. In my project, the message body is around 14 K bytes. If the memory page is 2K bytes, it is not efficient for memory efficiency management.

My question: is there any way to avoid copying large message string to the byte buffer? I guess C can support it using pointers. Is it true?

Thanks.


I wouldn't create the String, but create the ByteBuffer with the text you would place in the String.

Note: String is not mutable so it will be a copy of some other source eg a StringBuilder. Using a ByteBuffer instead will save you two copies.

You can place the message body in the ByteBuffer with enough padding at the start to put in the header later. This way the message body won't need to copied again.


This is a job for gathering writes: the write(ByteBuffer[], ...) method.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号