diff options
author | Vsevolod Stakhov <vsevolod@rspamd.com> | 2022-05-09 21:32:35 +0100 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@rspamd.com> | 2022-05-09 21:32:35 +0100 |
commit | f352f18cfb1448386a9c32a06042f4d94be13b27 (patch) | |
tree | 8de4da9cf96d3f2007de872b76a09da89ac92ae8 /src/libserver/symcache/symcache_runtime.cxx | |
parent | 22f603e83dd4fe3fc6d3e9e18b19bc87d57af235 (diff) | |
download | rspamd-f352f18cfb1448386a9c32a06042f4d94be13b27.tar.gz rspamd-f352f18cfb1448386a9c32a06042f4d94be13b27.zip |
[Rework] Use dynamic items in the callbacks
Diffstat (limited to 'src/libserver/symcache/symcache_runtime.cxx')
-rw-r--r-- | src/libserver/symcache/symcache_runtime.cxx | 67 |
1 files changed, 12 insertions, 55 deletions
diff --git a/src/libserver/symcache/symcache_runtime.cxx b/src/libserver/symcache/symcache_runtime.cxx index 2d941c0ed..b9706c62a 100644 --- a/src/libserver/symcache/symcache_runtime.cxx +++ b/src/libserver/symcache/symcache_runtime.cxx @@ -47,11 +47,6 @@ symcache_runtime::create(struct rspamd_task *task, symcache &cache) -> symcache_ rspamd_mempool_add_destructor(task->task_pool, symcache_runtime::savepoint_dtor, checkpoint); - for (auto &pair: checkpoint->last_id_mappings) { - pair.first = -1; - pair.second = -1; - } - /* Calculate profile probability */ ev_now_update_if_cheap(task->event_loop); ev_tstamp now = ev_now(task->event_loop); @@ -177,7 +172,7 @@ symcache_runtime::disable_symbol(struct rspamd_task *task, const symcache &cache if (item != nullptr) { - auto *dyn_item = get_dynamic_item(item->id, false); + auto *dyn_item = get_dynamic_item(item->id); if (dyn_item) { dyn_item->finished = true; @@ -204,7 +199,7 @@ symcache_runtime::enable_symbol(struct rspamd_task *task, const symcache &cache, if (item != nullptr) { - auto *dyn_item = get_dynamic_item(item->id, false); + auto *dyn_item = get_dynamic_item(item->id); if (dyn_item) { dyn_item->finished = false; @@ -231,7 +226,7 @@ symcache_runtime::is_symbol_checked(const symcache &cache, std::string_view name if (item != nullptr) { - auto *dyn_item = get_dynamic_item(item->id, true); + auto *dyn_item = get_dynamic_item(item->id); if (dyn_item) { return dyn_item->started; @@ -252,7 +247,7 @@ symcache_runtime::is_symbol_enabled(struct rspamd_task *task, const symcache &ca return false; } else { - auto *dyn_item = get_dynamic_item(item->id, true); + auto *dyn_item = get_dynamic_item(item->id); if (dyn_item) { if (dyn_item->started) { @@ -274,53 +269,14 @@ symcache_runtime::is_symbol_enabled(struct rspamd_task *task, const symcache &ca return true; } -auto symcache_runtime::get_dynamic_item(int id, bool save_in_cache) const -> cache_dynamic_item * +auto symcache_runtime::get_dynamic_item(int id) const -> cache_dynamic_item * { - /* Lookup in cache */ - for (const auto &cache_id: last_id_mappings) { - if (cache_id.first == -1) { - break; - } - if (cache_id.first == id) { - auto *dyn_item = &dynamic_items[cache_id.second]; - - return dyn_item; - } - } /* Not found in the cache, do a hash lookup */ auto our_id_maybe = rspamd::find_map(order->by_cache_id, id); if (our_id_maybe) { - auto *dyn_item = &dynamic_items[our_id_maybe.value()]; - - if (!save_in_cache) { - return dyn_item; - } - - /* Insert in the cache, swapping the first item with the last empty item */ - auto first_known = last_id_mappings[0]; - last_id_mappings[0].first = id; - last_id_mappings[0].second = our_id_maybe.value(); - - if (first_known.first != -1) { - /* This loop is guaranteed to finish as we have just inserted one item */ - - constexpr const auto cache_size = sizeof(last_id_mappings) / sizeof(last_id_mappings[0]); - int i = cache_size - 1; - for (;; --i) { - if (last_id_mappings[i].first != -1) { - if (i < cache_size - 1) { - i++; - } - break; - } - } - - last_id_mappings[i] = first_known; - } - - return dyn_item; + return &dynamic_items[our_id_maybe.value()]; } return nullptr; @@ -363,7 +319,7 @@ symcache_runtime::process_pre_postfilters(struct rspamd_task *task, auto compare_functor = +[](int a, int b) { return a < b; }; auto proc_func = [&](cache_item *item) { - auto dyn_item = get_dynamic_item(item->id, true); + auto dyn_item = get_dynamic_item(item->id); if (!dyn_item->started && !dyn_item->finished) { if (has_slow) { @@ -598,7 +554,7 @@ auto symcache_runtime::check_item_deps(struct rspamd_task *task, symcache &cache continue; } - auto *dep_dyn_item = get_dynamic_item(dep.item->id, true); + auto *dep_dyn_item = get_dynamic_item(dep.item->id); if (!dep_dyn_item->finished) { if (!dep_dyn_item->started) { @@ -703,13 +659,14 @@ rspamd_delayed_timer_dtor(gpointer d) } auto -symcache_runtime::finalize_item(struct rspamd_task *task, cache_item *item) -> void +symcache_runtime::finalize_item(struct rspamd_task *task, cache_dynamic_item *dyn_item) -> void { /* Limit to consider a rule as slow (in milliseconds) */ constexpr const gdouble slow_diff_limit = 300; + auto *item = get_item_by_dynamic_item(dyn_item); /* Sanity checks */ g_assert (items_inflight > 0); - auto *dyn_item = get_dynamic_item(item->id, false); + g_assert (item != nullptr); if (dyn_item->async_events > 0) { /* @@ -810,7 +767,7 @@ auto symcache_runtime::process_item_rdeps(struct rspamd_task *task, cache_item * for (const auto &rdep: item->rdeps) { if (rdep.item) { - auto *dyn_item = get_dynamic_item(rdep.item->id, true); + auto *dyn_item = get_dynamic_item(rdep.item->id); if (!dyn_item->started) { msg_debug_cache_task ("check item %d(%s) rdep of %s ", rdep.item->id, rdep.item->symbol.c_str(), item->symbol.c_str()); |