Boost logo

Boost Users :

Subject: Re: [Boost-users] [boost-spirit qi] internal copies
From: Hartmut Kaiser (hartmut.kaiser_at_[hidden])
Date: 2010-10-07 09:29:11


> I think this is more the question to spirit devlopers. I would like to
> know how spirit internally maintains the parsed data? Here is an example
> of what I mean.
> I have to parse some byte sequence represented as hex pairs:
>
> 00 ce ab 00 de ad be ef ...
>
> If I use the parser like this:
>
> boost::spirit::qi::uint_parser<unsigned char, 16, 2,2> hex_byte;
>
> std::string str("...");
> std::vector<byte_t> vec;
>
> iter_type curr=str.begin(), end=str.end();
>
> bool r=phrase_parse(curr, end, +hex_byte, space, vec);
>
> Is vec going to be copied somewhere inside of qi? Or are the bytes
> directly appended to the vec? I would like to reserver some space for the
> maximum possible input and avoid additional internal copies inside of
> qi...
>
> How does the behaviour changes std::vector<byte_t>  is part of the
> fusion::map like
>
> fusion::map
> < fusion::pair<tag1, string>
> , fusion::pair<tag2, string>
> ...
> , fusion::pair<tagN, std::vector<byte_t> >
> > data_item;
>
> Are the fields of the map used directly while parsing, or are there some
> temporaries, which will be first created (somewhere inside qi core) and
> filled with data and afterwards assigned to the corresponding map field?

The general idea is for all attributes to be directly used to receive the
parsed data.

Period.

As you might imagine this is not always true. You have to be aware of a
couple of pitfalls in order to avoid additional copies inside the parser
generated from a Spirit.Qi expression.

1) Don't use semantic actions, these need to instantiate an instance of the
attribute in order to pass it to the function (object) to be called.
2) Alternatives currently are known to create a new instance of the
attribute as well. This is a known issue and we need to come up with a
better solution.
3) Somebody posted examples on the Spirit ML a while ago, demonstrating that
certain constructs cause additional copying, but I'm not able to find that,
currently. All I remember is that it seemed to be possible to overcome those
problems as well. We just have not have the time to look into this more
closely yet.

HTH
Regards Hartmut
---------------
http://boost-spirit.com


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net