Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does locking slow down this sequential file parser?
    primarykey
    data
    text
    <p>I wrote a simple reader and parser for a graph file format. The problem is that it is incredibly slow. Here are the relevant methods:</p> <pre><code>Graph METISGraphReader::read(std::string path) { METISParser parser(path); std::pair&lt;int64_t, int64_t&gt; header = parser.getHeader(); int64_t n = header.first; int64_t m = header.second; Graph G(n); node u = 0; while (parser.hasNext()) { u += 1; std::vector&lt;node&gt; adjacencies = parser.getNext(); for (node v : adjacencies) { if (! G.hasEdge(u, v)) { G.insertEdge(u, v); } } } return G; } std::vector&lt;node&gt; METISParser::getNext() { std::string line; bool comment = false; do { comment = false; std::getline(this-&gt;graphFile, line); // check for comment line starting with '%' if (line[0] == '%') { comment = true; TRACE("comment line found"); } else { return parseLine(line); } } while (comment); } static std::vector&lt;node&gt; parseLine(std::string line) { std::stringstream stream(line); std::string token; char delim = ' '; std::vector&lt;node&gt; adjacencies; // split string and push adjacent nodes while (std::getline(stream, token, delim)) { node v = atoi(token.c_str()); adjacencies.push_back(v); } return adjacencies; } </code></pre> <p>To diagnose why it is so slow, I ran it in a profiler (Apple Instruments). The results were surprising: It's slow because of locking overhead. The program spends over 90% of its time in <code>pthread_mutex_lock</code> and <code>_pthread_cond_wait</code>.</p> <p><img src="https://i.stack.imgur.com/k4CFw.png" alt="Instruments"></p> <p>I have no idea where the locking overhead comes from, but I need to get rid of it. Can you suggest next steps?</p> <p>EDIT: See the call stack expanded for <code>_pthread_con_wait</code>. I cannot figure out the source of the locking overhead by looking at this:</p> <p><img src="https://i.stack.imgur.com/e0ek5.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload