Skip to content

Commit

Permalink
Merge pull request stellar#3244 from MonsieurNicolas/perf3b
Browse files Browse the repository at this point in the history
Some more perf tweaks

Reviewed-by: jonjove
  • Loading branch information
latobarita authored Oct 15, 2021
2 parents 66f83f8 + f6d799d commit dc5f5a3
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 55 deletions.
84 changes: 71 additions & 13 deletions src/ledger/InternalLedgerEntry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0

#include "ledger/InternalLedgerEntry.h"
#include "ledger/LedgerHashUtils.h"
#include "util/GlobalChecks.h"
#include "util/XDRCereal.h"
#include "util/types.h"
Expand Down Expand Up @@ -78,19 +79,19 @@ InternalLedgerKey::InternalLedgerKey(InternalLedgerEntryType t) : mType(t)
InternalLedgerKey::InternalLedgerKey(LedgerKey const& lk)
: InternalLedgerKey(InternalLedgerEntryType::LEDGER_ENTRY)
{
ledgerKey() = lk;
ledgerKeyRef() = lk;
}

InternalLedgerKey::InternalLedgerKey(SponsorshipKey const& sk)
: InternalLedgerKey(InternalLedgerEntryType::SPONSORSHIP)
{
sponsorshipKey() = sk;
sponsorshipKeyRef() = sk;
}

InternalLedgerKey::InternalLedgerKey(SponsorshipCounterKey const& sck)
: InternalLedgerKey(InternalLedgerEntryType::SPONSORSHIP_COUNTER)
{
sponsorshipCounterKey() = sck;
sponsorshipCounterKeyRef() = sck;
}

InternalLedgerKey::InternalLedgerKey(InternalLedgerKey const& glk)
Expand All @@ -105,6 +106,22 @@ InternalLedgerKey::InternalLedgerKey(InternalLedgerKey&& glk)
assign(std::move(glk));
}

InternalLedgerKey
InternalLedgerKey::makeSponsorshipKey(AccountID const& sponsoredId)
{
InternalLedgerKey res(InternalLedgerEntryType::SPONSORSHIP);
res.sponsorshipKeyRef().sponsoredID = sponsoredId;
return res;
}

InternalLedgerKey
InternalLedgerKey::makeSponsorshipCounterKey(AccountID const& sponsoringId)
{
InternalLedgerKey res(InternalLedgerEntryType::SPONSORSHIP_COUNTER);
res.sponsorshipCounterKeyRef().sponsoringID = sponsoringId;
return res;
}

InternalLedgerKey&
InternalLedgerKey::operator=(InternalLedgerKey const& glk)
{
Expand All @@ -131,20 +148,50 @@ InternalLedgerKey::~InternalLedgerKey()
destruct();
}

size_t
InternalLedgerKey::hash() const
{
if (mHash != 0)
{
return mHash;
}
size_t res;
switch (type())
{
case stellar::InternalLedgerEntryType::LEDGER_ENTRY:
res = std::hash<stellar::LedgerKey>()(ledgerKey());
break;
case stellar::InternalLedgerEntryType::SPONSORSHIP:
res = shortHash::computeHash(stellar::ByteSlice(
sponsorshipKey().sponsoredID.ed25519().data(), 8));
break;
case stellar::InternalLedgerEntryType::SPONSORSHIP_COUNTER:
res = shortHash::computeHash(stellar::ByteSlice(
sponsorshipCounterKey().sponsoringID.ed25519().data(), 8));
break;
default:
abort();
}
hashMix(res, static_cast<size_t>(type()));
mHash = res;
return res;
}

void
InternalLedgerKey::assign(InternalLedgerKey const& glk)
{
releaseAssert(glk.type() == mType);
mHash = glk.mHash;
switch (mType)
{
case InternalLedgerEntryType::LEDGER_ENTRY:
ledgerKey() = glk.ledgerKey();
ledgerKeyRef() = glk.ledgerKey();
break;
case InternalLedgerEntryType::SPONSORSHIP:
sponsorshipKey() = glk.sponsorshipKey();
sponsorshipKeyRef() = glk.sponsorshipKey();
break;
case InternalLedgerEntryType::SPONSORSHIP_COUNTER:
sponsorshipCounterKey() = glk.sponsorshipCounterKey();
sponsorshipCounterKeyRef() = glk.sponsorshipCounterKey();
break;
default:
abort();
Expand All @@ -155,16 +202,17 @@ void
InternalLedgerKey::assign(InternalLedgerKey&& glk)
{
releaseAssert(glk.type() == mType);
mHash = glk.mHash;
switch (mType)
{
case InternalLedgerEntryType::LEDGER_ENTRY:
ledgerKey() = std::move(glk.ledgerKey());
ledgerKeyRef() = std::move(glk.mLedgerKey);
break;
case InternalLedgerEntryType::SPONSORSHIP:
sponsorshipKey() = std::move(glk.sponsorshipKey());
sponsorshipKeyRef() = std::move(glk.mSponsorshipKey);
break;
case InternalLedgerEntryType::SPONSORSHIP_COUNTER:
sponsorshipCounterKey() = std::move(glk.sponsorshipCounterKey());
sponsorshipCounterKeyRef() = std::move(glk.mSponsorshipCounterKey);
break;
default:
abort();
Expand All @@ -188,6 +236,7 @@ InternalLedgerKey::construct()
default:
abort();
}
mHash = 0;
}

void
Expand All @@ -207,6 +256,7 @@ InternalLedgerKey::destruct()
default:
abort();
}
mHash = 0;
}

