From 0684148d54e8839ced859e63906449368188d46e Mon Sep 17 00:00:00 2001 From: Charlie Blake Date: Mon, 11 Jun 2018 17:23:05 +0100 Subject: Speedup build and add lru cache --- src/main/game/global_cache.cpp | 78 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 70 insertions(+), 8 deletions(-) (limited to 'src/main/game/global_cache.cpp') diff --git a/src/main/game/global_cache.cpp b/src/main/game/global_cache.cpp index 3620ddb..3732789 100644 --- a/src/main/game/global_cache.cpp +++ b/src/main/game/global_cache.cpp @@ -14,6 +14,17 @@ using namespace boost; typedef sol_rules::build_policy pol; typedef sol_rules::stock_deal_type sdt; +typedef boost::multi_index::multi_index_container< + cached_game_state, + boost::multi_index::indexed_by< + boost::multi_index::sequenced<>, + boost::multi_index::hashed_unique< + boost::multi_index::identity, + hasher + > + > +> item_list; + /////////////////////// // CACHED GAME STATE // @@ -163,18 +174,69 @@ size_t hasher::hash_value(card const& c) const { // GLOBAL CACHE // ////////////////// -bool global_cache::insert(const game_state& gs) { - return u_set.emplace(gs).second; +unlimited_cache::unlimited_cache(const game_state& gs) + : cache(0, hasher(gs)) { +} + +bool unlimited_cache::insert(const game_state& gs) { + return cache.emplace(gs).second; +} + +bool unlimited_cache::contains(const game_state& gs) const { + return cache.count(cached_game_state(gs)) > 0; +} + +void unlimited_cache::clear() { + cache.clear(); } -bool global_cache::contains(const game_state& gs) const { - return u_set.count(cached_game_state(gs)) > 0; + +/////////////// +// LRU CACHE // +/////////////// + +// Based on https://www.boost.org/doc/libs/1_67_0/libs/multi_index/example/serialization.cpp +/* Boost.MultiIndex example of serialization of a MRU list. + * + * Copyright 2003-2008 Joaquin M Lopez Munoz. + * Distributed under the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or copy at + * http://www.boost.org/LICENSE_1_0.txt) + * + * See http://www.boost.org/libs/multi_index for library home page. + */ + +item_list::ctor_args_list lru_cache::get_init_tuple(const game_state& gs) { + return boost::make_tuple( + item_list::nth_index<0>::type::ctor_args(), + boost::make_tuple( + size_t(0), + multi_index::identity(), + hasher(gs), + equal_to() + ) + ); +} + +lru_cache::lru_cache(const game_state& gs, size_t max_num_items_) + : max_num_items(max_num_items_), cache(get_init_tuple(gs)) { +} + +bool lru_cache::insert(const game_state& gs) { + pair p = cache.push_front(cached_game_state(gs)); + + if(!p.second){ /* duplicate item */ + cache.relocate(cache.begin(), p.first); /* put in front */ + } else if(cache.size() > max_num_items){ /* keep the length <= max_num_items */ + cache.pop_back(); + } + return p.second; } -void global_cache::clear() { - u_set.clear(); +bool lru_cache::contains(const game_state& gs) const { + return cache.get<1>().count(cached_game_state(gs)) > 0; } -global_cache::global_cache(const game_state& gs) - : u_set(0, hasher(gs)) { +void lru_cache::clear() { + cache.clear(); } -- cgit v1.2.1