From 0679a16ecda8113dac1853fccaba1896847f7b09 Mon Sep 17 00:00:00 2001 From: Jose Ricardo Ziviani Date: Mon, 25 Feb 2019 18:11:22 -0300 Subject: [PATCH] Fix problem in run_insert Signed-off-by: Jose Ricardo Ziviani --- include/compiler.h | 2 ++ src/compiler.cpp | 22 +++++++++++++++++++--- test/code.insert.cycle1 | 4 ---- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/include/compiler.h b/include/compiler.h index d41e1e5..90232c1 100644 --- a/include/compiler.h +++ b/include/compiler.h @@ -31,6 +31,7 @@ namespace amps class compiler { bool running_cache_; + bool update_main_cache_; std::string result_; error &error_; size_t current_cache_; @@ -74,6 +75,7 @@ namespace amps public: compiler(error &err); std::string generate(metainfo &metainfo, const user_map &usermap); + void reset(); template void set_callback(F&& callback) diff --git a/src/compiler.cpp b/src/compiler.cpp index bbf58f9..f0eb425 100644 --- a/src/compiler.cpp +++ b/src/compiler.cpp @@ -11,6 +11,7 @@ namespace amps { compiler::compiler(error &err) : running_cache_(false), + update_main_cache_(false), error_(err), current_cache_(0) { @@ -27,6 +28,8 @@ namespace amps cache_[metainfo.hash()] = block_cache{counter - 1, metainfo.size() - 1, 0, 1}; + update_main_cache_ = true; + // program main loop for (; counter < metainfo.size(); context_.jump_to(counter + 1)) { @@ -527,20 +530,33 @@ namespace amps return false; } + size_t counter = context_.get_counter(); scan insert_scan(error_); insert_scan.do_scan(read_full(filename)); metainfo &new_info = insert_scan.get_metainfo(); auto cache_it = cache_.find(new_info.hash()); + for (auto &kv : cache_) { + if (counter >= kv.second.start && + counter <= kv.second.end) { + kv.second.end = counter; + } + } + // the block inserted isn't cached: cache it, put the content in // the current program and execute it if (cache_it == cache_.end()) { - size_t counter = context_.get_counter(); + if (update_main_cache_) { + cache_[info.hash()].end = counter; + update_main_cache_ = false; + } + auto diff = static_cast(counter); cache_[new_info.hash()] = block_cache{counter - 1, new_info.size() + counter, 0, 1}; + size_t new_size = new_info.size(); copy(info.begin() + diff + 1, info.end(), back_inserter(new_info)); info.resize(info.size() + new_size); @@ -553,7 +569,7 @@ namespace amps // the block is cached: execute it else { // remove the "insert" code... - info.remove(context_.get_counter()); + info.remove(counter); if (++cache_it->second.iterations > MAX_ITERATION) { error_.critical(filename, " has run for more than ", @@ -563,7 +579,7 @@ namespace amps } // ...and execute the cached code - cache_it->second.resume = context_.get_counter() - 1; + cache_it->second.resume = counter - 1; context_.jump_to(cache_it->second.start); running_cache_ = true; diff --git a/test/code.insert.cycle1 b/test/code.insert.cycle1 index 2cf8604..7d79bd0 100644 --- a/test/code.insert.cycle1 +++ b/test/code.insert.cycle1 @@ -3,11 +3,7 @@ this is the test line 2 from recursive 1 this is the test line 1 this is the test line 2 -from recursive 1 -from recursive 2 -this is the test line 3 from recursive 2 this is the test line 3 this is the test line 4 from recursive 1 -from recursive 2