void
Expand Down Expand Up @@ -236,9 +286,10 @@ InternalLedgerKey::checkDiscriminant(InternalLedgerEntryType expected) const
}

LedgerKey&
InternalLedgerKey::ledgerKey()
InternalLedgerKey::ledgerKeyRef()
{
checkDiscriminant(InternalLedgerEntryType::LEDGER_ENTRY);
mHash = 0;
return mLedgerKey;
}

Expand All @@ -250,9 +301,10 @@ InternalLedgerKey::ledgerKey() const
}

SponsorshipKey&
InternalLedgerKey::sponsorshipKey()
InternalLedgerKey::sponsorshipKeyRef()
{
checkDiscriminant(InternalLedgerEntryType::SPONSORSHIP);
mHash = 0;
return mSponsorshipKey;
}

Expand All @@ -264,9 +316,10 @@ InternalLedgerKey::sponsorshipKey() const
}

SponsorshipCounterKey&
InternalLedgerKey::sponsorshipCounterKey()
InternalLedgerKey::sponsorshipCounterKeyRef()
{
checkDiscriminant(InternalLedgerEntryType::SPONSORSHIP_COUNTER);
mHash = 0;
return mSponsorshipCounterKey;
}

Expand Down Expand Up @@ -301,7 +354,7 @@ InternalLedgerKey::toString() const
bool
operator==(InternalLedgerKey const& lhs, InternalLedgerKey const& rhs)
{
if (lhs.type() != rhs.type())
if (lhs.hash() != rhs.hash() || lhs.type() != rhs.type())
{
return false;
}
Expand Down Expand Up @@ -369,6 +422,11 @@ InternalLedgerEntry::InternalLedgerEntry(InternalLedgerEntry&& gle)
InternalLedgerEntry&
InternalLedgerEntry::operator=(InternalLedgerEntry const& gle)
{
if (this == &gle)
{
return *this;
}

type(gle.type());
assign(gle);
return *this;
Expand Down
16 changes: 12 additions & 4 deletions src/ledger/InternalLedgerEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct SponsorshipCounterKey
class InternalLedgerKey
{
private:
size_t mutable mHash;
InternalLedgerEntryType mType;
union
{
Expand All @@ -45,7 +46,16 @@ class InternalLedgerKey

void checkDiscriminant(InternalLedgerEntryType expected) const;

void type(InternalLedgerEntryType t);
LedgerKey& ledgerKeyRef();
SponsorshipKey& sponsorshipKeyRef();
SponsorshipCounterKey& sponsorshipCounterKeyRef();

public:
static InternalLedgerKey makeSponsorshipKey(AccountID const& sponsoredId);
static InternalLedgerKey
makeSponsorshipCounterKey(AccountID const& sponsoringId);

InternalLedgerKey();
explicit InternalLedgerKey(InternalLedgerEntryType t);

Expand All @@ -61,19 +71,17 @@ class InternalLedgerKey

~InternalLedgerKey();

void type(InternalLedgerEntryType t);
InternalLedgerEntryType type() const;

LedgerKey& ledgerKey();
LedgerKey const& ledgerKey() const;

SponsorshipKey& sponsorshipKey();
SponsorshipKey const& sponsorshipKey() const;

SponsorshipCounterKey& sponsorshipCounterKey();
SponsorshipCounterKey const& sponsorshipCounterKey() const;

std::string toString() const;

size_t hash() const;
};

struct SponsorshipEntry
Expand Down
14 changes: 1 addition & 13 deletions src/ledger/LedgerHashUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,19 +160,7 @@ template <> class hash<stellar::InternalLedgerKey>
size_t
operator()(stellar::InternalLedgerKey const& glk) const
{
switch (glk.type())
{
case stellar::InternalLedgerEntryType::LEDGER_ENTRY:
return hash<stellar::LedgerKey>()(glk.ledgerKey());
case stellar::InternalLedgerEntryType::SPONSORSHIP:
return stellar::shortHash::computeHash(stellar::ByteSlice(
glk.sponsorshipKey().sponsoredID.ed25519().data(), 8));
case stellar::InternalLedgerEntryType::SPONSORSHIP_COUNTER:
return stellar::shortHash::computeHash(stellar::ByteSlice(
glk.sponsorshipCounterKey().sponsoringID.ed25519().data(), 8));
default:
abort();
}
return glk.hash();
}
};
}
Loading

0 comments on commit dc5f5a3

Please sign in to comment.