Browse Source

Avoid more maps, more lookups and SHA3.

cl-refactor
Gav Wood 10 years ago
parent
commit
83f57b9ed0
  1. 8
      libdevcrypto/MemoryDB.cpp
  2. 19
      libdevcrypto/MemoryDB.h
  3. 14
      libdevcrypto/OverlayDB.cpp
  4. 2
      libethereum/BlockChain.cpp

8
libdevcrypto/MemoryDB.cpp

@ -41,7 +41,7 @@ std::map<h256, std::string> MemoryDB::get() const
return ret;
}
std::string MemoryDB::lookup(h256 _h) const
std::string MemoryDB::lookup(h256 const& _h) const
{
auto it = m_over.find(_h);
if (it != m_over.end())
@ -54,7 +54,7 @@ std::string MemoryDB::lookup(h256 _h) const
return std::string();
}
bool MemoryDB::exists(h256 _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))))
@ -62,7 +62,7 @@ bool MemoryDB::exists(h256 _h) const
return false;
}
void MemoryDB::insert(h256 _h, bytesConstRef _v)
void MemoryDB::insert(h256 const& _h, bytesConstRef _v)
{
m_over[_h] = _v.toString();
m_refCount[_h]++;
@ -71,7 +71,7 @@ void MemoryDB::insert(h256 _h, bytesConstRef _v)
#endif
}
bool MemoryDB::kill(h256 _h)
bool MemoryDB::kill(h256 const& _h)
{
if (m_refCount.count(_h))
{

19
libdevcrypto/MemoryDB.h

@ -47,25 +47,22 @@ public:
void clear() { m_over.clear(); }
std::map<h256, std::string> get() const;
std::string lookup(h256 _h) const;
bool exists(h256 _h) const;
void insert(h256 _h, bytesConstRef _v);
bool kill(h256 _h);
std::string lookup(h256 const& _h) const;
bool exists(h256 const& _h) const;
void insert(h256 const& _h, bytesConstRef _v);
bool kill(h256 const& _h);
void purge();
bytes lookupAux(h256 _h) const { auto h = aux(_h); return m_aux.count(h) ? m_aux.at(h) : bytes(); }
void removeAux(h256 _h) { m_auxActive.erase(aux(_h)); }
void insertAux(h256 _h, bytesConstRef _v) { auto h = aux(_h); m_auxActive.insert(h); m_aux[h] = _v.toBytes(); }
bytes lookupAux(h256 const& _h) const { try { return m_aux.at(_h).first; } catch (...) { return bytes(); } }
void removeAux(h256 const& _h) { m_aux[_h].second = false; }
void insertAux(h256 const& _h, bytesConstRef _v) { m_aux[_h] = make_pair(_v.toBytes(), true); }
std::set<h256> keys() const;
protected:
static h256 aux(h256 _k) { return h256(sha3(_k).ref().cropped(0, 24), h256::AlignLeft); }
std::map<h256, std::string> m_over;
std::map<h256, unsigned> m_refCount;
std::set<h256> m_auxActive;
std::map<h256, bytes> m_aux;
std::map<h256, std::pair<bytes, bool>> m_aux;
mutable bool m_enforceRefs = false;
};

14
libdevcrypto/OverlayDB.cpp

@ -47,14 +47,14 @@ void OverlayDB::commit()
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()));
}
for (auto const& i: m_auxActive)
if (m_aux.count(i))
for (auto const& i: m_aux)
if (i.second.second)
{
batch.Put(i.ref(), bytesConstRef(&m_aux[i]));
m_aux.erase(i);
bytes b = i.first.asBytes();
b.push_back(255); // for aux
batch.Put(bytesConstRef(&b), bytesConstRef(&i.second.first));
}
m_db->Write(m_writeOptions, &batch);
m_auxActive.clear();
m_aux.clear();
m_over.clear();
m_refCount.clear();
@ -67,7 +67,9 @@ bytes OverlayDB::lookupAux(h256 _h) const
if (!ret.empty())
return ret;
std::string v;
m_db->Get(m_readOptions, aux(_h).ref(), &v);
bytes b = _h.asBytes();
b.push_back(255); // for aux
m_db->Get(m_readOptions, bytesConstRef(&b), &v);
if (v.empty())
cwarn << "Aux not found: " << _h;
return asBytes(v);

2
libethereum/BlockChain.cpp

@ -47,7 +47,7 @@ using namespace dev::eth;
namespace js = json_spirit;
#define ETH_CATCH 1
#define ETH_TIMED_IMPORTS 0
#define ETH_TIMED_IMPORTS 1
#ifdef _WIN32
const char* BlockChainDebug::name() { return EthBlue "8" EthWhite " <>"; }

Loading…
Cancel
Save