Browse Source

Trie maps consolidated for speed.

cl-refactor
Gav Wood 10 years ago
parent
commit
1b1c2e95d1
  1. 2
      libdevcore/Common.cpp
  2. 52
      libdevcrypto/MemoryDB.cpp
  3. 7
      libdevcrypto/MemoryDB.h
  4. 14
      libdevcrypto/OverlayDB.cpp
  5. 8
      libdevcrypto/TrieDB.h
  6. 2
      libethcore/Common.cpp

2
libdevcore/Common.cpp

@ -27,7 +27,7 @@ using namespace dev;
namespace dev
{
char const* Version = "0.9.13";
char const* Version = "0.9.14";
}

52
libdevcrypto/MemoryDB.cpp

@ -32,22 +32,20 @@ const char* DBWarn::name() { return "TDB"; }
std::map<h256, std::string> MemoryDB::get() const
{
if (!m_enforceRefs)
return m_over;
std::map<h256, std::string> ret;
for (auto const& i: m_refCount)
if (i.second)
ret.insert(*m_over.find(i.first));
for (auto const& i: m_main)
if (!m_enforceRefs || i.second.second > 0)
ret.insert(make_pair(i.first, i.second.first));
return ret;
}
std::string MemoryDB::lookup(h256 const& _h) const
{
auto it = m_over.find(_h);
if (it != m_over.end())
auto it = m_main.find(_h);
if (it != m_main.end())
{
if (!m_enforceRefs || (m_refCount.count(it->first) && m_refCount.at(it->first)))
return it->second;
if (!m_enforceRefs || it->second.second > 0)
return it->second.first;
// else if (m_enforceRefs && m_refCount.count(it->first) && !m_refCount.at(it->first))
// cnote << "Lookup required for value with no refs. Let's hope it's in the DB." << _h;
}
@ -56,27 +54,33 @@ std::string MemoryDB::lookup(h256 const& _h) const
bool MemoryDB::exists(h256 const& _h) const
{
auto it = m_over.find(_h);
if (it != m_over.end() && (!m_enforceRefs || (m_refCount.count(it->first) && m_refCount.at(it->first))))
auto it = m_main.find(_h);
if (it != m_main.end() && (!m_enforceRefs || it->second.second > 0))
return true;
return false;
}
void MemoryDB::insert(h256 const& _h, bytesConstRef _v)
{
m_over[_h] = _v.toString();
m_refCount[_h]++;
auto it = m_main.find(_h);
if (it != m_main.end())
{
it->second.first = _v.toString();
it->second.second++;
}
else
m_main[_h] = make_pair(_v.toString(), 1);
#if ETH_PARANOIA
dbdebug << "INST" << _h << "=>" << m_refCount[_h];
dbdebug << "INST" << _h << "=>" << m_main[_h].second;
#endif
}
bool MemoryDB::kill(h256 const& _h)
{
if (m_refCount.count(_h))
if (m_main.count(_h))
{
if (m_refCount[_h] > 0)
--m_refCount[_h];
if (m_main[_h].second > 0)
m_main[_h].second--;
#if ETH_PARANOIA
else
{
@ -85,7 +89,7 @@ bool MemoryDB::kill(h256 const& _h)
dbdebug << "NOKILL-WAS" << _h;
return false;
}
dbdebug << "KILL" << _h << "=>" << m_refCount[_h];
dbdebug << "KILL" << _h << "=>" << m_main[_h].second;
return true;
}
else
@ -101,16 +105,18 @@ bool MemoryDB::kill(h256 const& _h)
void MemoryDB::purge()
{
for (auto const& i: m_refCount)
if (!i.second)
m_over.erase(i.first);
for (auto it = m_main.begin(); it != m_main.end(); )
if (it->second.second)
++it;
else
it = m_main.erase(it);
}
set<h256> MemoryDB::keys() const
{
set<h256> ret;
for (auto const& i: m_refCount)
if (i.second && h128(i.first.ref().cropped(0, 16)))
for (auto const& i: m_main)
if (i.second.second)
ret.insert(i.first);
return ret;
}

7
libdevcrypto/MemoryDB.h

@ -44,7 +44,7 @@ class MemoryDB
public:
MemoryDB() {}
void clear() { m_over.clear(); }
void clear() { m_main.clear(); } // WARNING !!!! didn't originally clear m_refCount!!!
std::map<h256, std::string> get() const;
std::string lookup(h256 const& _h) const;
@ -60,8 +60,7 @@ public:
std::set<h256> keys() const;
protected:
std::map<h256, std::string> m_over;
std::map<h256, unsigned> m_refCount;
std::map<h256, std::pair<std::string, unsigned>> m_main;
std::map<h256, std::pair<bytes, bool>> m_aux;
mutable bool m_enforceRefs = false;
@ -80,7 +79,7 @@ private:
inline std::ostream& operator<<(std::ostream& _out, MemoryDB const& _m)
{
for (auto i: _m.get())
for (auto const& i: _m.get())
{
_out << i.first << ": ";
_out << RLP(i.second);

14
libdevcrypto/OverlayDB.cpp

@ -41,11 +41,11 @@ void OverlayDB::commit()
{
ldb::WriteBatch batch;
// cnote << "Committing nodes to disk DB:";
for (auto const& i: m_over)
for (auto const& i: m_main)
{
// cnote << i.first << "#" << m_refCount[i.first];
if (m_refCount[i.first])
batch.Put(ldb::Slice((char const*)i.first.data(), i.first.size), ldb::Slice(i.second.data(), i.second.size()));
// cnote << i.first << "#" << m_main[i.first].second;
if (i.second.second)
batch.Put(ldb::Slice((char const*)i.first.data(), i.first.size), ldb::Slice(i.second.first.data(), i.second.first.size()));
}
for (auto const& i: m_aux)
if (i.second.second)
@ -56,8 +56,7 @@ void OverlayDB::commit()
}
m_db->Write(m_writeOptions, &batch);
m_aux.clear();
m_over.clear();
m_refCount.clear();
m_main.clear();
}
}
@ -77,8 +76,7 @@ bytes OverlayDB::lookupAux(h256 _h) const
void OverlayDB::rollback()
{
m_over.clear();
m_refCount.clear();
m_main.clear();
}
std::string OverlayDB::lookup(h256 _h) const

8
libdevcrypto/TrieDB.h

@ -861,8 +861,8 @@ template <class DB> bytes GenericTrieDB<DB>::mergeAt(RLP const& _orig, NibbleSli
template <class DB> void GenericTrieDB<DB>::mergeAtAux(RLPStream& _out, RLP const& _orig, NibbleSlice _k, bytesConstRef _v)
{
#if ETH_PARANOIA
tdebug << "mergeAtAux " << _orig << _k << sha3(_orig.data()) << ((_orig.isData() && _orig.size() <= 32) ? _orig.toHash<h256>() : std::string());
#if ETH_PARANOIA || !ETH_TRUE
tdebug << "mergeAtAux " << _orig << _k << sha3(_orig.data()) << ((_orig.isData() && _orig.size() <= 32) ? _orig.toHash<h256>().abridged() : std::string());
#endif
RLP r = _orig;
@ -1016,8 +1016,8 @@ template <class DB> bytes GenericTrieDB<DB>::deleteAt(RLP const& _orig, NibbleSl
template <class DB> bool GenericTrieDB<DB>::deleteAtAux(RLPStream& _out, RLP const& _orig, NibbleSlice _k)
{
#if ETH_PARANOIA
tdebug << "deleteAtAux " << _orig << _k << sha3(_orig.data()) << ((_orig.isData() && _orig.size() <= 32) ? _orig.toHash<h256>() : std::string());
#if ETH_PARANOIA || !ETH_TRUE
tdebug << "deleteAtAux " << _orig << _k << sha3(_orig.data()) << ((_orig.isData() && _orig.size() <= 32) ? _orig.toHash<h256>().abridged() : std::string());
#endif
bytes b = _orig.isEmpty() ? bytes() : deleteAt(_orig.isList() ? _orig : RLP(node(_orig.toHash<h256>())), _k);

2
libethcore/Common.cpp

@ -36,7 +36,7 @@ namespace eth
{
const unsigned c_protocolVersion = 60;
const unsigned c_minorProtocolVersion = 1;
const unsigned c_minorProtocolVersion = 2;
const unsigned c_databaseBaseVersion = 9;
#if ETH_FATDB
const unsigned c_databaseVersionModifier = 1;

Loading…
Cancel
Save