An emplace_hint with the iterator pointing at end() doesn't really help, so rather attempt an insert with a temporary value. Also check if the upper-case string we got back is the same as the input string, in which case, we can save memory by mapping the input string to itself. Change-Id: I40b9e2b65a831e44c4b88d51d835242a47d8a86d Reviewed-on: https://gerrit.libreoffice.org/72516 Tested-by: Jenkins Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
109 lines
2.9 KiB
C++
109 lines
2.9 KiB
C++
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
/*
|
|
* This file is part of the LibreOffice project.
|
|
*
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
*/
|
|
|
|
#include <svl/sharedstringpool.hxx>
|
|
#include <svl/sharedstring.hxx>
|
|
#include <unotools/charclass.hxx>
|
|
#include <osl/mutex.hxx>
|
|
|
|
#include <unordered_map>
|
|
#include <unordered_set>
|
|
|
|
namespace svl {
|
|
|
|
namespace {
|
|
|
|
sal_Int32 getRefCount( const rtl_uString* p )
|
|
{
|
|
return (p->refCount & 0x3FFFFFFF);
|
|
}
|
|
|
|
}
|
|
|
|
struct SharedStringPool::Impl
|
|
{
|
|
mutable osl::Mutex maMutex;
|
|
// set of upper-case, so we can share these as the value in the maStrMap
|
|
std::unordered_set<OUString> maStrPoolUpper;
|
|
// map with rtl_uString* as key so we can avoid some ref-counting
|
|
std::unordered_map<OUString,rtl_uString*> maStrMap;
|
|
const CharClass& mrCharClass;
|
|
|
|
explicit Impl( const CharClass& rCharClass ) : mrCharClass(rCharClass) {}
|
|
};
|
|
|
|
SharedStringPool::SharedStringPool( const CharClass& rCharClass ) :
|
|
mpImpl(new Impl(rCharClass)) {}
|
|
|
|
SharedStringPool::~SharedStringPool()
|
|
{
|
|
}
|
|
|
|
SharedString SharedStringPool::intern( const OUString& rStr )
|
|
{
|
|
osl::MutexGuard aGuard(&mpImpl->maMutex);
|
|
|
|
auto [mapIt,bInserted] = mpImpl->maStrMap.emplace(rStr, rStr.pData);
|
|
if (bInserted)
|
|
{
|
|
// This is a new string insertion. Establish mapping to upper-case variant.
|
|
OUString aUpper = mpImpl->mrCharClass.uppercase(rStr);
|
|
if (aUpper == rStr)
|
|
{
|
|
auto insertResult = mpImpl->maStrPoolUpper.insert(rStr);
|
|
mapIt->second = insertResult.first->pData;
|
|
}
|
|
else
|
|
{
|
|
auto insertResult = mpImpl->maStrPoolUpper.insert(aUpper);
|
|
mapIt->second = insertResult.first->pData;
|
|
}
|
|
}
|
|
return SharedString(mapIt->first.pData, mapIt->second);
|
|
}
|
|
|
|
void SharedStringPool::purge()
|
|
{
|
|
osl::MutexGuard aGuard(&mpImpl->maMutex);
|
|
|
|
std::unordered_set<OUString> aNewStrPoolUpper;
|
|
{
|
|
auto it = mpImpl->maStrMap.begin(), itEnd = mpImpl->maStrMap.end();
|
|
while (it != itEnd)
|
|
{
|
|
const rtl_uString* p = it->first.pData;
|
|
if (getRefCount(p) == 1)
|
|
it = mpImpl->maStrMap.erase(it);
|
|
else
|
|
{
|
|
// Still referenced outside the pool. Keep it.
|
|
aNewStrPoolUpper.insert(it->second);
|
|
++it;
|
|
}
|
|
}
|
|
}
|
|
mpImpl->maStrPoolUpper = std::move(aNewStrPoolUpper);
|
|
}
|
|
|
|
size_t SharedStringPool::getCount() const
|
|
{
|
|
osl::MutexGuard aGuard(&mpImpl->maMutex);
|
|
return mpImpl->maStrMap.size();
|
|
}
|
|
|
|
size_t SharedStringPool::getCountIgnoreCase() const
|
|
{
|
|
osl::MutexGuard aGuard(&mpImpl->maMutex);
|
|
return mpImpl->maStrPoolUpper.size();
|
|
}
|
|
|
|
}
|
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|