问:可以像对图形文件那样使用嵌套语义操作吗?这会影响性能吗?
我不会这样做的。只添加边缘批发可能要容易得多:
x3::parse(file_iterator, eof,
*((x3::int_ >> '\t' >> x3::int_ >> x3::eol)[add_edge])
);
哪里add_ege
可以这么简单:
auto add_edge = [&](auto& ctx){
// Add edge from from context
vertex_decriptor source, target;
auto tup = std::tie(source, target);
fusion::copy(x3::_attr(ctx), tup);
boost::add_edge(map_vertex(source), map_vertex(target), g);
};
Q.建议用 Spirit X3 一次解析整个文件还是应该用 Spirit X3 单独解析每一行?
我不认为精神提出任何建议。我会一次完成整个文件。而且我建议使用内存映射文件,这样您可以获得更高的效率(没有multi_pass
迭代器适配的随机访问迭代)。
一般说明:
您正在尝试使用空间感知解析器,但将它们与 istream_iterators 一起使用。然后,您必须记住重置skipws
流上的标志。
vertices
地图似乎是在浪费资源;考虑是否可以直接使用[user]
thing( vertex_id
) 而不是翻译成vertex_descriptor
.
这是一个清理过的版本,可以在大约 19 秒内解析来自https://snap.stanford.edu/data/loc-gowalla.html的文件(这已经相当快了):
Live On Coliru
#include <boost/fusion/adapted/std_tuple.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/spirit/include/support_istream_iterator.hpp>
#include <fstream>
#include <iostream>
namespace x3 = boost::spirit::x3;
namespace fusion = boost::fusion;
struct vertex_property {
double longitude;
double latitude;
};
struct edge_property { };
struct Reader {
bool read_edges(std::string fname) {
// Lambda function that adds edge to graph
auto add_edge = [this](auto& ctx){
// Add edge from from context
vertex_decriptor source, target;
auto tup = std::tie(source, target);
fusion::copy(x3::_attr(ctx), tup);
boost::add_edge(this->map_vertex(source), this->map_vertex(target), g);
};
// Parse the gowalla edge file
std::ifstream edge_file(fname);
if (!edge_file) return false;
boost::spirit::istream_iterator file_iterator(edge_file >> std::noskipws), eof;
x3::parse(file_iterator, eof, *((x3::int_ >> '\t' >> x3::int_ >> x3::eol)[add_edge]));
// Fail if we couldn't parse the whole edges file
return (file_iterator == eof);
}
bool read_locations(std::string fname) {
// Lambda function that adds locations to vertices in the graph
auto add_location = [&](auto& ctx){
// _attr(ctx) returns a boost fusion tuple
auto attr = x3::_attr(ctx);
auto vertex_id = fusion::at_c<0>(attr);
if (!location_already_added.insert(vertex_id).second)
return true; // Exit, as we already stored the location for this vertex
// Test if vertex is in our graph
// We are parsing locations from a different file than the graph, so
// there might be inconsistencies
auto mapped = mapped_vertices.find(vertex_id);
if (mapped == mapped_vertices.end()) {
std::cerr << "Tried to add location to vertex " << vertex_id << ", but this vertex is not in our graph" << std::endl;
return false;
}
// Add location to the vertex
auto& props = g[mapped->second];
props.latitude = fusion::at_c<1>(attr);
props.longitude = fusion::at_c<2>(attr);
return true;
};
// Parse the gowalla location file
std::ifstream location_file(fname);
if (!location_file) return false;
boost::spirit::istream_iterator file_iterator(location_file >> std::noskipws), eof;
x3::parse(file_iterator, eof,
// [vertex_id] [time of checkin] [latitude] [longitude] [location] id
*((x3::int_ >> '\t' >> x3::omit[*x3::graph] >> '\t' >> x3::double_ >> '\t' >> x3::double_)[add_location] >> '\t' >> x3::int_ >> x3::eol)
);
// Fail if we couldn't parse the whole location file
return (file_iterator == eof);
}
private:
// We use setS to enforce our graph not to become a multigraph
typedef boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS, vertex_property, edge_property> graph;
using vertex_decriptor = graph::vertex_descriptor;
std::map<int, vertex_decriptor> mapped_vertices;
std::set<int> location_already_added;
graph g;
// Lambda function that adds vertex to graph if not already added
vertex_decriptor map_vertex(int id) {
auto match = mapped_vertices.find(id);
if (match != mapped_vertices.end())
return match->second; // vertex already known
else // Otherwise add vertex
return mapped_vertices[id] = boost::add_vertex(g);
};
};
int main() {
Reader reader;
if (!reader.read_edges("loc-gowalla_edges.txt"))
std::cerr << "Couldn't parse whole edges file" << std::endl;
if (!reader.read_locations("loc-gowalla_totalCheckins.txt"))
std::cerr << "Couldn't parse whole location file" << std::endl;
}
映射文件
相比之下,用内存映射文件替换会更快:它在 3 秒内完成(再次快 6 倍以上):
Live On Coliru
示例更改片段:
boost::iostreams::mapped_file_source mm(fname);
auto f = mm.begin(), l = mm.end();
x3::parse(f, l, *((x3::int_ >> '\t' >> x3::int_ >> x3::eol)[add_edge]));
内存开销
剖析后。看起来拥有地图/设置可能还不错:

