From 27b86a5388a9b1a5d5c748fa7e6f8aa4a2f1a3f4 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Thu, 3 Jun 2010 17:43:38 -0400 Subject: Avoid unnecessary object copying when using foreach --- src/commodity.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/commodity.cc') diff --git a/src/commodity.cc b/src/commodity.cc index 1b85910f..a1c26403 100644 --- a/src/commodity.cc +++ b/src/commodity.cc @@ -262,7 +262,7 @@ commodity_t::varied_history_t::find_price(const commodity_t& source, price_point_t best; bool found = false; - foreach (history_by_commodity_map::value_type hist, histories) { + foreach (const history_by_commodity_map::value_type& hist, histories) { commodity_t& comm(*hist.first); if (comm == source) continue; -- cgit v1.2.3 From 45451125e329f47314e24b34425f6da3aab920cd Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Thu, 3 Jun 2010 23:23:12 -0400 Subject: Simplified some debug code --- src/commodity.cc | 49 ++++++++++++++++--------------------------------- 1 file changed, 16 insertions(+), 33 deletions(-) (limited to 'src/commodity.cc') diff --git a/src/commodity.cc b/src/commodity.cc index a1c26403..b1cdfdb4 100644 --- a/src/commodity.cc +++ b/src/commodity.cc @@ -120,12 +120,16 @@ commodity_t::history_t::find_price(const optional& moment, price_point_t point; bool found = false; +#if defined(DEBUG_ON) #define DEBUG_INDENT(cat, indent) \ do { \ if (SHOW_DEBUG(cat)) \ for (int i = 0; i < indent; i++) \ ledger::_log_buffer << " "; \ } while (false) +#else +#define DEBUG_INDENT(cat, indent) +#endif #if defined(DEBUG_ON) DEBUG_INDENT("commodity.prices.find", indent); @@ -141,10 +145,8 @@ commodity_t::history_t::find_price(const optional& moment, #endif if (prices.size() == 0) { -#if defined(DEBUG_ON) DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.prices.find", " there are no prices in this history"); -#endif return none; } @@ -153,10 +155,9 @@ commodity_t::history_t::find_price(const optional& moment, point.when = (*r).first; point.price = (*r).second; found = true; -#if defined(DEBUG_ON) + DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.prices.find", " using most recent price"); -#endif } else { history_map::const_iterator i = prices.lower_bound(*moment); if (i == prices.end()) { @@ -164,10 +165,9 @@ commodity_t::history_t::find_price(const optional& moment, point.when = (*r).first; point.price = (*r).second; found = true; -#if defined(DEBUG_ON) + DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.prices.find", " using last price"); -#endif } else { point.when = (*i).first; if (*moment < point.when) { @@ -181,40 +181,31 @@ commodity_t::history_t::find_price(const optional& moment, point.price = (*i).second; found = true; } -#if defined(DEBUG_ON) + DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.prices.find", " using found price"); -#endif } } if (! found) { -#if defined(DEBUG_ON) DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.prices.find", " could not find a price"); -#endif return none; } else if (moment && point.when > *moment) { -#if defined(DEBUG_ON) DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.prices.find", " price is too young "); -#endif return none; } else if (oldest && point.when < *oldest) { -#if defined(DEBUG_ON) DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.prices.find", " price is too old "); -#endif return none; } else { -#if defined(DEBUG_ON) DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.prices.find", " returning price: " << point.when << ", " << point.price); -#endif return point; } } @@ -232,7 +223,13 @@ commodity_t::varied_history_t::find_price(const commodity_t& source, optional point; optional limit = oldest; - assert(! commodity || source != *commodity); +#if defined(VERIFY_ON) + if (commodity) { + VERIFY(source != *commodity); + VERIFY(! commodity->has_annotation()); + VERIFY(source.referent() != commodity->referent()); + } +#endif #if defined(DEBUG_ON) DEBUG_INDENT("commodity.prices.find", indent); @@ -267,11 +264,9 @@ commodity_t::varied_history_t::find_price(const commodity_t& source, if (comm == source) continue; -#if defined(DEBUG_ON) DEBUG_INDENT("commodity.prices.find", indent + 1); DEBUG("commodity.prices.find", " searching for price via commodity '" << comm << "'"); -#endif point = hist.second.find_price(moment, limit #if defined(DEBUG_ON) @@ -284,10 +279,8 @@ commodity_t::varied_history_t::find_price(const commodity_t& source, optional xlat; if (commodity && comm != *commodity) { -#if defined(DEBUG_ON) DEBUG_INDENT("commodity.prices.find", indent + 1); DEBUG("commodity.prices.find", " looking for translation price"); -#endif xlat = comm.find_price(commodity, moment, limit #if defined(DEBUG_ON) @@ -295,57 +288,47 @@ commodity_t::varied_history_t::find_price(const commodity_t& source, #endif ); if (xlat) { -#if defined(DEBUG_ON) DEBUG_INDENT("commodity.prices.find", indent + 1); DEBUG("commodity.prices.find", " found translated price " << xlat->price << " from " << xlat->when); -#endif point->price = xlat->price * point->price; if (xlat->when < point->when) { point->when = xlat->when; -#if defined(DEBUG_ON) + DEBUG_INDENT("commodity.prices.find", indent + 1); DEBUG("commodity.prices.find", " adjusting date of result back to " << point->when); -#endif } } else { -#if defined(DEBUG_ON) DEBUG_INDENT("commodity.prices.find", indent + 1); DEBUG("commodity.prices.find", " saw no translated price there"); -#endif continue; } } assert(! commodity || point->price.commodity() == *commodity); -#if defined(DEBUG_ON) DEBUG_INDENT("commodity.prices.find", indent + 1); DEBUG("commodity.prices.find", " saw a price there: " << point->price << " from " << point->when); -#endif + if (! limit || point->when > *limit) { limit = point->when; best = *point; found = true; } } else { -#if defined(DEBUG_ON) DEBUG_INDENT("commodity.prices.find", indent + 1); DEBUG("commodity.prices.find", " saw no price there"); -#endif } } if (found) { -#if defined(DEBUG_ON) DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.prices.find", " found price " << best.price << " from " << best.when); DEBUG("commodity.download", "found price " << best.price << " from " << best.when); -#endif return best; } return none; -- cgit v1.2.3 From 2ae2dc86c6bf88f512f905ff3b9ed9c170baf536 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Fri, 4 Jun 2010 02:54:38 -0400 Subject: Reorganized some debug code --- src/commodity.cc | 37 ++++++++++++++++++++----------------- 1 file changed, 20 insertions(+), 17 deletions(-) (limited to 'src/commodity.cc') diff --git a/src/commodity.cc b/src/commodity.cc index b1cdfdb4..7aecc2bb 100644 --- a/src/commodity.cc +++ b/src/commodity.cc @@ -235,20 +235,20 @@ commodity_t::varied_history_t::find_price(const commodity_t& source, DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.prices.find", "varied_find_price for: " << source); - DEBUG_INDENT("commodity.prices.find", indent); + DEBUG_INDENT("commodity.prices.find", indent + 1); if (commodity) - DEBUG("commodity.prices.find", " looking for: commodity '" << *commodity << "'"); + DEBUG("commodity.prices.find", "looking for: commodity '" << *commodity << "'"); else - DEBUG("commodity.prices.find", " looking for: any commodity"); + DEBUG("commodity.prices.find", "looking for: any commodity"); if (moment) { - DEBUG_INDENT("commodity.prices.find", indent); - DEBUG("commodity.prices.find", " time index: " << *moment); + DEBUG_INDENT("commodity.prices.find", indent + 1); + DEBUG("commodity.prices.find", "time index: " << *moment); } if (oldest) { - DEBUG_INDENT("commodity.prices.find", indent); - DEBUG("commodity.prices.find", " only consider prices younger than: " << *oldest); + DEBUG_INDENT("commodity.prices.find", indent + 1); + DEBUG("commodity.prices.find", "only consider prices younger than: " << *oldest); } #endif @@ -266,7 +266,7 @@ commodity_t::varied_history_t::find_price(const commodity_t& source, DEBUG_INDENT("commodity.prices.find", indent + 1); DEBUG("commodity.prices.find", - " searching for price via commodity '" << comm << "'"); + "searching for price via commodity '" << comm << "'"); point = hist.second.find_price(moment, limit #if defined(DEBUG_ON) @@ -280,7 +280,7 @@ commodity_t::varied_history_t::find_price(const commodity_t& source, if (commodity && comm != *commodity) { DEBUG_INDENT("commodity.prices.find", indent + 1); - DEBUG("commodity.prices.find", " looking for translation price"); + DEBUG("commodity.prices.find", "looking for translation price"); xlat = comm.find_price(commodity, moment, limit #if defined(DEBUG_ON) @@ -289,7 +289,7 @@ commodity_t::varied_history_t::find_price(const commodity_t& source, ); if (xlat) { DEBUG_INDENT("commodity.prices.find", indent + 1); - DEBUG("commodity.prices.find", " found translated price " + DEBUG("commodity.prices.find", "found translated price " << xlat->price << " from " << xlat->when); point->price = xlat->price * point->price; @@ -298,35 +298,38 @@ commodity_t::varied_history_t::find_price(const commodity_t& source, DEBUG_INDENT("commodity.prices.find", indent + 1); DEBUG("commodity.prices.find", - " adjusting date of result back to " << point->when); + "adjusting date of result back to " << point->when); } } else { DEBUG_INDENT("commodity.prices.find", indent + 1); - DEBUG("commodity.prices.find", " saw no translated price there"); + DEBUG("commodity.prices.find", "saw no translated price there"); continue; } } assert(! commodity || point->price.commodity() == *commodity); + DEBUG_INDENT("commodity.prices.find", indent + 1); DEBUG("commodity.prices.find", - " saw a price there: " << point->price << " from " << point->when); + "saw a price there: " << point->price << " from " << point->when); if (! limit || point->when > *limit) { limit = point->when; best = *point; found = true; + + DEBUG_INDENT("commodity.prices.find", indent + 1); + DEBUG("commodity.prices.find", + "search limit adjusted to " << *limit); } } else { DEBUG_INDENT("commodity.prices.find", indent + 1); - DEBUG("commodity.prices.find", " saw no price there"); + DEBUG("commodity.prices.find", "saw no price there"); } } if (found) { - DEBUG_INDENT("commodity.prices.find", indent); - DEBUG("commodity.prices.find", - " found price " << best.price << " from " << best.when); + DEBUG_INDENT("commodity.prices.find", indent + 1); DEBUG("commodity.download", "found price " << best.price << " from " << best.when); return best; -- cgit v1.2.3 From 7f7243ff9360f1c44f9e7b16a0e93244645aa031 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Fri, 4 Jun 2010 03:11:14 -0400 Subject: Made the behavior of -V a bit more rational It now only values non-primary commodities in terms of primary ones. --- src/commodity.cc | 4 ++++ src/commodity.h | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'src/commodity.cc') diff --git a/src/commodity.cc b/src/commodity.cc index 7aecc2bb..61fd0fe5 100644 --- a/src/commodity.cc +++ b/src/commodity.cc @@ -264,6 +264,10 @@ commodity_t::varied_history_t::find_price(const commodity_t& source, if (comm == source) continue; + // Only value secondary commodities in terms of primary ones + if (! commodity && ! comm.has_flags(COMMODITY_PRIMARY)) + continue; + DEBUG_INDENT("commodity.prices.find", indent + 1); DEBUG("commodity.prices.find", "searching for price via commodity '" << comm << "'"); diff --git a/src/commodity.h b/src/commodity.h index 05dc3395..483d98b0 100644 --- a/src/commodity.h +++ b/src/commodity.h @@ -351,7 +351,8 @@ public: , const int indent = 0 #endif ) const { - if (base->varied_history && ! has_flags(COMMODITY_WALKED)) { + 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 -- cgit v1.2.3 From b7e8cb9d544af0f054093092afa4d0820aa4a453 Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Fri, 4 Jun 2010 03:23:31 -0400 Subject: More debug code cleanup --- src/commodity.cc | 50 +++++++++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 25 deletions(-) (limited to 'src/commodity.cc') diff --git a/src/commodity.cc b/src/commodity.cc index 61fd0fe5..c2fc0935 100644 --- a/src/commodity.cc +++ b/src/commodity.cc @@ -140,26 +140,26 @@ commodity_t::history_t::find_price(const optional& moment, if (oldest) { DEBUG_INDENT("commodity.prices.find", indent); - DEBUG("commodity.prices.find", " but no older than: " << *oldest); + DEBUG("commodity.prices.find", "but no older than: " << *oldest); } #endif if (prices.size() == 0) { DEBUG_INDENT("commodity.prices.find", indent); - DEBUG("commodity.prices.find", " there are no prices in this history"); + DEBUG("commodity.prices.find", "there are no prices in this history"); return none; } if (! moment) { history_map::const_reverse_iterator r = prices.rbegin(); - point.when = (*r).first; + point.when = (*r).first; point.price = (*r).second; found = true; DEBUG_INDENT("commodity.prices.find", indent); - DEBUG("commodity.prices.find", " using most recent price"); + DEBUG("commodity.prices.find", "using most recent price"); } else { - history_map::const_iterator i = prices.lower_bound(*moment); + history_map::const_iterator i = prices.upper_bound(*moment); if (i == prices.end()) { history_map::const_reverse_iterator r = prices.rbegin(); point.when = (*r).first; @@ -167,7 +167,7 @@ commodity_t::history_t::find_price(const optional& moment, found = true; DEBUG_INDENT("commodity.prices.find", indent); - DEBUG("commodity.prices.find", " using last price"); + DEBUG("commodity.prices.find", "using last price"); } else { point.when = (*i).first; if (*moment < point.when) { @@ -183,29 +183,29 @@ commodity_t::history_t::find_price(const optional& moment, } DEBUG_INDENT("commodity.prices.find", indent); - DEBUG("commodity.prices.find", " using found price"); + DEBUG("commodity.prices.find", "using found price"); } } if (! found) { DEBUG_INDENT("commodity.prices.find", indent); - DEBUG("commodity.prices.find", " could not find a price"); + DEBUG("commodity.prices.find", "could not find a price"); return none; } else if (moment && point.when > *moment) { DEBUG_INDENT("commodity.prices.find", indent); - DEBUG("commodity.prices.find", " price is too young "); + DEBUG("commodity.prices.find", "price is too young "); return none; } else if (oldest && point.when < *oldest) { DEBUG_INDENT("commodity.prices.find", indent); - DEBUG("commodity.prices.find", " price is too old "); + DEBUG("commodity.prices.find", "price is too old "); return none; } else { DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.prices.find", - " returning price: " << point.when << ", " << point.price); + "returning price: " << point.when << ", " << point.price); return point; } } @@ -235,19 +235,19 @@ commodity_t::varied_history_t::find_price(const commodity_t& source, DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.prices.find", "varied_find_price for: " << source); - DEBUG_INDENT("commodity.prices.find", indent + 1); + DEBUG_INDENT("commodity.prices.find", indent); if (commodity) DEBUG("commodity.prices.find", "looking for: commodity '" << *commodity << "'"); else DEBUG("commodity.prices.find", "looking for: any commodity"); if (moment) { - DEBUG_INDENT("commodity.prices.find", indent + 1); + DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.prices.find", "time index: " << *moment); } if (oldest) { - DEBUG_INDENT("commodity.prices.find", indent + 1); + DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.prices.find", "only consider prices younger than: " << *oldest); } #endif @@ -268,13 +268,13 @@ commodity_t::varied_history_t::find_price(const commodity_t& source, if (! commodity && ! comm.has_flags(COMMODITY_PRIMARY)) continue; - DEBUG_INDENT("commodity.prices.find", indent + 1); + DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.prices.find", "searching for price via commodity '" << comm << "'"); point = hist.second.find_price(moment, limit #if defined(DEBUG_ON) - , indent + 2 + , indent + 1 #endif ); assert(! point || point->price.commodity() == comm); @@ -283,16 +283,16 @@ commodity_t::varied_history_t::find_price(const commodity_t& source, optional xlat; if (commodity && comm != *commodity) { - DEBUG_INDENT("commodity.prices.find", indent + 1); + DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.prices.find", "looking for translation price"); xlat = comm.find_price(commodity, moment, limit #if defined(DEBUG_ON) - , indent + 2 + , indent + 1 #endif ); if (xlat) { - DEBUG_INDENT("commodity.prices.find", indent + 1); + DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.prices.find", "found translated price " << xlat->price << " from " << xlat->when); @@ -300,12 +300,12 @@ commodity_t::varied_history_t::find_price(const commodity_t& source, if (xlat->when < point->when) { point->when = xlat->when; - DEBUG_INDENT("commodity.prices.find", indent + 1); + DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.prices.find", "adjusting date of result back to " << point->when); } } else { - DEBUG_INDENT("commodity.prices.find", indent + 1); + DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.prices.find", "saw no translated price there"); continue; } @@ -313,7 +313,7 @@ commodity_t::varied_history_t::find_price(const commodity_t& source, assert(! commodity || point->price.commodity() == *commodity); - DEBUG_INDENT("commodity.prices.find", indent + 1); + DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.prices.find", "saw a price there: " << point->price << " from " << point->when); @@ -322,18 +322,18 @@ commodity_t::varied_history_t::find_price(const commodity_t& source, best = *point; found = true; - DEBUG_INDENT("commodity.prices.find", indent + 1); + DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.prices.find", "search limit adjusted to " << *limit); } } else { - DEBUG_INDENT("commodity.prices.find", indent + 1); + DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.prices.find", "saw no price there"); } } if (found) { - DEBUG_INDENT("commodity.prices.find", indent + 1); + DEBUG_INDENT("commodity.prices.find", indent); DEBUG("commodity.download", "found price " << best.price << " from " << best.when); return best; -- cgit v1.2.3 From a4a45cb4d61e028a19be3fb5be889e62dc83214e Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Fri, 4 Jun 2010 03:40:39 -0400 Subject: any and all do not need to be reserved tokens --- src/commodity.cc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/commodity.cc') diff --git a/src/commodity.cc b/src/commodity.cc index c2fc0935..2ed6553b 100644 --- a/src/commodity.cc +++ b/src/commodity.cc @@ -418,9 +418,7 @@ namespace { { switch (buf[0]) { case 'a': - return (std::strcmp(buf, "and") == 0 || - std::strcmp(buf, "any") == 0 || - std::strcmp(buf, "all") == 0); + return std::strcmp(buf, "and") == 0; case 'd': return std::strcmp(buf, "div") == 0; case 'e': -- cgit v1.2.3 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.cc | 111 +++++++++++++++++++++++++++++++++++++++++++++++++------ src/commodity.h | 41 +++++++++++--------- 2 files changed, 123 insertions(+), 29 deletions(-) (limited to 'src/commodity.cc') diff --git a/src/commodity.cc b/src/commodity.cc index 2ed6553b..1554887c 100644 --- a/src/commodity.cc +++ b/src/commodity.cc @@ -268,13 +268,13 @@ commodity_t::varied_history_t::find_price(const commodity_t& source, if (! commodity && ! comm.has_flags(COMMODITY_PRIMARY)) continue; - DEBUG_INDENT("commodity.prices.find", indent); + DEBUG_INDENT("commodity.prices.find", indent + 1); DEBUG("commodity.prices.find", "searching for price via commodity '" << comm << "'"); point = hist.second.find_price(moment, limit #if defined(DEBUG_ON) - , indent + 1 + , indent + 2 #endif ); assert(! point || point->price.commodity() == comm); @@ -283,16 +283,16 @@ commodity_t::varied_history_t::find_price(const commodity_t& source, optional xlat; if (commodity && comm != *commodity) { - DEBUG_INDENT("commodity.prices.find", indent); + DEBUG_INDENT("commodity.prices.find", indent + 1); DEBUG("commodity.prices.find", "looking for translation price"); - xlat = comm.find_price(commodity, moment, limit + xlat = comm.find_price(commodity, moment, limit, true #if defined(DEBUG_ON) - , indent + 1 + , indent + 2 #endif ); if (xlat) { - DEBUG_INDENT("commodity.prices.find", indent); + DEBUG_INDENT("commodity.prices.find", indent + 1); DEBUG("commodity.prices.find", "found translated price " << xlat->price << " from " << xlat->when); @@ -300,12 +300,12 @@ commodity_t::varied_history_t::find_price(const commodity_t& source, if (xlat->when < point->when) { point->when = xlat->when; - DEBUG_INDENT("commodity.prices.find", indent); + DEBUG_INDENT("commodity.prices.find", indent + 1); DEBUG("commodity.prices.find", "adjusting date of result back to " << point->when); } } else { - DEBUG_INDENT("commodity.prices.find", indent); + DEBUG_INDENT("commodity.prices.find", indent + 1); DEBUG("commodity.prices.find", "saw no translated price there"); continue; } @@ -313,7 +313,7 @@ commodity_t::varied_history_t::find_price(const commodity_t& source, assert(! commodity || point->price.commodity() == *commodity); - DEBUG_INDENT("commodity.prices.find", indent); + DEBUG_INDENT("commodity.prices.find", indent + 1); DEBUG("commodity.prices.find", "saw a price there: " << point->price << " from " << point->when); @@ -322,12 +322,12 @@ commodity_t::varied_history_t::find_price(const commodity_t& source, best = *point; found = true; - DEBUG_INDENT("commodity.prices.find", indent); + DEBUG_INDENT("commodity.prices.find", indent + 1); DEBUG("commodity.prices.find", "search limit adjusted to " << *limit); } } else { - DEBUG_INDENT("commodity.prices.find", indent); + DEBUG_INDENT("commodity.prices.find", indent + 1); DEBUG("commodity.prices.find", "saw no price there"); } } @@ -360,6 +360,95 @@ commodity_t::varied_history_t::history(const optional& commodity) return none; } +optional +commodity_t::find_price(const optional& commodity, + const optional& moment, + const optional& oldest, + const bool nested +#if defined(DEBUG_ON) + , const int indent +#endif + ) const +{ + if (! has_flags(COMMODITY_WALKED) && base->varied_history && + (commodity || ! has_flags(COMMODITY_PRIMARY))) { + optional pair; +#if defined(VERIFY_ON) + optional checkpoint; +#endif + + if (! nested) { + pair = base_t::time_and_commodity_t + (base_t::optional_time_pair_t(moment, oldest), + commodity ? &(*commodity) : NULL); + DEBUG_INDENT("commodity.prices.find", indent); + DEBUG("commodity.prices.find", "looking for memoized args: " + << (moment ? format_datetime(*moment) : "NONE") << ", " + << (oldest ? format_datetime(*oldest) : "NONE") << ", " + << (commodity ? commodity->symbol() : "NONE")); + + base_t::memoized_price_map::iterator i = base->price_map.find(*pair); + if (i != base->price_map.end()) { + DEBUG_INDENT("commodity.prices.find", indent); + DEBUG("commodity.prices.find", "found! returning: " + << ((*i).second ? (*i).second->price : amount_t(0L))); +#if defined(VERIFY_ON) + IF_VERIFY() { + checkpoint = (*i).second; + } else +#endif // defined(VERIFY_ON) + return (*i).second; + } + } + + optional point; + + const_cast(*this).add_flags(COMMODITY_WALKED); + try { + DEBUG_INDENT("commodity.prices.find", indent); + DEBUG("commodity.prices.find", "manually finding price..."); + + point = base->varied_history->find_price(*this, commodity, + moment, oldest +#if defined(DEBUG_ON) + , indent +#endif + ); + } + catch (...) { + const_cast(*this).drop_flags(COMMODITY_WALKED); + throw; + } + const_cast(*this).drop_flags(COMMODITY_WALKED); + +#if defined(VERIFY_ON) + if (DO_VERIFY() && pair) { + VERIFY(checkpoint == point); + return checkpoint; + } +#endif // defined(VERIFY_ON) + + if (! nested && pair) { + if (base->price_map.size() > base_t::max_price_map_size) { + DEBUG_INDENT("commodity.prices.find", indent); + DEBUG("commodity.prices.find", + "price map has grown too large, clearing it by half"); + + for (std::size_t i = 0; i < base_t::max_price_map_size >> 1; i++) + base->price_map.erase(base->price_map.begin()); + } + + DEBUG_INDENT("commodity.prices.find", indent); + DEBUG("commodity.prices.find", + "remembered: " << (point ? point->price : amount_t(0L))); + base->price_map.insert + (base_t::memoized_price_map::value_type(*pair, point)); + } + return point; + } + return none; +} + optional commodity_t::check_for_updated_price(const optional& point, const optional& moment, 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 From 653d2bd99a04f422c3755f42d6091f2b8817b26f Mon Sep 17 00:00:00 2001 From: John Wiegley Date: Sat, 5 Jun 2010 01:57:16 -0400 Subject: Corrected a verification test --- src/commodity.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'src/commodity.cc') diff --git a/src/commodity.cc b/src/commodity.cc index 1554887c..e45332b2 100644 --- a/src/commodity.cc +++ b/src/commodity.cc @@ -375,6 +375,7 @@ commodity_t::find_price(const optional& commodity, optional pair; #if defined(VERIFY_ON) optional checkpoint; + bool found = false; #endif if (! nested) { @@ -394,6 +395,7 @@ commodity_t::find_price(const optional& commodity, << ((*i).second ? (*i).second->price : amount_t(0L))); #if defined(VERIFY_ON) IF_VERIFY() { + found = true; checkpoint = (*i).second; } else #endif // defined(VERIFY_ON) @@ -422,7 +424,7 @@ commodity_t::find_price(const optional& commodity, const_cast(*this).drop_flags(COMMODITY_WALKED); #if defined(VERIFY_ON) - if (DO_VERIFY() && pair) { + if (DO_VERIFY() && found) { VERIFY(checkpoint == point); return checkpoint; } -- cgit v1.2.3