Files
sqlitebrowser/src/IconCache.cpp
Martin Kleusberg 5589bd9da4 Speed up the loading of the database structure
While testing the original database from issue #1892 which contains
hundreds of tables and fields, I noticed how long the loading of the
database structure takes. This is especially problematic since it needs
to be reloaded on various occasions, e.g. running most statements in the
Execute SQL tab, which stalls the application every time.

According to a profiler it is the QIcon constructor which requires most
of the time. By introducing this small icon cache class we can reduce
the time for loading icons to almost nothing.

While still not perfect the UI already feels much more responsive with
this patch.
2019-06-28 14:58:50 +02:00

23 lines
731 B
C++

#include "IconCache.h"
QIcon IconCache::null_icon;
std::unordered_map<std::string, QIcon> IconCache::icons;
const QIcon& IconCache::get(const std::string& name)
{
// Check if we already have an icon object with that name in the cache. If so, just return that
auto it = icons.find(name);
if(it != icons.end())
return it->second;
// If now, try to load an icon with that name and insert it into the cache
QIcon icon(":/icons/" + QString::fromStdString(name));
auto ins = icons.insert({name, icon});
// If the insertion was successful, return the inserted icon object. If it was not, return a null icon.
if(ins.second)
return ins.first->second;
else
return null_icon;
}