Skip to content

Commit

Permalink
use only relevant content of sourcecode file in dissassembly view
Browse files Browse the repository at this point in the history
I updated the SourceCodeModel to only include the function that is
disassembled. Until now the whole file was included which wasted a lot
of performance since KSyntaxHighlighter was run on the whole file.
  • Loading branch information
lievenhey committed Mar 10, 2023
1 parent fc20748 commit 14e52cb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
53 changes: 33 additions & 20 deletions src/models/sourcecodemodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,6 @@ void SourceCodeModel::setDisassembly(const DisassemblyOutput& disassemblyOutput,

m_mainSourceFileName = disassemblyOutput.mainSourceFileName;

const auto sourceCode = QString::fromUtf8(file.readAll());

m_document->clear();

m_document->setPlainText(sourceCode);
m_document->setTextWidth(m_document->idealWidth());

for (const auto& line : disassemblyOutput.disassemblyLines) {
if (line.fileLine.line == 0 || line.fileLine.file != disassemblyOutput.mainSourceFileName) {
continue;
Expand Down Expand Up @@ -101,9 +94,23 @@ void SourceCodeModel::setDisassembly(const DisassemblyOutput& disassemblyOutput,
Q_ASSERT(minLineNumber < maxLineNumber);

m_prettySymbol = disassemblyOutput.symbol.prettySymbol;
m_startLine = minLineNumber - 2;
m_lineOffset = minLineNumber - 1;
m_numLines = maxLineNumber - m_startLine;
m_startLine = minLineNumber - 1; // convert to index
m_numLines = maxLineNumber - minLineNumber + 1; // include minLineNumber

for (int i = 0; i < m_startLine; i++) {
file.readLine();
}

QString sourceCode;

for (int i = 0; i < m_numLines; i++) {
sourceCode += QString::fromUtf8(file.readLine());
}

m_document->clear();

m_document->setPlainText(sourceCode);
m_document->setTextWidth(m_document->idealWidth());
}

QVariant SourceCodeModel::headerData(int section, Qt::Orientation orientation, int role) const
Expand Down Expand Up @@ -142,7 +149,7 @@ QVariant SourceCodeModel::data(const QModelIndex& index, int role) const
return {};
}

const auto fileLine = Data::FileLine(m_mainSourceFileName, index.row() + m_lineOffset);
const auto fileLine = Data::FileLine(m_mainSourceFileName, index.row() + m_startLine);
if (role == FileLineRole) {
return QVariant::fromValue(fileLine);
} else if (role == Qt::ToolTipRole) {
Expand All @@ -154,7 +161,8 @@ QVariant SourceCodeModel::data(const QModelIndex& index, int role) const
if (index.row() == 0) {
return m_prettySymbol;
}
const auto block = m_document->findBlockByLineNumber(index.row() + m_startLine);
// row 0 is the function name, we need to substract one to get the line index
const auto block = m_document->findBlockByLineNumber(index.row() - 1);
if (!block.isValid())
return {};
if (role == SyntaxHighlightRole)
Expand All @@ -166,7 +174,7 @@ QVariant SourceCodeModel::data(const QModelIndex& index, int role) const
return fileLine.line;
}

auto cost = [role, id = index.row() + m_lineOffset](int type, const Data::Costs& costs) -> QVariant {
auto cost = [role, id = index.row() + m_startLine](int type, const Data::Costs& costs) -> QVariant {
const auto cost = costs.cost(type, id);
const auto totalCost = costs.totalCost(type);
if (role == CostRole) {
Expand All @@ -185,9 +193,9 @@ QVariant SourceCodeModel::data(const QModelIndex& index, int role) const
return cost(column, m_selfCosts);
return cost(column - m_selfCosts.numTypes(), m_inclusiveCosts);
} else if (role == HighlightRole) {
return index.row() + m_lineOffset == m_highlightLine;
return index.row() + m_startLine == m_highlightLine;
} else if (role == RainbowLineNumberRole) {
int line = index.row() + m_lineOffset;
int line = index.row() + m_startLine;
if (m_validLineNumbers.contains(line))
return line;
return -1;
Expand All @@ -202,7 +210,12 @@ int SourceCodeModel::columnCount(const QModelIndex& parent) const

int SourceCodeModel::rowCount(const QModelIndex& parent) const
{
return parent.isValid() ? 0 : m_numLines;
// don't show the function name, when we have no source code
if (m_numLines == 0)
return 0;

// 1 line for the function name + source codes lines
return parent.isValid() ? 0 : m_numLines + 1;
}

void SourceCodeModel::updateHighlighting(int line)
Expand All @@ -215,15 +228,15 @@ Data::FileLine SourceCodeModel::fileLineForIndex(const QModelIndex& index) const
{
if (!index.isValid())
return {};
return {m_mainSourceFileName, index.row() + m_lineOffset};
return {m_mainSourceFileName, index.row() + m_startLine};
}

QModelIndex SourceCodeModel::indexForFileLine(const Data::FileLine& fileLine) const
{
if (fileLine.file != m_mainSourceFileName || fileLine.line < m_lineOffset
|| fileLine.line >= m_lineOffset + m_numLines)
if (fileLine.file != m_mainSourceFileName || fileLine.line < m_startLine
|| fileLine.line > m_startLine + m_numLines)
return {};
return index(fileLine.line - m_lineOffset, 0);
return index(fileLine.line - m_startLine, 0);
}

void SourceCodeModel::setSysroot(const QString& sysroot)
Expand Down
1 change: 0 additions & 1 deletion src/models/sourcecodemodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ public slots:
Data::Costs m_inclusiveCosts;
QString m_mainSourceFileName;
QString m_prettySymbol;
int m_lineOffset = 0;
int m_startLine = 0;
int m_numLines = 0;
int m_highlightLine = 0;
Expand Down

0 comments on commit 14e52cb

Please sign in to comment.