diff --git a/src/terrain_filter.cpp b/src/terrain_filter.cpp index 61e4bf4750c..4fca2af732f 100644 --- a/src/terrain_filter.cpp +++ b/src/terrain_filter.cpp @@ -100,6 +100,13 @@ terrain_filter& terrain_filter::operator=(const terrain_filter& other) return *this ; } +terrain_filter::terrain_filter_cache::terrain_filter_cache() : + parsed_terrain(NULL), + adjacent_matches(NULL), + adjacent_match_cache(), + ufilter_() +{} + namespace { struct cfg_isor { bool operator() (std::pair val) { @@ -156,9 +163,12 @@ bool terrain_filter::match_internal(const map_location& loc, const bool ignore_x //Allow filtering on unit if(cfg_.has_child("filter")) { - const unit_filter ufilt(vconfig(cfg_.child("filter")), fc_, flat_); const unit_map::const_iterator u = fc_->get_disp_context().units().find(loc); - if (u == fc_->get_disp_context().units().end() || !ufilt( *u, loc)) + if (!u.valid()) + return false; + if (!cache_.ufilter_) + cache_.ufilter_.reset(new unit_filter(vconfig(cfg_.child("filter")), fc_, flat_)); + if (!cache_.ufilter_->matches(*u, loc)) return false; } diff --git a/src/terrain_filter.hpp b/src/terrain_filter.hpp index 3f3039cbb92..789b65e0698 100644 --- a/src/terrain_filter.hpp +++ b/src/terrain_filter.hpp @@ -23,9 +23,12 @@ class config; class filter_context; class unit; +class unit_filter; class unit_map; class team; +#include //to memoize unit_filter + //terrain_filter: a class that implements the Standard Location Filter class terrain_filter : public xy_pred { public: @@ -70,12 +73,7 @@ private: const filter_context * fc_; struct terrain_filter_cache { - terrain_filter_cache() : - parsed_terrain(NULL), - adjacent_matches(NULL), - adjacent_match_cache() - { - } + terrain_filter_cache(); ~terrain_filter_cache(); @@ -87,6 +85,8 @@ private: //adjacent_match_cache: optimize handling of [filter_adjacent_location] for match() std::vector< std::pair > > adjacent_match_cache; + + boost::scoped_ptr ufilter_; }; mutable terrain_filter_cache cache_;