@@ -207,19 +207,30 @@ struct eosvmoc_tier {
207207 return it != wasm_instantiation_cache.end ();
208208 }
209209
210- void code_block_num_last_used (const digest_type& code_hash, const uint8_t & vm_type, const uint8_t & vm_version, const uint32_t & block_num) {
210+ void code_block_num_last_used (const digest_type& code_hash, uint8_t vm_type, uint8_t vm_version,
211+ block_num_type first_used_block_num, block_num_type block_num_last_used)
212+ {
211213 // The caller of this method apply_eosio_setcode has asserted that
212214 // the transaction is not read-only, implying we are
213215 // in write window. Read-only threads are not running.
214216 // Safe to update the cache without locking.
215217 wasm_cache_index::iterator it = wasm_instantiation_cache.find (boost::make_tuple (code_hash, vm_type, vm_version));
216- if (it != wasm_instantiation_cache.end ())
217- wasm_instantiation_cache.modify (it, [block_num](wasm_cache_entry& e) {
218- e.last_block_num_used = block_num;
219- });
218+ if (it != wasm_instantiation_cache.end ()) {
219+ if (first_used_block_num == block_num_last_used) {
220+ // First used and no longer needed in the same block, erase immediately, do not wait for LIB.
221+ // Since created and destroyed in the same block, likely will not be needed in a forked block.
222+ // Prevents many setcodes in the same block using up space in the cache.
223+ wasm_instantiation_cache.erase (it);
224+ } else {
225+ wasm_instantiation_cache.modify (it, [block_num_last_used](wasm_cache_entry& e) {
226+ e.last_block_num_used = block_num_last_used;
227+ });
228+ }
229+ }
220230
221231#ifdef EOSIO_EOS_VM_OC_RUNTIME_ENABLED
222- if (eosvmoc)
232+ // see comment above
233+ if (first_used_block_num == block_num_last_used && eosvmoc)
223234 eosvmoc->cc .free_code (code_hash, vm_version);
224235#endif
225236 }
0 commit comments