简体   繁体   English

Boost Spirit:慢解析优化

[英]Boost Spirit: slow parsing optimization

I'm new to Spirit and to Boost in general.我是 Spirit 和 Boost 的新手。 I'm trying to parse a section of a VRML file that looks like this:我正在尝试解析 VRML 文件的一部分,如下所示:

point
            [
            #coordinates written in meters.
            -3.425386e-001 -1.681608e-001 0.000000e+000,
            -3.425386e-001 -1.642545e-001 0.000000e+000,
            -3.425386e-001 -1.603483e-001 0.000000e+000,

The comment that starts with # is optional.#开头的注释是可选的。

I've written a grammar, that works fine, but the parsing process is taking to long.我写了一个语法,效果很好,但解析过程需要很长时间。 I would like to optimize it to run much faster.我想优化它以运行得更快。 My code looks like this:我的代码如下所示:

struct Point
{
    double a;
    double b;
    double c;

    Point() : a(0.0), b(0.0), c(0.0){}
};

BOOST_FUSION_ADAPT_STRUCT
(
    Point,
    (double, a) 
    (double, b)
    (double, c)
)

namespace qi   = boost::spirit::qi;
namespace repo = boost::spirit::repository;

template <typename Iterator>
struct PointParser : 
public qi::grammar<Iterator, std::vector<Point>(), qi::space_type>
{
   PointParser() : PointParser::base_type(start, "PointGrammar")
   {
      singlePoint = qi::double_>>qi::double_>>qi::double_>>*qi::lit(",");
      comment = qi::lit("#")>>*(qi::char_("a-zA-Z.") - qi::eol);
      prefix = repo::seek[qi::lexeme[qi::skip[qi::lit("point")>>qi::lit("[")>>*comment]]];
      start %= prefix>>qi::repeat[singlePoint];     

      //BOOST_SPIRIT_DEBUG_NODES((prefix)(comment)(singlePoint)(start));
   }

   qi::rule<Iterator, Point(), qi::space_type>              singlePoint;
   qi::rule<Iterator, qi::space_type>                       comment;
   qi::rule<Iterator, qi::space_type>                       prefix;
   qi::rule<Iterator, std::vector<Point>(), qi::space_type> start;
};

The section that I intend to parse, is located at the middle of the input text, so I need to skip the portion of the text in order to get to it.我打算解析的部分位于输入文本的中间,因此我需要跳过文本部分才能找到它。 I implemented it using repo::seek .我使用repo::seek实现了它。 Is this the best way?这是最好的方法吗?

I run the parser in the following way:我按以下方式运行解析器:

std::vector<Point> points;
typedef PointParser<std::string::const_iterator> pointParser;   
pointParser g2; 

auto start = ch::high_resolution_clock::now();  
bool r = phrase_parse(Data.begin(), Data.end(), g2, qi::space, points); 
auto end = ch::high_resolution_clock::now();

auto duration = ch::duration_cast<boost::chrono::milliseconds>(end - start).count();

To parse about 80k entries in the input text, takes about 2.5 seconds, which is pretty slow for my needs.要解析输入文本中的大约 80k 个条目,大约需要 2.5 秒,这对于我的需要来说非常慢。 My question is there a way to write a parsing rules in more optimized way to make it (much) faster?我的问题是有没有办法以更优化的方式编写解析规则以使其(更快)速度? How can I improve this implementation in general?一般来说,我该如何改进这个实现?

I'm new to Spirit, so some explanation will be much appreciated.我是 Spirit 的新手,因此非常感谢您的一些解释。

I've hooked your grammar into a Nonius benchmark and generated uniformly random input data of ~85k lines (download: http://stackoverflow-sehe.s3.amazonaws.com/input.txt , 7.4 MB).我已将您的语法连接到Nonius基准测试中,并生成了约 85k 行的均匀随机输入数据(下载: http ://stackoverflow-sehe.s3.amazonaws.com/input.txt,7.4 MB)。

  • are you measuring time in a release build?您是否正在测量发布版本中的时间?
  • are you using slow file input?你在使用慢速文件输入吗?

When reading the file up-front I consistently get a time of ~36ms to parse the whole bunch.预先读取文件时,我始终有大约 36 毫秒的时间来解析整个文件。

clock resolution: mean is 17.616 ns (40960002 iterations)

benchmarking sample
collecting 100 samples, 1 iterations each, in estimated 3.82932 s
mean: 36.0971 ms, lb 35.9127 ms, ub 36.4456 ms, ci 0.95
std dev: 1252.71 μs, lb 762.716 μs, ub 2.003 ms, ci 0.95
found 6 outliers among 100 samples (6%)
variance is moderately inflated by outliers

Code: see below.代码:见下文。


Notes:笔记:

  • you seem conflicted on the use of skippers and seek together.你似乎对使用船长和一起寻找有冲突。 I'd suggest you simplify prefix :我建议你简化prefix

     comment = '#' >> *(qi::char_ - qi::eol); prefix = repo::seek[ qi::lit("point") >> '[' >> *comment ];

    prefix will use the space skipper, and ignore any matched attributes (because of the rule declared type). prefix将使用空格跳过,并忽略任何匹配的属性(因为规则声明类型)。 Make comment implicitly a lexeme by dropping the skipper from the rule declaration:通过从规则声明中删除船长,使comment隐式成为一个词素

     // implicit lexeme: qi::rule<Iterator> comment;

    Note See Boost spirit skipper issues for more background information.注意有关更多背景信息,参阅Boost 精神船长问题

Live On Coliru 在 Coliru

#include <boost/fusion/adapted/struct.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/repository/include/qi_seek.hpp>

namespace qi   = boost::spirit::qi;
namespace repo = boost::spirit::repository;

struct Point { double a = 0, b = 0, c = 0; };

BOOST_FUSION_ADAPT_STRUCT(Point, a, b, c)

template <typename Iterator>
struct PointParser : public qi::grammar<Iterator, std::vector<Point>(), qi::space_type>
{
    PointParser() : PointParser::base_type(start, "PointGrammar")
    {
        singlePoint = qi::double_ >> qi::double_ >> qi::double_ >> *qi::lit(',');

        comment     = '#' >> *(qi::char_ - qi::eol);

        prefix      = repo::seek[
            qi::lit("point") >> '[' >> *comment
            ];

        //prefix      = repo::seek[qi::lexeme[qi::skip[qi::lit("point")>>qi::lit("[")>>*comment]]];

        start      %= prefix >> *singlePoint;

        //BOOST_SPIRIT_DEBUG_NODES((prefix)(comment)(singlePoint)(start));
    }

  private:
    qi::rule<Iterator, Point(), qi::space_type>              singlePoint;
    qi::rule<Iterator, std::vector<Point>(), qi::space_type> start;
    qi::rule<Iterator, qi::space_type>                       prefix;
    // implicit lexeme:
    qi::rule<Iterator>  comment;
};

#include <nonius/benchmark.h++>
#include <nonius/main.h++>
#include <boost/iostreams/device/mapped_file.hpp>

static boost::iostreams::mapped_file_source src("input.txt");

NONIUS_BENCHMARK("sample", [](nonius::chronometer cm) {
    std::vector<Point> points;

    using It = char const*;
    PointParser<It> g2;

    cm.measure([&](int) {
        It f = src.begin(), l = src.end();
        return phrase_parse(f, l, g2, qi::space, points);
        bool ok =  phrase_parse(f, l, g2, qi::space, points);
        if (ok)
            std::cout << "Parsed " << points.size() << " points\n";
        else
            std::cout << "Parsed failed\n";

        if (f!=l)
            std::cout << "Remaining unparsed input: '" << std::string(f,std::min(f+30, l)) << "'\n";

        assert(ok);
    });
})

Graph:图形:

在此处输入图片说明

Another run output, live:另一个运行输出,实时:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM