Optimization - mutex replaced by read/write lock

This commit is contained in:
Jakub Melka 2020-11-04 18:50:48 +01:00
parent 290c769cc3
commit 03c454951e

View File

@ -29,6 +29,7 @@
#include <freetype/t1tables.h> #include <freetype/t1tables.h>
#include <QMutex> #include <QMutex>
#include <QReadWriteLock>
#include <QPainterPath> #include <QPainterPath>
#include <QDataStream> #include <QDataStream>
#include <QTreeWidgetItem> #include <QTreeWidgetItem>
@ -426,11 +427,11 @@ private:
/// Function checks, if error occured, and if yes, then exception is thrown /// Function checks, if error occured, and if yes, then exception is thrown
static void checkFreeTypeError(FT_Error error); static void checkFreeTypeError(FT_Error error);
/// Mutex for accessing the glyph data /// Read/write lock for accessing the glyph data
QMutex m_mutex; QReadWriteLock m_readWriteLock;
/// Glyph cache, must be protected by the mutex above /// Glyph cache, must be protected by the mutex above
std::map<unsigned int, Glyph> m_glyphCache; std::unordered_map<unsigned int, Glyph> m_glyphCache;
/// For embedded fonts, this byte array contains embedded font data /// For embedded fonts, this byte array contains embedded font data
QByteArray m_embeddedFontData; QByteArray m_embeddedFontData;
@ -818,7 +819,10 @@ int PDFRealizedFontImpl::outlineCubicTo(const FT_Vector* control1, const FT_Vect
const PDFRealizedFontImpl::Glyph& PDFRealizedFontImpl::getGlyph(unsigned int glyphIndex) const PDFRealizedFontImpl::Glyph& PDFRealizedFontImpl::getGlyph(unsigned int glyphIndex)
{ {
QMutexLocker lock(&m_mutex); if (glyphIndex)
{
{
QReadLocker readLock(&m_readWriteLock);
// First look into cache // First look into cache
auto it = m_glyphCache.find(glyphIndex); auto it = m_glyphCache.find(glyphIndex);
@ -826,9 +830,9 @@ const PDFRealizedFontImpl::Glyph& PDFRealizedFontImpl::getGlyph(unsigned int gly
{ {
return it->second; return it->second;
} }
}
if (glyphIndex) QWriteLocker writeLock(&m_readWriteLock);
{
Glyph glyph; Glyph glyph;
FT_Outline_Funcs glyphOutlineInterface; FT_Outline_Funcs glyphOutlineInterface;
@ -845,8 +849,12 @@ const PDFRealizedFontImpl::Glyph& PDFRealizedFontImpl::getGlyph(unsigned int gly
glyph.advance = !m_isVertical ? m_face->glyph->advance.x : m_face->glyph->advance.y; glyph.advance = !m_isVertical ? m_face->glyph->advance.x : m_face->glyph->advance.y;
glyph.advance *= FONT_MULTIPLIER; glyph.advance *= FONT_MULTIPLIER;
m_glyphCache[glyphIndex] = qMove(glyph); auto it = m_glyphCache.find(glyphIndex);
return m_glyphCache[glyphIndex]; if (it == m_glyphCache.cend())
{
it = m_glyphCache.insert(std::make_pair(glyphIndex, qMove(glyph))).first;
}
return it->second;
} }
static Glyph dummy; static Glyph dummy;