mirror of
https://github.com/verilator/verilator.git
synced 2026-10-06 10:03:44 +02:00
Internals: Optimize temporary memory allocations (#6517)
This patch gets rid of over 80% of temporary dynamic memory allocations (when a malloced node is immediately freed with no other malloc in between). It also gets rid of over 20% of all calls to malloc. It's worth ~3% average verilation speed up with tcmalloc, and more without tcmalloc.
This commit is contained in:
+8
-13
@@ -465,22 +465,14 @@ void V3Graph::subtreeLoops(V3EdgeFuncP edgeFuncp, V3GraphVertex* vertexp, V3Grap
|
||||
//######################################################################
|
||||
// Algorithms - sorting
|
||||
|
||||
struct GraphSortVertexCmp final {
|
||||
bool operator()(const V3GraphVertex* lhsp, const V3GraphVertex* rhsp) const {
|
||||
return lhsp->sortCmp(rhsp) < 0;
|
||||
}
|
||||
};
|
||||
struct GraphSortEdgeCmp final {
|
||||
bool operator()(const V3GraphEdge* lhsp, const V3GraphEdge* rhsp) const {
|
||||
return lhsp->sortCmp(rhsp) < 0;
|
||||
}
|
||||
};
|
||||
|
||||
void V3Graph::sortVertices() {
|
||||
// Sort list of vertices by rank, then fanout
|
||||
std::vector<V3GraphVertex*> vertexps;
|
||||
for (V3GraphVertex& vertex : m_vertices) vertexps.push_back(&vertex);
|
||||
std::stable_sort(vertexps.begin(), vertexps.end(), GraphSortVertexCmp());
|
||||
std::stable_sort(vertexps.begin(), vertexps.end(),
|
||||
[](const V3GraphVertex* lhsp, const V3GraphVertex* rhsp) { //
|
||||
return lhsp->sortCmp(rhsp) < 0;
|
||||
});
|
||||
// Re-insert in sorted order
|
||||
for (V3GraphVertex* const vertexp : vertexps) {
|
||||
m_vertices.unlink(vertexp);
|
||||
@@ -495,7 +487,10 @@ void V3Graph::sortEdges() {
|
||||
// Make a vector
|
||||
for (V3GraphEdge& edge : vertex.outEdges()) edges.push_back(&edge);
|
||||
// Sort
|
||||
std::stable_sort(edges.begin(), edges.end(), GraphSortEdgeCmp());
|
||||
std::stable_sort(edges.begin(), edges.end(),
|
||||
[](const V3GraphEdge* lhsp, const V3GraphEdge* rhsp) { //
|
||||
return lhsp->sortCmp(rhsp) < 0;
|
||||
});
|
||||
// Relink edges in specified order
|
||||
for (V3GraphEdge* const edgep : edges) edgep->relinkFromp(&vertex);
|
||||
// Prep for next
|
||||
|
||||
Reference in New Issue
Block a user