增强精神气的慢速

Boost Spirit QI slow

本文关键字:增强      更新时间:2023-10-16

我尝试用Boost Spirit Qi解析TPCH文件。我的实现灵感来自Spirit Qi的员工示例(http://www.boost.org/doc/libs/1_52_0/libs/spirit/spirit/example/qi/employeee.cpp)。数据的格式为CSV格式,代币通过'|'界定。字符。

它有效,但非常慢(1 GB的20秒)。

这是我的Qi Grammer用于lineItem文件:

struct lineitem {
    int l_orderkey;
    int l_partkey;
    int l_suppkey;
    int l_linenumber;
    std::string l_quantity;
    std::string l_extendedprice;
    std::string l_discount;
    std::string l_tax;
    std::string l_returnflag;
    std::string l_linestatus;
    std::string l_shipdate;
    std::string l_commitdate;
    std::string l_recepitdate;
    std::string l_shipinstruct;
    std::string l_shipmode;
    std::string l_comment;
};
BOOST_FUSION_ADAPT_STRUCT( lineitem,
    (int, l_orderkey)
    (int, l_partkey)
    (int, l_suppkey)
    (int, l_linenumber)
    (std::string, l_quantity)
    (std::string, l_extendedprice)
    (std::string, l_discount)
    (std::string, l_tax)
    (std::string, l_returnflag)
    (std::string, l_linestatus)
    (std::string, l_shipdate)
    (std::string, l_commitdate)
    (std::string, l_recepitdate)
    (std::string, l_shipinstruct)
    (std::string, l_shipmode)
    (std::string, l_comment)) 
vector<lineitem>* lineitems=new vector<lineitem>();
phrase_parse(state->dataPointer,
    state->dataEndPointer,
    (*(int_ >> "|" >>
    int_ >> "|" >> 
    int_ >> "|" >>
    int_ >> "|" >>
    +(char_ - '|') >> "|" >>
    +(char_ - '|') >> "|" >>
    +(char_ - '|') >> "|" >>
    +(char_ - '|') >> "|" >>
    +(char_ - '|') >> '|' >>
    +(char_ - '|') >> '|' >>
    +(char_ - '|') >> '|' >>
    +(char_ - '|') >> '|' >>
    +(char_ - '|') >> '|' >>
    +(char_ - '|') >> '|' >>
    +(char_ - '|') >> '|' >>
    +(char_ - '|') >> '|' 
    ) ), space, *lineitems
);

问题似乎是角色解析。它比其他转换要慢得多。有没有更好的方法将可变长度代币分解为字符串?

我找到了解决问题的解决方案。正如这篇文章所述性能瓶颈是Spirit Qi的字符串处理。所有其他数据类型似乎都非常快。

我通过自己处理数据而不是使用Spirit Qi处理来避免此问题。

我的解决方案使用了一个辅助类,该类为CSV文件的每个字段提供功能。该功能将值存储到结构中。字符串存储在char [] s中。击中解析器一个newline字符,它调用一个函数,该函数将结构添加到结果向量。Boost解析器称此功能而不是单独将值存储到向量中。

这是我的region.tbl文件的代码:

struct region{
    int r_regionkey;
    char r_name[25];
    char r_comment[152];
};
class regionStorage{
public:
regionStorage(vector<region>* regions) :regions(regions), pos(0) {}
void storer_regionkey(int const&i){
    currentregion.r_regionkey = i;
}
void storer_name(char const&i){
    currentregion.r_name[pos] = i;
    pos++;
}
void storer_comment(char const&i){
    currentregion.r_comment[pos] = i;
    pos++;
}
void resetPos() {
    pos = 0;
}
void endOfLine() {
    pos = 0;
    regions->push_back(currentregion);
}
private:
vector<region>* regions;
region currentregion;
int pos;
};

void parseRegion(){
    vector<region> regions;
    regionStorage regionstorageObject(&regions);
    phrase_parse(dataPointer, /*< start iterator >*/    
     state->dataEndPointer, /*< end iterator >*/
     (*(lexeme[
     +(int_[boost::bind(&regionStorage::storer_regionkey, &regionstorageObject, _1)] - '|') >> '|' >>
     +(char_[boost::bind(&regionStorage::storer_name, &regionstorageObject, _1)] - '|') >> char_('|')[boost::bind(&regionStorage::resetPos, &regionstorageObject)] >>
     +(char_[boost::bind(&regionStorage::storer_comment, &regionstorageObject, _1)] - '|') >> char_('|')[boost::bind(&regionStorage::endOfLine, &regionstorageObject)]
    ])), space);
   cout << regions.size() << endl;
}

这不是一个漂亮的解决方案,但它可以工作,而且更快。(1 GB TCPH数据,多线程)

2.2秒

问题主要来自将单个char元素附加到std::string容器中。根据您的语法,对于每个std::string属性,分配始于当炭时启动并在找到|分离器时停止。因此,起初有sizeof(char)+1保留字节(null端接的" 0")。编译器将必须根据分配器加倍算法运行std::string的分配器!这意味着必须非常频繁地重新分配记忆,以便小字符串重新分配。这意味着您的字符串被复制到内存分配双重分配,并以1,2,4,6,6,12,24 ...字符的间隔释放了先前的分配。难怪它很慢,这会导致频繁的Malloc呼叫引起巨大问题;更多的碎片,更大的自由存储器块的链接列表,这些内存块的变量(小)尺寸,在此依次会导致对应用程序分配的内存更长扫描在整个一生中的分配。tldr;数据变成碎片并广泛分散在内存中。

证明?每次在迭代器中满足有效字符时,char_parser调用以下代码。从Boost 1.54

/boost/spirit/home/qi/char/char/char_parser.hpp

if (first != last && this->derived().test(*first, context))
{
    spirit::traits::assign_to(*first, attr_);
    ++first;
    return true;
}
return false;

/boost/spirit/home/qi/detail/assign_to.hpp

// T is not a container and not a string
template <typename T_>
static void call(T_ const& val, Attribute& attr, mpl::false_, mpl::false_)
{
    traits::push_back(attr, val);
}

/boost/spirit/home/support/container.hpp

template <typename Container, typename T, typename Enable/* = void*/>
struct push_back_container
{
    static bool call(Container& c, T const& val)
    {
        c.insert(c.end(), val);
        return true;
    }
};

您发布的校正后续代码(将结构更改为char Name[Size])基本上与添加字符串Name.reserve(Size)语句指令相同。但是,目前尚无指示。

解决方案:

/boost/spirit/home/support/container.hpp

template <typename Container, typename T, typename Enable/* = void*/>
struct push_back_container
{
    static bool call(Container& c, T const& val, size_t initial_size = 8)
    {
        if (c.capacity() < initial_size)
            c.reserve(initial_size);
        c.insert(c.end(), val);
        return true;
    }
};

/boost/spirit/home/qi/char/char/char_parser.hpp

if (first != last && this->derived().test(*first, context))
{
    spirit::traits::assign_to(*first, attr_);
    ++first;
    return true;
}
if (traits::is_container<Attribute>::value == true)
    attr_.shrink_to_fit();
return false;

我尚未测试它,但我认为它可以像您看到的那样超过10倍的字符串属性加快char解析器。这将是提升精神更新中的一个很好的优化功能,包括设置初始缓冲区大小的reserve(initial_size)[ +( char_ - lit("|") ) ]指令。

编译时是否使用-O2?

Boosts库有很多冗余,使用优化标志时会删除。

另一个可能的解决方案是使用重复解析器指令:http://www.boost.org/doc/libs/1_52_0/libs/spirit/doc/html/spirit/qi/reference/reference/reperive/directive/repeat.html