include/vcl/glyphitem.hxx | 13 ++++---- vcl/source/gdi/impglyphitem.cxx | 61 ++++++++++++++++++++++++++++++++-------- 2 files changed, 57 insertions(+), 17 deletions(-)
New commits: commit d697e792c5b4b27aad8806694e1b8e22dcc70eae Author: Luboš Luňák <l.lu...@collabora.com> AuthorDate: Tue Mar 16 16:28:02 2021 +0100 Commit: Luboš Luňák <l.lu...@collabora.com> CommitDate: Wed Mar 17 22:49:57 2021 +0100 optimize SalLayoutGlyphs for the common case This should reduce memory usage (libstdc++ vector is 3 int's and allocates dynamically). The usual case should be no font fallback. Change-Id: I2e7981c0962f4f417fd024e3c27f01bc2a71127e Reviewed-on: https://gerrit.libreoffice.org/c/core/+/112591 Tested-by: Jenkins Reviewed-by: Luboš Luňák <l.lu...@collabora.com> diff --git a/include/vcl/glyphitem.hxx b/include/vcl/glyphitem.hxx index 02d783a3e166..d928bb954ee7 100644 --- a/include/vcl/glyphitem.hxx +++ b/include/vcl/glyphitem.hxx @@ -31,7 +31,11 @@ class SalLayoutGlyphsImpl; class VCL_DLLPUBLIC SalLayoutGlyphs final { - std::vector<SalLayoutGlyphsImpl*> m_pImpls; + SalLayoutGlyphsImpl* m_pImpl = nullptr; + // Extra items are in a dynamically allocated vector in order to save memory. + // The usual case should be that this stays unused (it should be only used + // when font fallback takes place). + std::vector<SalLayoutGlyphsImpl*>* m_pExtraImpls = nullptr; public: SalLayoutGlyphs() = default; @@ -42,11 +46,8 @@ public: SalLayoutGlyphs& operator=(const SalLayoutGlyphs&) = delete; SalLayoutGlyphs& operator=(SalLayoutGlyphs&&); - SalLayoutGlyphsImpl* Impl(unsigned int nLevel) const - { - return nLevel < m_pImpls.size() ? m_pImpls[nLevel] : nullptr; - } - void AppendImpl(SalLayoutGlyphsImpl* pImpl) { m_pImpls.push_back(pImpl); } + SalLayoutGlyphsImpl* Impl(unsigned int nLevel) const; + void AppendImpl(SalLayoutGlyphsImpl* pImpl); bool IsValid() const; void Invalidate(); diff --git a/vcl/source/gdi/impglyphitem.cxx b/vcl/source/gdi/impglyphitem.cxx index d271032e2ad6..e368305cdba1 100644 --- a/vcl/source/gdi/impglyphitem.cxx +++ b/vcl/source/gdi/impglyphitem.cxx @@ -25,35 +25,74 @@ SalLayoutGlyphs::~SalLayoutGlyphs() { - for (SalLayoutGlyphsImpl* impl : m_pImpls) - delete impl; + delete m_pImpl; + if (m_pExtraImpls) + for (SalLayoutGlyphsImpl* impl : *m_pExtraImpls) + delete impl; } -SalLayoutGlyphs::SalLayoutGlyphs(SalLayoutGlyphs&& rOther) { std::swap(m_pImpls, rOther.m_pImpls); } +SalLayoutGlyphs::SalLayoutGlyphs(SalLayoutGlyphs&& rOther) +{ + std::swap(m_pImpl, rOther.m_pImpl); + std::swap(m_pExtraImpls, rOther.m_pExtraImpls); +} SalLayoutGlyphs& SalLayoutGlyphs::operator=(SalLayoutGlyphs&& rOther) { if (this != &rOther) - std::swap(m_pImpls, rOther.m_pImpls); + { + std::swap(m_pImpl, rOther.m_pImpl); + std::swap(m_pExtraImpls, rOther.m_pExtraImpls); + } return *this; } bool SalLayoutGlyphs::IsValid() const { - if (m_pImpls.empty()) + if (m_pImpl == nullptr) + return false; + if (!m_pImpl->IsValid()) return false; - for (SalLayoutGlyphsImpl* impl : m_pImpls) - if (!impl->IsValid()) - return false; + if (m_pExtraImpls) + for (SalLayoutGlyphsImpl* impl : *m_pExtraImpls) + if (!impl->IsValid()) + return false; return true; } void SalLayoutGlyphs::Invalidate() { // Invalidating is in fact simply clearing. - for (SalLayoutGlyphsImpl* impl : m_pImpls) - delete impl; - m_pImpls.clear(); + delete m_pImpl; + m_pImpl = nullptr; + if (m_pExtraImpls) + { + for (SalLayoutGlyphsImpl* impl : *m_pExtraImpls) + delete impl; + delete m_pExtraImpls; + m_pExtraImpls = nullptr; + } +} + +SalLayoutGlyphsImpl* SalLayoutGlyphs::Impl(unsigned int nLevel) const +{ + if (nLevel == 0) + return m_pImpl; + if (m_pExtraImpls != nullptr && nLevel - 1 < m_pExtraImpls->size()) + return (*m_pExtraImpls)[nLevel - 1]; + return nullptr; +} + +void SalLayoutGlyphs::AppendImpl(SalLayoutGlyphsImpl* pImpl) +{ + if (m_pImpl == nullptr) + m_pImpl = pImpl; + else + { + if (m_pExtraImpls == nullptr) + m_pExtraImpls = new std::vector<SalLayoutGlyphsImpl*>; + m_pExtraImpls->push_back(pImpl); + } } SalLayoutGlyphsImpl* SalLayoutGlyphsImpl::clone() const { return new SalLayoutGlyphsImpl(*this); } _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits