@@ -79,58 +79,51 @@ namespace kagome::runtime {
79
79
80
80
if (!pool_opt) {
81
81
lock.unlock ();
82
- if (auto future = getFutureCompiledModule (code_hash)) {
83
- lock.lock ();
84
- pool_opt = pools_.get (code_hash);
85
- } else {
86
- OUTCOME_TRY (module, tryCompileModule (code_hash, code_zstd));
87
- BOOST_ASSERT (module != nullptr );
88
- lock.lock ();
82
+ OUTCOME_TRY (module, tryCompileModule (code_hash, code_zstd));
83
+ lock.lock ();
84
+ pool_opt = pools_.get (code_hash);
85
+ if (!pool_opt) {
89
86
pool_opt = std::ref (pools_.put (code_hash, InstancePool{module, {}}));
90
87
}
91
88
}
89
+ BOOST_ASSERT (pool_opt);
92
90
auto instance = pool_opt->get ().instantiate (lock);
93
91
return std::make_shared<BorrowedInstance>(
94
92
weak_from_this (), code_hash, std::move (instance));
95
93
}
96
94
97
- std::optional<std::shared_future<RuntimeInstancesPool::CompilationResult>>
98
- RuntimeInstancesPool::getFutureCompiledModule (
99
- const CodeHash &code_hash) const {
100
- std::unique_lock l{compiling_modules_mtx_};
101
- auto iter = compiling_modules_.find (code_hash);
102
- if (iter == compiling_modules_.end ()) {
103
- return std::nullopt;
104
- }
105
- auto future = iter->second ;
106
- l.unlock ();
107
- return future;
108
- }
109
-
110
95
RuntimeInstancesPool::CompilationResult
111
96
RuntimeInstancesPool::tryCompileModule (const CodeHash &code_hash,
112
97
common::BufferView code_zstd) {
113
98
std::unique_lock l{compiling_modules_mtx_};
99
+ if (auto iter = compiling_modules_.find (code_hash);
100
+ iter != compiling_modules_.end ()) {
101
+ std::shared_future<CompilationResult> future = iter->second ;
102
+ l.unlock ();
103
+ return future.get ();
104
+ }
114
105
std::promise<CompilationResult> promise;
115
- auto [iter, inserted ] =
106
+ auto [iter, is_inserted ] =
116
107
compiling_modules_.insert ({code_hash, promise.get_future ()});
117
- BOOST_ASSERT (inserted);
108
+ BOOST_ASSERT (is_inserted);
109
+ BOOST_ASSERT (iter != compiling_modules_.end ());
118
110
l.unlock ();
119
111
120
112
common::Buffer code;
121
- CompilationResult res{ nullptr } ;
113
+ std::optional< CompilationResult> res;
122
114
if (!uncompressCodeIfNeeded (code_zstd, code)) {
123
115
res = CompilationError{" Failed to uncompress code" };
124
116
} else {
125
117
res = common::map_result (module_factory_->make (code), [](auto &&module) {
126
118
return std::shared_ptr<const Module>(module);
127
119
});
128
120
}
129
- promise. set_value (res);
121
+ BOOST_ASSERT (res);
130
122
131
123
l.lock ();
132
124
compiling_modules_.erase (iter);
133
- return res;
125
+ promise.set_value (*res);
126
+ return *res;
134
127
}
135
128
136
129
outcome::result<std::shared_ptr<ModuleInstance>>
0 commit comments