Commit d32d3b97 authored by Igor Kushnir's avatar Igor Kushnir Committed by Milian Wolff
Browse files

ItemRepositoryReferenceCounting: remove extra checks

For many years IndexedIdentifier and IndexedQualifiedIdentifier didn't
have m_index or index validity checks. These classes don't need the
checks (otherwise, there would be assertion failures in
ItemRepository::verifyIndex()). The checks were added to
ItemRepositoryReferenceCounting utility functions in a recent commit
"Port instantiation information type repository to external locking"
because IndexedInstantiationInformation needs them. I can see two
disadvantages of the redundant checks:
1. The knowledge that the index is never zero for *Identifier classes is
   lost.
2. Extra checks have performance cost.

This commit moves the checks into IndexedInstantiationInformation's
member functions, thereby freeing the *Identifier classes from them.
parent 834fb13b
......@@ -155,7 +155,9 @@ IndexedInstantiationInformation::IndexedInstantiationInformation(uint index) : m
if (m_index == standardInstantiationInformationIndex())
m_index = 0;
ItemRepositoryReferenceCounting::inc(this);
if (m_index) {
ItemRepositoryReferenceCounting::inc(this);
}
}
// NOTE: the definitions of ItemRepositoryReferenceCounting's inc(), dec() and setIndex() are so
......@@ -169,18 +171,23 @@ IndexedInstantiationInformation::IndexedInstantiationInformation(uint index) : m
IndexedInstantiationInformation::IndexedInstantiationInformation(const IndexedInstantiationInformation& rhs) noexcept
: m_index(rhs.m_index)
{
ItemRepositoryReferenceCounting::inc(this);
if (m_index) {
ItemRepositoryReferenceCounting::inc(this);
}
}
IndexedInstantiationInformation& IndexedInstantiationInformation::operator=(const IndexedInstantiationInformation& rhs) noexcept
{
ItemRepositoryReferenceCounting::setIndex(this, m_index, rhs.m_index);
const auto checkIndex = [](unsigned int index) { return index != 0; };
ItemRepositoryReferenceCounting::setIndex(this, m_index, rhs.m_index, checkIndex);
return *this;
}
IndexedInstantiationInformation::~IndexedInstantiationInformation()
{
ItemRepositoryReferenceCounting::dec(this);
if (m_index) {
ItemRepositoryReferenceCounting::dec(this);
}
}
bool IndexedInstantiationInformation::isValid() const
......
......@@ -17,8 +17,9 @@ struct ItemRepositoryReferenceCounting {
{
Q_ASSERT(item);
const auto index = item->index();
Q_ASSERT(index);
if (!index || !shouldDoDUChainReferenceCounting(item)) {
if (!shouldDoDUChainReferenceCounting(item)) {
return false;
}
......@@ -32,8 +33,9 @@ struct ItemRepositoryReferenceCounting {
{
Q_ASSERT(item);
const auto index = item->index();
Q_ASSERT(index);
if (!index || !shouldDoDUChainReferenceCounting(item)) {
if (!shouldDoDUChainReferenceCounting(item)) {
return false;
}
......@@ -42,8 +44,17 @@ struct ItemRepositoryReferenceCounting {
return true;
}
template <typename Item>
static inline void setIndex(Item* item, unsigned int& m_index, unsigned int index)
struct AssumeValidIndex {
bool operator()(unsigned int index) const
{
Q_ASSERT(index);
return true;
}
};
template <typename Item, typename CheckIndex = AssumeValidIndex>
static inline void setIndex(Item* item, unsigned int& m_index, unsigned int index,
CheckIndex checkIndex = AssumeValidIndex{})
{
Q_ASSERT(item);
if (m_index == index) {
......@@ -52,13 +63,13 @@ struct ItemRepositoryReferenceCounting {
if (shouldDoDUChainReferenceCounting(item)) {
LockedItemRepository::write<Item>([&](auto& repo) {
if (m_index) {
if (checkIndex(m_index)) {
item->decrease(repo.dynamicItemFromIndexSimple(m_index)->m_refCount, m_index);
}
m_index = index;
if (m_index) {
if (checkIndex(m_index)) {
item->increase(repo.dynamicItemFromIndexSimple(m_index)->m_refCount, m_index);
}
});
......
Supports Markdown
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