You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@activemq.apache.org by stephenju <st...@ju-ju.com> on 2012/02/13 16:06:52 UTC

ActiveMQ CPP: Getting text from a Text Message without making temporary

Is there a way to access the text bytes without creating a temporary
std::string? I am dealing with some large text messages and try not to
allocate memory when making copies. For example, when I get the message:

std::string reply = myMessage->getText();

getText() makes a copy of its data on return. And the assignment operator
copies it again into my variable. If I can pass my std::string variable to
the message and have it fill it out for me, or I can get the constant
reference to the string data in the message, it will save considerable time
and memory.

There are also times that I don't need to make a copy of the text. Like when
parsing the text as XML or JSON, the data is no longer needed after the
parsed structure is created.

Thanks.

--
View this message in context: http://activemq.2283324.n4.nabble.com/ActiveMQ-CPP-Getting-text-from-a-Text-Message-without-making-temporary-tp4384039p4384039.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Re: ActiveMQ CPP: Getting text from a Text Message without making temporary

Posted by stephenju <st...@ju-ju.com>.
Cool. That's good to know. I didn't know std::string is reference counted. I
came from some really old implementation of STL and it wasn't like that.

Thank you so much. Now I am not so afraid of copying strings. :)

--
View this message in context: http://activemq.2283324.n4.nabble.com/ActiveMQ-CPP-Getting-text-from-a-Text-Message-without-making-temporary-tp4384039p4385614.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Re: ActiveMQ CPP: Getting text from a Text Message without making temporary

Posted by Timothy Bish <ta...@gmail.com>.
On Mon, 2012-02-13 at 14:26 -0800, stephenju wrote: 
> Just went through the source files and find it actually makes 4 copies of the
> text.
> 
> MarshallingSupport::readString32() makes 1 from the stream.
> ActiveMQTextMessage::getText() then make another one to store in its data
> member. Then it makes yet another one as return value. Finally, the
> assignment in my code makes one for itself.
> 
> Maybe I count them wrong? :)
> 
C++ String object uses a reference counted design so the copies are
lightweight copies internally they all point to the same data until a
mutating operation is performed.  Most modern compilers will also
optimize the return by using RVO and NRVO to eliminate the use of a
temporary.

> --
> View this message in context: http://activemq.2283324.n4.nabble.com/ActiveMQ-CPP-Getting-text-from-a-Text-Message-without-making-temporary-tp4384039p4385547.html
> Sent from the ActiveMQ - User mailing list archive at Nabble.com.

-- 
Tim Bish
Sr Software Engineer | FuseSource Corp
tim.bish@fusesource.com | www.fusesource.com
skype: tabish121 | twitter: @tabish121
blog: http://timbish.blogspot.com/


Re: ActiveMQ CPP: Getting text from a Text Message without making temporary

Posted by stephenju <st...@ju-ju.com>.
Just went through the source files and find it actually makes 4 copies of the
text.

MarshallingSupport::readString32() makes 1 from the stream.
ActiveMQTextMessage::getText() then make another one to store in its data
member. Then it makes yet another one as return value. Finally, the
assignment in my code makes one for itself.

Maybe I count them wrong? :)

--
View this message in context: http://activemq.2283324.n4.nabble.com/ActiveMQ-CPP-Getting-text-from-a-Text-Message-without-making-temporary-tp4384039p4385547.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Re: ActiveMQ CPP: Getting text from a Text Message without making temporary

Posted by stephenju <st...@ju-ju.com>.
Thanks for the warning. Yes, our connections are compressed. So this is a no
go I guess.

--
View this message in context: http://activemq.2283324.n4.nabble.com/ActiveMQ-CPP-Getting-text-from-a-Text-Message-without-making-temporary-tp4384039p4385205.html
Sent from the ActiveMQ - User mailing list archive at Nabble.com.

Re: ActiveMQ CPP: Getting text from a Text Message without making temporary

Posted by Timothy Bish <ta...@gmail.com>.
On Mon, 2012-02-13 at 23:57 +0400, Ivan Pechorin wrote: 
> 2012/2/13 stephenju <st...@ju-ju.com>
> 
> > Is there a way to access the text bytes without creating a temporary
> > std::string? I am dealing with some large text messages and try not to
> > allocate memory when making copies. For example, when I get the message:
> >
> > std::string reply = myMessage->getText();
> >
> > getText() makes a copy of its data on return. And the assignment operator
> > copies it again into my variable. If I can pass my std::string variable to
> > the message and have it fill it out for me, or I can get the constant
> > reference to the string data in the message, it will save considerable time
> > and memory.
> >
> > There are also times that I don't need to make a copy of the text. Like
> > when
> > parsing the text as XML or JSON, the data is no longer needed after the
> > parsed structure is created.
> >
> >
> Technically, you can get text bytes without copying using something like
> this:
> 
> using namespace cms;
> using namespace activemq::commands;
> ...
> const TextMessage* textMsg = ...
> const ActiveMQTextMessage* msg =
> dynamic_cast<ActiveMQTextMessage*>( textMsg );
> assert(msg != NULL);
> 
> const std::vector<unsigned char>& data( msg->getContent() );
> assert(data.size() >= 4);
> 
> unsigned int data_len = (data[0] << 24 | data[1] << 16 | data[2] <<  8 |
> data[3] <<  0);
> const char* data_ptr = (const char*) &(data[4]);
> 
> It's not pretty,

Be careful though because if the message payload is compressed you won't
get a valid string, also the payload will be in modified UTF-8 format so
it could be different then what you'd expect also. 


-- 
Tim Bish
Sr Software Engineer | FuseSource Corp
tim.bish@fusesource.com | www.fusesource.com
skype: tabish121 | twitter: @tabish121
blog: http://timbish.blogspot.com/


Re: ActiveMQ CPP: Getting text from a Text Message without making temporary

Posted by Ivan Pechorin <iv...@gmail.com>.
2012/2/13 stephenju <st...@ju-ju.com>

> Is there a way to access the text bytes without creating a temporary
> std::string? I am dealing with some large text messages and try not to
> allocate memory when making copies. For example, when I get the message:
>
> std::string reply = myMessage->getText();
>
> getText() makes a copy of its data on return. And the assignment operator
> copies it again into my variable. If I can pass my std::string variable to
> the message and have it fill it out for me, or I can get the constant
> reference to the string data in the message, it will save considerable time
> and memory.
>
> There are also times that I don't need to make a copy of the text. Like
> when
> parsing the text as XML or JSON, the data is no longer needed after the
> parsed structure is created.
>
>
Technically, you can get text bytes without copying using something like
this:

using namespace cms;
using namespace activemq::commands;
...
const TextMessage* textMsg = ...
const ActiveMQTextMessage* msg =
dynamic_cast<ActiveMQTextMessage*>( textMsg );
assert(msg != NULL);

const std::vector<unsigned char>& data( msg->getContent() );
assert(data.size() >= 4);

unsigned int data_len = (data[0] << 24 | data[1] << 16 | data[2] <<  8 |
data[3] <<  0);
const char* data_ptr = (const char*) &(data[4]);

It's not pretty,