据我所知,该程序使用了 152MiB,其中只有 4.1location_already_added
乍一看。
减少内存使用和时间
即便如此,用set<int> location_already_added
动态位集替换 并删除map<int, vertex_descriptor>
确实会进一步减少内存使用和程序运行时间。
这次它在2 秒内完成(另外 33% 的折扣)。
由于显而易见的原因,它占用的内存大约减少了 10% :138.7 MiB。
Live On Coliru
变化:
#include <boost/fusion/adapted/std_tuple.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/spirit/home/x3.hpp>
#include <boost/iostreams/device/mapped_file.hpp>
#include <boost/dynamic_bitset.hpp>
#include <fstream>
#include <iostream>
namespace x3 = boost::spirit::x3;
namespace fusion = boost::fusion;
struct vertex_property {
double longitude;
double latitude;
};
struct edge_property { };
struct Reader {
Reader() {
g.m_vertices.reserve(1024);
}
bool read_edges(std::string fname) {
// Lambda function that adds edge to graph
auto add_edge = [this](auto& ctx){
// Add edge from from context
vertex_decriptor source, target;
auto tup = std::tie(source, target);
fusion::copy(x3::_attr(ctx), tup);
boost::add_edge(this->map_vertex(source), this->map_vertex(target), g);
};
// Parse the gowalla edge file
boost::iostreams::mapped_file_source mm(fname);
auto f = mm.begin(), l = mm.end();
x3::parse(f, l, *((x3::int_ >> '\t' >> x3::int_ >> x3::eol)[add_edge]));
// Fail if we couldn't parse the whole edges file
return f == l;
}
bool read_locations(std::string fname) {
boost::dynamic_bitset<> location_already_added(num_vertices(g));
// Lambda function that adds locations to vertices in the graph
auto add_location = [&](auto& ctx){
// _attr(ctx) returns a boost fusion tuple
auto const& attr = x3::_attr(ctx);
auto vertex_id = fusion::at_c<0>(attr);
if (location_already_added.test(vertex_id))
return true; // Exit, as we already stored the location for this vertex
location_already_added.set(vertex_id);
// Test if vertex is in our graph
// We are parsing locations from a different file than the graph, so
// there might be inconsistencies
auto mapped = this->mapped_vertex(vertex_id);
if (graph::null_vertex() == mapped) {
std::cerr << "Tried to add location to vertex " << vertex_id << ", but this vertex is not in our graph" << std::endl;
return false;
}
// Add location to the vertex
auto& props = g[mapped];
props.latitude = fusion::at_c<1>(attr);
props.longitude = fusion::at_c<2>(attr);
return true;
};
// Parse the gowalla location file
std::ifstream location_file(fname);
if (!location_file) return false;
boost::iostreams::mapped_file_source mm(fname);
auto f = mm.begin(), l = mm.end();
x3::parse(f, l,
// [vertex_id] [time of checkin] [latitude] [longitude] [location] id
*((x3::int_ >> '\t' >> x3::omit[*x3::graph] >> '\t' >> x3::double_ >> '\t' >> x3::double_)[add_location] >> '\t' >> x3::int_ >> x3::eol)
);
// Fail if we couldn't parse the whole location file
return f == l;
}
typedef boost::adjacency_list<boost::setS, boost::vecS, boost::undirectedS, vertex_property, edge_property> graph;
private:
// We use setS to enforce our graph not to become a multigraph
using vertex_decriptor = graph::vertex_descriptor;
graph g;
#if USE_VERTEX_DESCRIPTOR_MAPPING
std::map<int, vertex_decriptor> mapped_vertices;
vertex_decriptor map_vertex(int id) {
auto match = mapped_vertices.find(id);
if (match != mapped_vertices.end())
return match->second; // vertex already known
else // Otherwise add vertex
return mapped_vertices[id] = boost::add_vertex(g);
};
vertex_decriptor mapped_vertex(int id) const {
auto mapped = mapped_vertices.find(id);
return mapped == mapped_vertices.end()
? return graph::null_vertex()
: mapped->second;
}
#else
static vertex_decriptor map_vertex(int id) { return id; }
static vertex_decriptor mapped_vertex(int id) { return id; }
#endif
};
int main() {
Reader reader;
if (!reader.read_edges("loc-gowalla_edges.txt"))
std::cerr << "Couldn't parse whole edges file" << std::endl;
if (!reader.read_locations("loc-gowalla_totalCheckins.txt"))
std::cerr << "Couldn't parse whole location file" << std::endl;
}