Browse Source

Trying to cache compiled contracts with pointer to code as a key (it's not a good idea)

cl-refactor
Paweł Bylica 10 years ago
parent
commit
7c9cf6e5e7
  1. 37
      libevmjit/Cache.cpp
  2. 7
      libevmjit/Cache.h
  3. 9
      libevmjit/ExecutionEngine.cpp
  4. 2
      libevmjit/ExecutionEngine.h

37
libevmjit/Cache.cpp

@ -1,4 +1,7 @@
#include "Cache.h"
#include <unordered_map>
#include <cassert>
#include <iostream>
namespace dev
{
@ -6,6 +9,40 @@ namespace eth
{
namespace jit
{
namespace
{
using CacheMap = std::unordered_map<Cache::Key, ExecBundle>;
CacheMap& getCacheMap()
{
static CacheMap map;
return map;
}
}
#define LOG(...) std::cerr << "CACHE "
ExecBundle& Cache::registerExec(Cache::Key _key, ExecBundle&& _exec)
{
auto& map = getCacheMap();
auto r = map.insert(std::make_pair(_key, std::move(_exec)));
assert(r.second && "Updating cached objects not supported");
LOG() << "add " << _key << "\n";
return r.first->second; // return exec, now owned by cache
}
ExecBundle* Cache::findExec(Cache::Key _key)
{
auto& map = getCacheMap();
auto it = map.find(_key);
if (it != map.end())
{
LOG() << "hit " << _key << "\n";
return &it->second;
}
LOG() << "miss " << _key << "\n";
return nullptr;
}
}
}

7
libevmjit/Cache.h

@ -1,5 +1,9 @@
#pragma once
#include <memory>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
namespace dev
{
namespace eth
@ -17,7 +21,10 @@ struct ExecBundle
class Cache
{
public:
using Key = void const*;
static ExecBundle& registerExec(Key _key, ExecBundle&& _exec);
static ExecBundle* findExec(Key _key);
};
}

9
libevmjit/ExecutionEngine.cpp

@ -41,6 +41,12 @@ ReturnCode ExecutionEngine::run(bytes const& _code, RuntimeData* _data, Env* _en
ReturnCode ExecutionEngine::run(std::unique_ptr<llvm::Module> _module, RuntimeData* _data, Env* _env)
{
auto key = _data->code; // TODO: Change cache key
if (auto cachedExec = Cache::findExec(key))
{
return run(*cachedExec, _data, _env);
}
auto module = _module.get(); // Keep ownership of the module in _module
llvm::sys::PrintStackTraceOnErrorSignal();
@ -88,7 +94,8 @@ ReturnCode ExecutionEngine::run(std::unique_ptr<llvm::Module> _module, RuntimeDa
if (!exec.entryFunc)
return ReturnCode::LLVMLinkError;
auto returnCode = run(exec, _data, _env);
auto& cachedExec = Cache::registerExec(_data->code, std::move(exec));
auto returnCode = run(cachedExec, _data, _env);
auto executionEndTime = std::chrono::high_resolution_clock::now();
clog(JIT) << " + " << std::chrono::duration_cast<std::chrono::milliseconds>(executionEndTime - executionStartTime).count() << " ms ";

2
libevmjit/ExecutionEngine.h

@ -13,7 +13,7 @@ namespace eth
{
namespace jit
{
class ExecBundle;
struct ExecBundle;
class ExecutionEngine
{

Loading…
Cancel
Save