Commit 14c4c518 authored by Jan Paul Batrina's avatar Jan Paul Batrina
Browse files

[ColorPicker] Use QVector instead of QHash for storing the indices

QHash::keys() returns a QList which needs to be converted into a QVector
for the return value, resulting to 2 allocations on a very hot code path.

The color indices are now stored in a pair of QVectors, one for the
color note indices (that needs to be returned in inlineNotes()) and
the other holds the corresponding indices. This removes the wasteful
allocations, but slightly increases memory consumption.
parent 7bc8a545
......@@ -176,11 +176,12 @@ QVector<int> ColorPickerInlineNoteProvider::inlineNotes(int line) const
colorNoteIndex = match.capturedStart();
}
m_colorNoteIndices[line][colorNoteIndex] = colorOtherIndex;
m_colorNoteIndices[line].colorNoteIndices.append(colorNoteIndex);
m_colorNoteIndices[line].otherColorIndices.append(colorOtherIndex);
}
}
return m_colorNoteIndices.value(line).keys().toVector();
return m_colorNoteIndices[line].colorNoteIndices;
}
QSize ColorPickerInlineNoteProvider::inlineNoteSize(const KTextEditor::InlineNote &note) const
......@@ -192,7 +193,11 @@ void ColorPickerInlineNoteProvider::paintInlineNote(const KTextEditor::InlineNot
{
const auto line = note.position().line();
auto colorEnd = note.position().column();
auto colorStart = m_colorNoteIndices[line][colorEnd];
const QVector<int> &colorNoteIndices = m_colorNoteIndices[line].colorNoteIndices;
// Since the colorNoteIndices are inserted in left-to-right (increasing) order in inlineNotes, we can use binary search to find the index (or color note number) for the line
const int colorNoteNumber = std::lower_bound(colorNoteIndices.cbegin(), colorNoteIndices.cend(), colorEnd) - colorNoteIndices.cbegin();
auto colorStart = m_colorNoteIndices[line].otherColorIndices[colorNoteNumber];
if (colorStart > colorEnd) {
colorEnd = colorStart;
colorStart = note.position().column();
......@@ -216,7 +221,11 @@ void ColorPickerInlineNoteProvider::inlineNoteActivated(const KTextEditor::Inlin
{
const auto line = note.position().line();
auto colorEnd = note.position().column();
auto colorStart = m_colorNoteIndices[line][colorEnd];
const QVector<int> &colorNoteIndices = m_colorNoteIndices[line].colorNoteIndices;
// Since the colorNoteIndices are inserted in left-to-right (increasing) order in inlineNotes, we can use binary search to find the index (or color note number) for the line
const int colorNoteNumber = std::lower_bound(colorNoteIndices.cbegin(), colorNoteIndices.cend(), colorEnd) - colorNoteIndices.cbegin();
auto colorStart = m_colorNoteIndices[line].otherColorIndices[colorNoteNumber];
if (colorStart > colorEnd) {
colorEnd = colorStart;
colorStart = note.position().column();
......
......@@ -41,10 +41,16 @@ private:
int m_startChangedLines = -1;
int m_previousNumLines = -1;
// line, <colorNoteIndex, otherColorIndex>
mutable QHash<int, QHash<int, int>> m_colorNoteIndices;
struct ColorIndices {
// When m_putPreviewAfterColor is true, otherColorIndices holds the starting color indices while colorNoteIndices holds the end color indices
// colorNoteIndices[i] corresponds to otherColorIndices[i]
QVector<int> colorNoteIndices;
QVector<int> otherColorIndices;
};
// mutable is used here since InlineNoteProvider::inlineNotes is const only, and we update the notes lazily (only when inlineNotes is called)
mutable QHash<int, ColorIndices> m_colorNoteIndices;
// config variables shared between all note providers
QRegularExpression m_colorRegEx;
bool m_putPreviewAfterColor;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment