Browse Source

replaces cache maps with hash tables, reverted noteCanonChanged

cl-refactor
arkpar 10 years ago
parent
commit
7387a1c250
  1. 3
      libdevcore/FixedHash.h
  2. 8
      libdevcore/Guards.h
  3. 13
      libethereum/BlockChain.cpp
  4. 18
      libethereum/BlockChain.h
  5. 13
      libethereum/BlockDetails.h

3
libdevcore/FixedHash.h

@ -64,6 +64,9 @@ public:
/// Convert from the corresponding arithmetic type. /// Convert from the corresponding arithmetic type.
FixedHash(Arith const& _arith) { toBigEndian(_arith, m_data); } FixedHash(Arith const& _arith) { toBigEndian(_arith, m_data); }
/// Convert from unsigned
explicit FixedHash(unsigned _u) { toBigEndian(_u, m_data); }
/// Explicitly construct, copying from a byte array. /// Explicitly construct, copying from a byte array.
explicit FixedHash(bytes const& _b, ConstructFromHashType _t = FailIfDifferent) { if (_b.size() == N) memcpy(m_data.data(), _b.data(), std::min<unsigned>(_b.size(), N)); else { m_data.fill(0); if (_t != FailIfDifferent) { auto c = std::min<unsigned>(_b.size(), N); for (unsigned i = 0; i < c; ++i) m_data[_t == AlignRight ? N - 1 - i : i] = _b[_t == AlignRight ? _b.size() - 1 - i : i]; } } } explicit FixedHash(bytes const& _b, ConstructFromHashType _t = FailIfDifferent) { if (_b.size() == N) memcpy(m_data.data(), _b.data(), std::min<unsigned>(_b.size(), N)); else { m_data.fill(0); if (_t != FailIfDifferent) { auto c = std::min<unsigned>(_b.size(), N); for (unsigned i = 0; i < c; ++i) m_data[_t == AlignRight ? N - 1 - i : i] = _b[_t == AlignRight ? _b.size() - 1 - i : i]; } } }

8
libdevcore/Guards.h

@ -66,11 +66,11 @@ struct GenericUnguardSharedBool
class SpinLock class SpinLock
{ {
public: public:
SpinLock() { lck.clear(); } SpinLock() { m_lock.clear(); }
void lock() { while (lck.test_and_set(std::memory_order_acquire)) {} } void lock() { while (m_lock.test_and_set(std::memory_order_acquire)) {} }
void unlock() { lck.clear(std::memory_order_release); } void unlock() { m_lock.clear(std::memory_order_release); }
private: private:
std::atomic_flag lck; std::atomic_flag m_lock;
}; };
using SpinGuard = std::lock_guard<SpinLock>; using SpinGuard = std::lock_guard<SpinLock>;

13
libethereum/BlockChain.cpp

@ -293,13 +293,9 @@ LastHashes BlockChain::lastHashes(unsigned _n) const
Guard l(x_lastLastHashes); Guard l(x_lastLastHashes);
if (m_lastLastHashesNumber != _n || m_lastLastHashes.empty()) if (m_lastLastHashesNumber != _n || m_lastLastHashes.empty())
{ {
LastHashes lastHashes(256); m_lastLastHashes.resize(256);
for (unsigned i = 0; i < 256; ++i) for (unsigned i = 0; i < 256; ++i)
{ m_lastLastHashes[i] = _n >= i ? numberHash(_n - i) : h256();
size_t prevIndex = m_lastLastHashesNumber - _n + i;
lastHashes[i] = (prevIndex < m_lastLastHashes.size()) ? m_lastLastHashes[prevIndex] : (_n >= i ? numberHash(_n - i) : h256());
}
m_lastLastHashes = std::move(lastHashes);
m_lastLastHashesNumber = _n; m_lastLastHashesNumber = _n;
} }
return m_lastLastHashes; return m_lastLastHashes;
@ -613,6 +609,7 @@ ImportRoute BlockChain::import(bytes const& _block, OverlayDB const& _db, Import
} }
clog(BlockChainNote) << " Imported and best" << td << " (#" << bi.number << "). Has" << (details(bi.parentHash).children.size() - 1) << "siblings. Route:" << route; clog(BlockChainNote) << " Imported and best" << td << " (#" << bi.number << "). Has" << (details(bi.parentHash).children.size() - 1) << "siblings. Route:" << route;
noteCanonChanged();
StructuredLogger::chainNewHead( StructuredLogger::chainNewHead(
bi.headerHash(WithoutNonce).abridged(), bi.headerHash(WithoutNonce).abridged(),
@ -770,7 +767,7 @@ void BlockChain::noteUsed(h256 const& _h, unsigned _extra) const
m_inUse.insert(id); m_inUse.insert(id);
} }
template <class T> static unsigned getHashSize(map<h256, T> const& _map) template <class T> static unsigned getHashSize(unordered_map<h256, T> const& _map)
{ {
unsigned ret = 0; unsigned ret = 0;
for (auto const& i: _map) for (auto const& i: _map)
@ -858,7 +855,7 @@ void BlockChain::garbageCollect(bool _force)
} }
} }
m_cacheUsage.pop_back(); m_cacheUsage.pop_back();
m_cacheUsage.push_front(std::set<CacheID>{}); m_cacheUsage.push_front(std::unordered_set<CacheID>{});
} }
void BlockChain::checkConsistency() void BlockChain::checkConsistency()

18
libethereum/BlockChain.h

