From f16a5382ed9a9750c69595e5752f80e39cf7a4b8 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Thu, 3 Jun 2010 05:56:30 -0400 Subject: commodity_t::find_price now uses memoization This reduces the slowdown of using -V and -X from 36x in some cases down to around 4-5x (for a debug build). --- src/commodity.h | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) (limited to 'src/commodity.h') diff --git a/src/commodity.h b/src/commodity.h index 483d98b0..d8aad10d 100644 --- a/src/commodity.h +++ b/src/commodity.h @@ -59,6 +59,10 @@ struct price_point_t datetime_t when; amount_t price; + bool operator==(const price_point_t& other) const { + return when == other.when && price == other.price; + } + #if defined(HAVE_BOOST_SERIALIZATION) private: /** Serialization. */ @@ -175,6 +179,16 @@ protected: optional smaller; optional larger; + typedef std::pair, + optional > optional_time_pair_t; + typedef std::pair time_and_commodity_t; + typedef std::map > memoized_price_map; + + static const std::size_t max_price_map_size = 16; + mutable memoized_price_map price_map; + mutable bool searched; public: @@ -334,37 +348,28 @@ public: const bool reflexive = true) { if (! base->varied_history) base->varied_history = varied_history_t(); - base->varied_history->add_price(*this, date, price, reflexive); + DEBUG("commodity.prices.find", "Price added, clearing price_map"); + base->price_map.clear(); // a price was added, invalid the map } bool remove_price(const datetime_t& date, commodity_t& commodity) { - if (base->varied_history) + if (base->varied_history) { base->varied_history->remove_price(date, commodity); + DEBUG("commodity.prices.find", "Price removed, clearing price_map"); + base->price_map.clear(); // a price was added, invalid the map + } return false; } optional find_price(const optional& commodity = none, const optional& moment = none, - const optional& oldest = none + const optional& oldest = none, + const bool nested = false #if defined(DEBUG_ON) , const int indent = 0 #endif - ) const { - if (! has_flags(COMMODITY_WALKED) && base->varied_history && - (commodity || ! has_flags(COMMODITY_PRIMARY))) { - const_cast(*this).add_flags(COMMODITY_WALKED); - optional point = - base->varied_history->find_price(*this, commodity, moment, oldest -#if defined(DEBUG_ON) - , indent -#endif - ); - const_cast(*this).drop_flags(COMMODITY_WALKED); - return point; - } - return none; - } + ) const; optional check_for_updated_price(const optional& point, -- cgit v1.2.3