Implement proj_transform caching using boost::unordered_map which allows calling find method with compatible key type.

In this case `std::pair<boost::string_view, boost::string_view>` avoiding potentially expensive temp string keys.
(TODO: In the future use c++20 `std::unordered_map::find` transparent keys facility)
This commit is contained in:
Artem Pavlenko 2021-03-03 15:01:58 +00:00
parent 6fedae386d
commit b75737fd6a
3 changed files with 54 additions and 49 deletions

View file

@ -66,10 +66,9 @@ struct layer_rendering_material
std::vector<layer_rendering_material> materials_;
layer_rendering_material(layer const& lay, projection const& dest)
:
lay_(lay),
proj0_(dest),
proj1_(lay.srs(),true) {}
: lay_(lay),
proj0_(dest),
proj1_(lay.srs(), true) {}
layer_rendering_material(layer_rendering_material && rhs) = default;
};
@ -255,13 +254,7 @@ void feature_style_processor<Processor>::prepare_layer(layer_rendering_material
}
processor_context_ptr current_ctx = ds->get_context(ctx_map);
std::string key = mat.proj0_.params() + mat.proj1_.params();
auto itr = m_.proj_cache().find(key);
if (itr == m_.proj_cache().end())
{
throw std::runtime_error("Failed to initialise projection transform");
}
proj_transform * proj_trans_ptr = itr->second.get();
proj_transform * proj_trans_ptr = m_.get_proj_transform(mat.proj0_.params(), mat.proj1_.params());
box2d<double> query_ext = extent; // unbuffered
box2d<double> buffered_query_ext(query_ext); // buffered
@ -500,14 +493,7 @@ void feature_style_processor<Processor>::render_material(layer_rendering_materia
layer const& lay = mat.lay_;
std::vector<rule_cache> const & rule_caches = mat.rule_caches_;
std::string key = mat.proj0_.params() + mat.proj1_.params();
auto itr = m_.proj_cache().find(key);
if (itr == m_.proj_cache().end())
{
throw std::runtime_error("Failed to initialize projection transform");
}
proj_transform* proj_trans_ptr = itr->second.get();
proj_transform * proj_trans_ptr = m_.get_proj_transform(mat.proj0_.params(), mat.proj1_.params());
bool cache_features = lay.cache_features() && active_styles.size() > 1;
datasource_ptr ds = lay.datasource();

View file

@ -38,10 +38,12 @@
MAPNIK_DISABLE_WARNING_PUSH
#include <mapnik/warning_ignore.hpp>
#include <boost/optional.hpp>
#include <boost/functional/hash.hpp>
#include <boost/unordered_map.hpp>
#include <boost/utility/string_view.hpp>
MAPNIK_DISABLE_WARNING_POP
// stl
#include <unordered_map>
#include <memory>
#include <vector>
#include <string>
@ -57,8 +59,32 @@ class layer;
class MAPNIK_DECL Map : boost::equality_comparable<Map>
{
using proj_cache_type = std::unordered_map<std::string, std::unique_ptr<proj_transform>>;
public:
using key_type = std::pair<std::string, std::string>;
using compatible_key_type = std::pair<boost::string_view, boost::string_view>;
struct compatible_hash
{
template <typename KeyType>
std::size_t operator() (KeyType const& key) const
{
using hash_type = boost::hash<typename KeyType::first_type>;
std::size_t seed = hash_type{}(key.first);
seed ^= hash_type{}(key.second) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
return seed;
}
};
struct compatible_predicate
{
bool operator()(compatible_key_type const& k1,
compatible_key_type const& k2) const
{
return k1 == k2;
}
};
using proj_cache_type = boost::unordered_map<key_type, std::unique_ptr<proj_transform>, compatible_hash>;
enum aspect_fix_mode
{
@ -492,11 +518,8 @@ public:
{
return font_memory_cache_;
}
proj_cache_type const& proj_cache() const
{
return proj_cache_;
}
proj_transform * get_proj_transform(std::string const& source, std::string const& dest) const;
private:
friend void swap(Map & rhs, Map & lhs);
void fixAspectRatio();

View file

@ -323,15 +323,27 @@ size_t Map::layer_count() const
return layers_.size();
}
proj_transform * Map::get_proj_transform(std::string const& source, std::string const& dest) const
{
compatible_key_type key = std::make_pair<boost::string_view, boost::string_view>(source, dest);
auto itr = proj_cache_.find(key, compatible_hash{}, compatible_predicate{});
if (itr == proj_cache_.end())
{
throw std::runtime_error("Failed to initialise projection transform:" +
key.first.to_string() + " -> " + key.second.to_string());
}
return itr->second.get();
}
void Map::add_layer(layer const& l)
{
std::string key = srs_ + l.srs();
auto itr = proj_cache_.find(key);
compatible_key_type key = std::make_pair<boost::string_view, boost::string_view>(srs_, l.srs());
auto itr = proj_cache_.find(key, compatible_hash{}, compatible_predicate{});
if (itr == proj_cache_.end())
{
mapnik::projection source(srs_, true);
mapnik::projection dest(l.srs(), true);
proj_cache_.emplace(key,
proj_cache_.emplace(std::make_pair(srs_, l.srs()),
std::make_unique<proj_transform>(source, dest));
}
layers_.emplace_back(l);
@ -339,13 +351,13 @@ void Map::add_layer(layer const& l)
void Map::add_layer(layer && l)
{
std::string key = srs_ + l.srs();
auto itr = proj_cache_.find(key);
compatible_key_type key = std::make_pair<boost::string_view, boost::string_view>(srs_, l.srs());
auto itr = proj_cache_.find(key, compatible_hash{}, compatible_predicate{});
if (itr == proj_cache_.end())
{
mapnik::projection source(srs_, true);
mapnik::projection dest(l.srs(), true);
proj_cache_.emplace(key,
proj_cache_.emplace(make_pair(srs_, l.srs()),
std::make_unique<proj_transform>(source, dest));
}
layers_.push_back(std::move(l));
@ -535,15 +547,7 @@ void Map::zoom_all()
if (layer.active())
{
std::string const& layer_srs = layer.srs();
std::string key = srs_ + layer_srs;
auto itr = proj_cache_.find(key);
if (itr == proj_cache_.end())
{
throw std::runtime_error("Failed to initialise projection transform");
}
proj_transform* proj_trans_ptr = itr->second.get();
proj_transform * proj_trans_ptr = get_proj_transform(srs_, layer_srs);;
box2d<double> layer_ext = layer.envelope();
if (proj_trans_ptr->backward(layer_ext, PROJ_ENVELOPE_POINTS))
{
@ -723,15 +727,7 @@ featureset_ptr Map::query_point(unsigned index, double x, double y) const
mapnik::datasource_ptr ds = layer.datasource();
if (ds)
{
std::string key = srs_ + layer.srs();
auto itr = proj_cache_.find(key);
if (itr == proj_cache_.end())
{
throw std::runtime_error("Failed to initialise projection transform");
}
proj_transform * proj_trans_ptr = itr->second.get();
proj_transform * proj_trans_ptr = get_proj_transform(srs_ ,layer.srs());
double z = 0;
if (!proj_trans_ptr->equal() && !proj_trans_ptr->backward(x,y,z))
{