Commit 7477a4a1 authored by Igor Kushnir's avatar Igor Kushnir
Browse files

ProjectFileDataProvider: clear items when no open projects left

This change speeds up ProjectFileDataProvider::projectClosing() during
KDevelop exit while a single project - kdevelop itself with a little
more than 67 thousand files - is open, from 14 ms to 13 ms on average.
In case of WebKit with its almost 320 thousand files as the single
project - from 56 ms to 54 ms.

When the last project is closed, there is a unique opportunity to free
the memory of m_projectFiles without an extra allocation and without
moving any ProjectFile objects. `m_projectFiles = {}` in place of
`m_projectFiles.clear()` does not affect the execution time of
projectClosing(). However releasing the memory can slow down the next
call to ProjectFileDataProvider::projectOpened(). When the capacity of
m_projectFiles is 0, projectOpened() has to repeatedly reallocate memory
and move ProjectFile objects while iterating over the files of the just
opened project. When all projects are closed, the user is likely exiting
KDevelop (in which case `= {}` vs `clear()` does not matter) or is going
to open another project soon.

Even when a huge project with 300 thousand files is closed, at most mere
15 MB could be released here, because m_projectFiles.capacity() would be
at most 600 thousand and sizeof(ProjectFile) == 24.
parent 8e0f203a
......@@ -244,6 +244,14 @@ void ProjectFileDataProvider::projectClosing(IProject* project)
// file additions to the project that is about to be closed and destroyed.
disconnect(project, nullptr, this, nullptr);
if (ICore::self()->projectController()->projectCount() == 0) {
// No open projects left => just remove all files. This is a little faster
// than the algorithm below. Releasing the memory here would slow down the
// next call to projectOpened() => keep the capacity of m_projectFiles.
const Path projectPath = project->path();
const auto logicalEnd = std::remove_if(m_projectFiles.begin(), m_projectFiles.end(),
[&projectPath](const ProjectFile& f) {
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