From 0b7cf2e7ec34d2d2261fe2b9f03ac4e4f1da4799 Mon Sep 17 00:00:00 2001 From: German Semenov Date: Fri, 3 Nov 2023 04:56:32 -0500 Subject: [PATCH 1/2] Optimize inserts using reserve() --- src/core/Cutter.cpp | 8 ++++++-- src/core/MainWindow.cpp | 3 ++- src/widgets/DisassemblerGraphView.cpp | 3 ++- src/widgets/HeapBinsGraphView.cpp | 8 ++++++-- src/widgets/RegisterRefsWidget.cpp | 1 + src/widgets/RizinGraphWidget.cpp | 1 + src/widgets/StackWidget.cpp | 1 + 7 files changed, 19 insertions(+), 6 deletions(-) diff --git a/src/core/Cutter.cpp b/src/core/Cutter.cpp index b70b15855e..3884a56319 100644 --- a/src/core/Cutter.cpp +++ b/src/core/Cutter.cpp @@ -254,6 +254,7 @@ QVector CutterCore::getCutterRCFilePaths() const QVector result; result.push_back(QFileInfo(QDir::home(), ".cutterrc").absoluteFilePath()); QStringList locations = QStandardPaths::standardLocations(QStandardPaths::AppConfigLocation); + result.reserve(locations.size() + 1); for (auto &location : locations) { result.push_back(QFileInfo(QDir(location), ".cutterrc").absoluteFilePath()); } @@ -1851,6 +1852,7 @@ QVector CutterCore::getRegisterRefValues() } RzListIter *it; RzRegItem *ri; + result.reserve(ritems->length); CutterRzListForeach (ritems, it, RzRegItem, ri) { RegisterRefValueDescription desc; desc.name = ri->name; @@ -2806,8 +2808,10 @@ QList CutterCore::getBreakpoints() CORE_LOCK(); QList ret; // TODO: use higher level API, don't touch rizin bps_idx directly - for (int i = 0; i < core->dbg->bp->bps_idx_count; i++) { - if (auto bpi = core->dbg->bp->bps_idx[i]) { + const auto* breakpoints = core->dbg->bp; + ret.reserve(breakpoints->bps_idx_count); + for (int i = 0; i < breakpoints->bps_idx_count; i++) { + if (auto bpi = breakpoints->bps_idx[i]) { ret.push_back(breakpointDescriptionFromRizin(i, bpi)); } } diff --git a/src/core/MainWindow.cpp b/src/core/MainWindow.cpp index e7364d4387..fca80a2ae6 100644 --- a/src/core/MainWindow.cpp +++ b/src/core/MainWindow.cpp @@ -412,7 +412,8 @@ void MainWindow::initDocks() auto makeActionList = [this](QList docks) { QList result; - for (auto dock : docks) { + result.reserve(docks.size()); + for (const auto* dock : docks) { if (dock != nullptr) { result.push_back(dock->toggleViewAction()); } else { diff --git a/src/widgets/DisassemblerGraphView.cpp b/src/widgets/DisassemblerGraphView.cpp index 82da086613..7ceaf5c696 100644 --- a/src/widgets/DisassemblerGraphView.cpp +++ b/src/widgets/DisassemblerGraphView.cpp @@ -280,8 +280,9 @@ void DisassemblerGraphView::loadCurrentGraph() rz_core_print_disasm(core, bbi->addr, buf.get(), (int)bbi->size, (int)bbi->size, NULL, &options); - auto vecVisitor = CutterPVector(vec.get()); + auto vecVisitor = CutterPVector(options.vec); auto iter = vecVisitor.begin(); + db.instrs.reserve(rz_pvector_len(options.vec)); while (iter != vecVisitor.end()) { RzAnalysisDisasmText *op = *iter; Instr instr; diff --git a/src/widgets/HeapBinsGraphView.cpp b/src/widgets/HeapBinsGraphView.cpp index c81c7ae328..e34ce863ee 100644 --- a/src/widgets/HeapBinsGraphView.cpp +++ b/src/widgets/HeapBinsGraphView.cpp @@ -85,8 +85,9 @@ void HeapBinsGraphView::display_single_linked_list(QVector chunk addBlock(gbBin, content); // add the graph blocks for the chunks + GraphLayout::GraphBlock gbChunk; + gbChunk.edges.reserve(chunks.size()); for (int i = 0; i < chunks.size(); i++) { - GraphLayout::GraphBlock gbChunk; gbChunk.entry = chunks[i].addr; if (tcache && chunks[i].fd) { @@ -101,6 +102,7 @@ void HeapBinsGraphView::display_single_linked_list(QVector chunk } addBlock(gbChunk, chunks[i].content, chunks[i].addr); + memset(&gbChunk, 0, sizeof(gbChunk)); } // add the END block if no message @@ -126,8 +128,9 @@ void HeapBinsGraphView::display_double_linked_list(QVector chunk addBlock(gbBin, content, heapBin->addr); // add the blocks for the chunks + GraphLayout::GraphBlock gbChunk; + gbChunk.edges.reserve(chunks.size() * 2); for (int i = 0; i < chunks.size(); i++) { - GraphLayout::GraphBlock gbChunk; gbChunk.entry = chunks[i].addr; gbChunk.edges.emplace_back(chunks[i].fd); gbChunk.edges.emplace_back(chunks[i].bk); @@ -138,6 +141,7 @@ void HeapBinsGraphView::display_double_linked_list(QVector chunk } addBlock(gbChunk, chunks[i].content, chunks[i].addr); + memset(&gbChunk, 0, sizeof(gbChunk)); } } diff --git a/src/widgets/RegisterRefsWidget.cpp b/src/widgets/RegisterRefsWidget.cpp index c35e29364a..0b3a63b596 100644 --- a/src/widgets/RegisterRefsWidget.cpp +++ b/src/widgets/RegisterRefsWidget.cpp @@ -185,6 +185,7 @@ void RegisterRefsWidget::refreshRegisterRef() registerRefModel->beginResetModel(); registerRefs.clear(); + registerRefs.reserve(Core()->getRegisterRefs().size()); for (const RegisterRef ® : Core()->getRegisterRefs()) { RegisterRefDescription desc; diff --git a/src/widgets/RizinGraphWidget.cpp b/src/widgets/RizinGraphWidget.cpp index 380657ee1f..4118203b40 100644 --- a/src/widgets/RizinGraphWidget.cpp +++ b/src/widgets/RizinGraphWidget.cpp @@ -115,6 +115,7 @@ void GenericRizinGraphView::loadCurrentGraph() auto edges = block["out_nodes"]; GraphLayout::GraphBlock layoutBlock; layoutBlock.entry = id; + layoutBlock.edges.reserve(edges.size()); for (auto edge : edges) { auto targetId = edge.toUt64(); layoutBlock.edges.emplace_back(targetId); diff --git a/src/widgets/StackWidget.cpp b/src/widgets/StackWidget.cpp index 71b66fe49c..bdd908b094 100644 --- a/src/widgets/StackWidget.cpp +++ b/src/widgets/StackWidget.cpp @@ -149,6 +149,7 @@ void StackModel::reload() beginResetModel(); values.clear(); + values.reserve(stackItems.size()); for (const AddrRefs &stackItem : stackItems) { Item item; From ccd62042c1a43112312ec2347bd7bcb8fb44b732 Mon Sep 17 00:00:00 2001 From: German Semenov Date: Fri, 3 Nov 2023 04:57:48 -0500 Subject: [PATCH 2/2] Added another microoptimizations --- src/widgets/ColorPicker.cpp | 5 +++-- src/widgets/ColorThemeListView.cpp | 2 +- src/widgets/DecompilerWidget.cpp | 5 +++-- src/widgets/DisassemblyWidget.cpp | 2 +- src/widgets/GraphGridLayout.cpp | 18 +++++++++--------- src/widgets/HexdumpWidget.cpp | 2 +- 6 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/widgets/ColorPicker.cpp b/src/widgets/ColorPicker.cpp index e35f1c5ffb..9e8209748d 100644 --- a/src/widgets/ColorPicker.cpp +++ b/src/widgets/ColorPicker.cpp @@ -26,8 +26,9 @@ void ColorPickArea::paintEvent(QPaintEvent *event) { QPainter p(this); - for (int x = event->rect().x(); x <= event->rect().right(); x++) { - for (int y = event->rect().y(); y <= event->rect().bottom(); y++) { + const auto& erect = event->rect(); + for (int x = erect.x(); x <= erect.right(); x++) { + for (int y = erect.y(); y <= erect.bottom(); y++) { qhelpers::ColorFloat h, s, v; QColor c = pointToColor(x, y); c.getHsvF(&h, &s, &v); diff --git a/src/widgets/ColorThemeListView.cpp b/src/widgets/ColorThemeListView.cpp index c662a5300e..83845e91de 100644 --- a/src/widgets/ColorThemeListView.cpp +++ b/src/widgets/ColorThemeListView.cpp @@ -371,7 +371,7 @@ void ColorSettingsModel::updateTheme() theme.clear(); ColorThemeWorker::Theme obj = ThemeWorker().getTheme(Config()->getColorTheme()); - for (auto it = obj.constBegin(); it != obj.constEnd(); it++) { + for (auto it = obj.constBegin(); it != obj.constEnd(); ++it) { theme.push_back({ it.key(), it.value(), false }); } diff --git a/src/widgets/DecompilerWidget.cpp b/src/widgets/DecompilerWidget.cpp index c54ad6f939..1a32e83326 100644 --- a/src/widgets/DecompilerWidget.cpp +++ b/src/widgets/DecompilerWidget.cpp @@ -57,9 +57,10 @@ DecompilerWidget::DecompilerWidget(MainWindow *main) selectedDecompilerId = "ghidra"; } for (Decompiler *dec : decompilers) { - ui->decompilerComboBox->addItem(dec->getName(), dec->getId()); + auto* combobox = ui->decompilerComboBox; + combobox->addItem(dec->getName(), dec->getId()); if (dec->getId() == selectedDecompilerId) { - ui->decompilerComboBox->setCurrentIndex(ui->decompilerComboBox->count() - 1); + combobox->setCurrentIndex(combobox->count() - 1); } } decompilerSelectionEnabled = decompilers.size() > 1; diff --git a/src/widgets/DisassemblyWidget.cpp b/src/widgets/DisassemblyWidget.cpp index 946a2d357c..42773d77c6 100644 --- a/src/widgets/DisassemblyWidget.cpp +++ b/src/widgets/DisassemblyWidget.cpp @@ -376,7 +376,7 @@ void DisassemblyWidget::highlightCurrentLine() // Highlight the current line QTextEdit::ExtraSelection highlightSelection; - highlightSelection.cursor = cursor; + highlightSelection.cursor = std::move(cursor); highlightSelection.cursor.movePosition(QTextCursor::Start); while (true) { RVA lineOffset = DisassemblyPreview::readDisassemblyOffset(highlightSelection.cursor); diff --git a/src/widgets/GraphGridLayout.cpp b/src/widgets/GraphGridLayout.cpp index 0cf7840d90..65dd8c5161 100644 --- a/src/widgets/GraphGridLayout.cpp +++ b/src/widgets/GraphGridLayout.cpp @@ -806,12 +806,12 @@ void calculateSegmentOffsets(std::vector &segments, std::vectorx + 1 < x) { - rightSideIt++; + ++rightSideIt; } while (rightSideIt != nodeRightSide.end() && rightSideIt->x + 1 == x) { maxSegment.setRange(rightSideIt->y0, rightSideIt->y1 + 1, rightSideIt->size - leftColumWidth); - rightSideIt++; + ++rightSideIt; } while (nextSegmentIt != segments.end() && nextSegmentIt->x == x @@ -823,7 +823,7 @@ void calculateSegmentOffsets(std::vector &segments, std::vectorspacingOverride ? nextSegmentIt->spacingOverride : segmentSpacing; maxSegment.setRange(nextSegmentIt->y0, nextSegmentIt->y1 + 1, y); edgeOffsets[nextSegmentIt->edgeIndex] = y; - nextSegmentIt++; + ++nextSegmentIt; } auto firstRightSideSegment = nextSegmentIt; @@ -836,19 +836,19 @@ void calculateSegmentOffsets(std::vector &segments, std::vectorx < x) { - leftSideIt++; + ++leftSideIt; } while (leftSideIt != nodeLeftSide.end() && leftSideIt->x == x) { maxSegment.setRange(leftSideIt->y0, leftSideIt->y1 + 1, leftSideIt->size - rightColumnWidth); - leftSideIt++; + ++leftSideIt; } while (nextSegmentIt != segments.end() && nextSegmentIt->x == x) { int y = maxSegment.rangeMaximum(nextSegmentIt->y0, nextSegmentIt->y1 + 1); y += nextSegmentIt->spacingOverride ? nextSegmentIt->spacingOverride : segmentSpacing; maxSegment.setRange(nextSegmentIt->y0, nextSegmentIt->y1 + 1, y); edgeOffsets[nextSegmentIt->edgeIndex] = y; - nextSegmentIt++; + ++nextSegmentIt; } auto rightSideMiddle = std::max(maxSegment.rangeMaximum(0, H), 0); rightSideMiddle = @@ -916,10 +916,10 @@ static void centerEdges(std::vector &segmentOffsets, const std::vector int offset = segmentOffsets[it->index]; left = std::min(left, offset); right = std::max(right, offset); - it++; + ++it; } int spacing = (edgeColumnWidth[chunkStart->x] - (right - left)) / 2 - left; - for (auto segment = chunkStart; segment != it; segment++) { + for (auto segment = chunkStart; segment != it; ++segment) { if (segment->start) { segmentOffsets[segment->index] += spacing; } @@ -1556,7 +1556,7 @@ static void createInequalitiesFromSegments(std::vector segments, ++it; } if (startPos->first < segment.y0) { - startPos++; + ++startPos; } lastSegments.erase(startPos, it); // erase segments covered by current one lastSegments[segment.y0] = { diff --git a/src/widgets/HexdumpWidget.cpp b/src/widgets/HexdumpWidget.cpp index 306582d9fc..02dee8dd23 100644 --- a/src/widgets/HexdumpWidget.cpp +++ b/src/widgets/HexdumpWidget.cpp @@ -238,7 +238,7 @@ void HexdumpWidget::updateParseWindow(RVA start_address, int size) tempConfig.set("asm.arch", arch).set("asm.bits", bits).set("cfg.bigendian", bigEndian); ui->hexDisasTextEdit->setPlainText( - selectedCommand != "" ? Core()->cmdRawAt( + !selectedCommand.isEmpty() ? Core()->cmdRawAt( QString("%1 @! %2").arg(selectedCommand).arg(size), start_address) : ""); } else {