Commit 074b9376 authored by Friedrich W. H. Kossebau's avatar Friedrich W. H. Kossebau
Browse files

Use reserve() before appending with known size

parent d9efcd2e
......@@ -95,6 +95,7 @@ void KDevelopSessions::loadSessions()
// Switch kdevelop session: -u
// Should we add a match for this option or would that clutter the matches too much?
const QStringList list = findSessions();
m_sessions.reserve(list.size());
foreach (const QString &sessionfile, list)
{
Session session;
......
......@@ -370,6 +370,7 @@ QList<Variable*> Locals::updateLocals(QStringList locals)
// Variables that changed type -- likewise.
QList<Variable*> ret;
ret.reserve(childItems.size());
foreach (TreeItem *i, childItems) {
Q_ASSERT(qobject_cast<Variable*>(i));
ret << static_cast<Variable*>(i);
......
......@@ -227,6 +227,7 @@ void ClassModel::nodesLayoutChanged(ClassModelNodes::Node*)
QModelIndexList oldIndexList = persistentIndexList();
QModelIndexList newIndexList;
newIndexList.reserve(oldIndexList.size());
foreach(const QModelIndex& oldIndex, oldIndexList)
{
Node* node = static_cast<Node*>(oldIndex.internalPointer());
......
......@@ -34,6 +34,7 @@ namespace KDevelop {
struct CodeCompletionItemLastGrouper {
CodeCompletionItemLastGrouper(QList<QExplicitlySharedDataPointer<CompletionTreeElement> >& tree, CompletionTreeNode* parent, QList<CompletionTreeItemPointer> items)
{
tree.reserve(tree.size() + items.size());
foreach( CompletionTreeItemPointer item, items ) {
item->setParent(parent);
tree << QExplicitlySharedDataPointer<CompletionTreeElement>( item.data() );
......@@ -60,6 +61,7 @@ struct CodeCompletionItemGrouper {
(*it).append(item);
}
tree.reserve(tree.size() + groups.size());
for( typename GroupMap::const_iterator it = groups.constBegin(); it != groups.constEnd(); ++it ) {
QExplicitlySharedDataPointer<CompletionTreeNode> node(new CompletionTreeNode());
node->setParent(parent);
......
......@@ -139,6 +139,7 @@ struct CodeCompletionItemTester
addElements(completionContext->ungroupedElements());
names.reserve(items.size());
foreach(Item i, items) {
names << i->data(fakeModel().index(0, KTextEditor::CodeCompletionModel::Name), Qt::DisplayRole, nullptr).toString();
}
......
......@@ -136,8 +136,9 @@ FunctionDescription::FunctionDescription(const DeclarationPointer& declaration)
}
int i = 0;
foreach (Declaration* arg, context->localDeclarations())
{
const auto localDeclarations = context->localDeclarations();
arguments.reserve(localDeclarations.size());
for (Declaration* arg : localDeclarations) {
VariableDescription var = VariableDescription(DeclarationPointer(arg));
if (function)
{
......
......@@ -257,6 +257,7 @@ namespace CodeDescription
template <class T> QVariant toVariantList(const QVector<T>& list)
{
QVariantList ret;
ret.reserve(list.size());
foreach (const T& t, list)
{
ret << QVariant::fromValue<T>(t);
......
......@@ -206,7 +206,9 @@ DocumentChangeSet::ChangeResult DocumentChangeSet::applyAllChanges()
}
QList<QUrl> allFiles;
foreach (IndexedString file, d->documentsRename.keys().toSet() + d->changes.keys().toSet()) {
const auto changedFiles = d->documentsRename.keys().toSet() + d->changes.keys().toSet();
allFiles.reserve(changedFiles.size());
for (const IndexedString& file : changedFiles) {
allFiles << file.toUrl();
}
......@@ -451,7 +453,10 @@ DocumentChangeSet::ChangeResult DocumentChangeSetPrivate::generateNewText(const
line.replace(change.m_range.start().column(), line.length() - change.m_range.start().column(),
change.m_newText);
// null other lines and remember for deletion
for(int i = change.m_range.start().line() + 1; i <= change.m_range.end().line(); ++i) {
const int firstLine = change.m_range.start().line() + 1;
const int lastLine = change.m_range.end().line();
removedLines.reserve(removedLines.size() + lastLine - firstLine + 1);
for (int i = firstLine; i <= lastLine; ++i) {
textLines[i].clear();
removedLines << i;
}
......
......@@ -89,6 +89,7 @@ ConfigOption SourceFileTemplatePrivate::readEntry(const QDomElement& element,
else if (tag == QLatin1String("choices")) {
QStringList values;
QDomNodeList choices = element.elementsByTagName(QStringLiteral("choice"));
values.reserve(choices.size());
for (int j = 0; j < choices.size(); ++j) {
QDomElement choiceElement = choices.at(j).toElement();
values << choiceElement.attribute(QStringLiteral("name"));
......
......@@ -235,6 +235,7 @@ QStringList TemplateRenderer::render(const QStringList& contents) const
{
qCDebug(LANGUAGE) << d->context.stackHash(0);
QStringList ret;
ret.reserve(contents.size());
foreach (const QString& content, contents)
{
ret << render(content);
......
......@@ -166,6 +166,7 @@ QStandardItem *TemplatesModelPrivate::createItem(const QString& name, const QStr
QStringList path = category.split(QLatin1Char('/'));
QStringList currentPath;
currentPath.reserve(path.size());
foreach (const QString& entry, path)
{
currentPath << entry;
......@@ -319,6 +320,7 @@ QModelIndexList TemplatesModel::templateIndexes(const QString& fileName) const
QStringList categories = general.readEntry("Category").split(QLatin1Char('/'));
QStringList levels;
levels.reserve(categories.size());
foreach (const QString& category, categories)
{
levels << category;
......
......@@ -866,12 +866,14 @@ QVector<DUContext*> DUContext::importers() const
ENSURE_CAN_READ
QVector<DUContext*> ret;
ret.reserve(d_func()->m_importersSize());
FOREACH_FUNCTION(const IndexedDUContext& ctx, d_func()->m_importers)
ret << ctx.context();
if(owner()) {
//Add indirect importers to the list
KDevVarLengthArray<IndexedDUContext> indirect = Importers::self().importers(owner()->id());
ret.reserve(ret.size() + indirect.size());
foreach (const IndexedDUContext ctx, indirect) {
ret << ctx.context();
}
......
......@@ -776,9 +776,11 @@ QStringList QualifiedIdentifier::toStringList(IdentifierStringFormattingOptions
ret.append(QString());
if(m_index) {
ret.reserve(ret.size() + cd->identifiersSize());
FOREACH_FUNCTION_STATIC(const IndexedIdentifier& index, cd->identifiers)
ret << index.identifier().toString(options);
}else{
ret.reserve(ret.size() + dd->identifiersSize());
FOREACH_FUNCTION_STATIC(const IndexedIdentifier& index, dd->identifiers)
ret << index.identifier().toString(options);
}
......
......@@ -198,6 +198,8 @@ void UsesCollector::startCollecting() {
///Step 4: Copy allDeclarations into m_declarations, build top-context list, etc.
QList<ReferencedTopDUContext> candidateTopContexts;
candidateTopContexts.reserve(allDeclarations.size());
m_declarations.reserve(allDeclarations.size());
foreach(const IndexedDeclaration d, allDeclarations) {
m_declarations << d;
m_declarationTopContexts.insert(d.indexedTopContext());
......
......@@ -174,6 +174,7 @@ QList< QExplicitlySharedDataPointer<ParsingEnvironmentFile> > ParsingEnvironment
IndexedTopDUContext top = indexedTopContext();
if(top.isLoaded()) {
TopDUContext* topCtx = top.data();
imp.reserve(topCtx->d_func()->m_importedContextsSize());
FOREACH_FUNCTION(const DUContext::Import& import, topCtx->d_func()->m_importedContexts)
imp << import.indexedContext();
}else{
......
......@@ -47,6 +47,7 @@ LocalIndexedProblem::LocalIndexedProblem(const ProblemPointer& problem, const To
// the const cast is ugly but we don't really "change" the state as observed from the outside
auto& serialized = const_cast<Problem*>(problem.data())->d_func_dynamic()->diagnosticsList();
serialized.clear();
serialized.reserve(problem->m_diagnostics.size());
foreach(const ProblemPointer& child, problem->m_diagnostics) {
serialized << LocalIndexedProblem(child, top);
}
......
......@@ -248,6 +248,7 @@ int findCommaOrEnd( const QString& str , int pos, QChar validEnd)
QString reverse( const QString& str ) {
QString ret;
int len = str.length();
ret.reserve(len);
for( int a = len-1; a >= 0; --a ) {
switch(str[a].unicode()) {
case '(':
......@@ -336,11 +337,13 @@ void skipFunctionArguments(QString str, QStringList& skippedArguments, int& argu
QString reduceWhiteSpace(QString str) {
str = str.trimmed();
QString ret;
const int len = str.length();
ret.reserve(len);
QChar spaceChar = QLatin1Char(' ');
bool hadSpace = false;
for( int a = 0; a < str.length(); a++ ) {
for (int a = 0; a < len; ++a) {
if( str[a].isSpace() ) {
hadSpace = true;
} else {
......@@ -351,7 +354,7 @@ QString reduceWhiteSpace(QString str) {
ret += str[a];
}
}
ret.squeeze();
return ret;
}
......
......@@ -287,6 +287,7 @@ void TopDUContextDynamicData::DUChainItemStorage<Item>::storeData(uint& currentD
{
auto const oldOffsets = offsets;
offsets.clear();
offsets.reserve(items.size());
for (int a = 0; a < items.size(); ++a) {
auto item = items[a];
if (!item) {
......
......@@ -108,6 +108,7 @@ QList<AbstractType::Ptr> FunctionType::arguments () const
{
///@todo Don't do the conversion
QList<AbstractType::Ptr> ret;
ret.reserve(d_func()->m_argumentsSize());
FOREACH_FUNCTION(const IndexedType& arg, d_func()->m_arguments)
ret << arg.abstractType();
return ret;
......
......@@ -198,6 +198,7 @@ public:
{
model->beginInsertRows( QModelIndex(), model->rowCount(), model->rowCount() + items.size() - 1);
m_filteredItems.reserve(m_filteredItems.size() + items.size());
foreach( const FilteredItem& item, items ) {
if( item.type == FilteredItem::ErrorItem ) {
m_errorItems.insert(m_filteredItems.size());
......
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