@ -28,6 +28,8 @@
#include <deque> #include <deque>
#include <chrono> #include <chrono>
#include <unordered_map>
#include <unordered_set>
#include <libdevcore/Log.h> #include <libdevcore/Log.h>
#include <libdevcore/Exceptions.h> #include <libdevcore/Exceptions.h>
#include <libdevcore/Guards.h> #include <libdevcore/Guards.h>
@ -40,6 +42,11 @@
#include "BlockQueue.h" #include "BlockQueue.h"
namespace ldb = leveldb; namespace ldb = leveldb;
template <> struct std::hash<std::pair<dev::h256, unsigned>>
{
size_t operator()(const pair<dev::h256, unsigned> &x ) const { return std::hash<dev::h256>()(x.first) ^ std::hash<unsigned>()(x.second); }
};
namespace dev namespace dev
{ {
@ -66,7 +73,7 @@ std::map<Address, Account> const& genesisState();
ldb::Slice toSlice(h256 const& _h, unsigned _sub = 0); ldb::Slice toSlice(h256 const& _h, unsigned _sub = 0);
using BlocksHash = std::map<h256, bytes>; using BlocksHash = std::unordered_map<h256, bytes>;
using TransactionHashes = h256s; using TransactionHashes = h256s;
using UncleHashes = h256s; using UncleHashes = h256s;
using ImportRoute = std::pair<h256s, h256s>; using ImportRoute = std::pair<h256s, h256s>;
@ -144,7 +151,7 @@ public:
UncleHashes uncleHashes() const { return uncleHashes(currentHash()); } UncleHashes uncleHashes() const { return uncleHashes(currentHash()); }
/// Get the hash for a given block's number. /// Get the hash for a given block's number.
h256 numberHash(unsigned _i) const { if (!_i) return genesisHash(); return queryExtras<BlockHash, ExtraBlockHash>(h256(u256(_i)), m_blockHashes, x_blockHashes, NullBlockHash).value; } h256 numberHash(unsigned _i) const { if (!_i) return genesisHash(); return queryExtras<BlockHash, ExtraBlockHash>(h256(_i), m_blockHashes, x_blockHashes, NullBlockHash).value; }
/// Get the last N hashes for a given block. (N is determined by the LastHashes type.) /// Get the last N hashes for a given block. (N is determined by the LastHashes type.)
LastHashes lastHashes() const { return lastHashes(number()); } LastHashes lastHashes() const { return lastHashes(number()); }
@ -251,7 +258,7 @@ private:
void open(std::string const& _path, WithExisting _we = WithExisting::Trust); void open(std::string const& _path, WithExisting _we = WithExisting::Trust);
void close(); void close();
template<class T, unsigned N> T queryExtras(h256 const& _h, std::map<h256, T>& _m, boost::shared_mutex& _x, T const& _n, ldb::DB* _extrasDB = nullptr) const template<class T, unsigned N> T queryExtras(h256 const& _h, std::unordered_map<h256, T>& _m, boost::shared_mutex& _x, T const& _n, ldb::DB* _extrasDB = nullptr) const
{ {
{ {
ReadGuard l(_x); ReadGuard l(_x);
@ -295,11 +302,12 @@ private:
using CacheID = std::pair<h256, unsigned>; using CacheID = std::pair<h256, unsigned>;
mutable Mutex x_cacheUsage; mutable Mutex x_cacheUsage;
mutable std::deque<std::set<CacheID>> m_cacheUsage; mutable std::deque<std::unordered_set<CacheID>> m_cacheUsage;
mutable std::set<CacheID> m_inUse; mutable std::unordered_set<CacheID> m_inUse;
void noteUsed(h256 const& _h, unsigned _extra = (unsigned)-1) const; void noteUsed(h256 const& _h, unsigned _extra = (unsigned)-1) const;
std::chrono::system_clock::time_point m_lastCollection; std::chrono::system_clock::time_point m_lastCollection;
void noteCanonChanged() const { Guard l(x_lastLastHashes); m_lastLastHashes.clear(); }
mutable Mutex x_lastLastHashes; mutable Mutex x_lastLastHashes;
mutable LastHashes m_lastLastHashes; mutable LastHashes m_lastLastHashes;
mutable unsigned m_lastLastHashesNumber = (unsigned)-1; mutable unsigned m_lastLastHashesNumber = (unsigned)-1;

13
libethereum/BlockDetails.h

@ -21,6 +21,7 @@
#pragma once #pragma once
#include <unordered_map>
#pragma warning(push) #pragma warning(push)
#pragma warning(disable: 4100 4267) #pragma warning(disable: 4100 4267)
#include <leveldb/db.h> #include <leveldb/db.h>
@ -114,12 +115,12 @@ struct TransactionAddress
static const unsigned size = 67; static const unsigned size = 67;
}; };
using BlockDetailsHash = std::map<h256, BlockDetails>; using BlockDetailsHash = std::unordered_map<h256, BlockDetails>;
using BlockLogBloomsHash = std::map<h256, BlockLogBlooms>; using BlockLogBloomsHash = std::unordered_map<h256, BlockLogBlooms>;
using BlockReceiptsHash = std::map<h256, BlockReceipts>; using BlockReceiptsHash = std::unordered_map<h256, BlockReceipts>;
using TransactionAddressHash = std::map<h256, TransactionAddress>; using TransactionAddressHash = std::unordered_map<h256, TransactionAddress>;
using BlockHashHash = std::map<h256, BlockHash>; using BlockHashHash = std::unordered_map<h256, BlockHash>;
using BlocksBloomsHash = std::map<h256, BlocksBlooms>; using BlocksBloomsHash = std::unordered_map<h256, BlocksBlooms>;
static const BlockDetails NullBlockDetails; static const BlockDetails NullBlockDetails;
static const BlockLogBlooms NullBlockLogBlooms; static const BlockLogBlooms NullBlockLogBlooms;

Loading…
Cancel
Save