Move proj_transform_cache declarations and implementaion into separate translation unit (ref VC++ C2492)

This commit is contained in:
Artem Pavlenko 2021-03-25 12:16:15 +00:00
parent c76f65a49a
commit 503b9c5bbf
7 changed files with 154 additions and 67 deletions

View file

@ -41,7 +41,7 @@
#include <mapnik/expression_evaluator.hpp> #include <mapnik/expression_evaluator.hpp>
#include <mapnik/scale_denominator.hpp> #include <mapnik/scale_denominator.hpp>
#include <mapnik/projection.hpp> #include <mapnik/projection.hpp>
#include <mapnik/proj_transform.hpp> #include <mapnik/proj_transform_cache.hpp>
#include <mapnik/util/featureset_buffer.hpp> #include <mapnik/util/featureset_buffer.hpp>
#include <mapnik/util/variant.hpp> #include <mapnik/util/variant.hpp>
#include <mapnik/symbolizer_dispatch.hpp> #include <mapnik/symbolizer_dispatch.hpp>
@ -254,7 +254,7 @@ void feature_style_processor<Processor>::prepare_layer(layer_rendering_material
} }
processor_context_ptr current_ctx = ds->get_context(ctx_map); processor_context_ptr current_ctx = ds->get_context(ctx_map);
proj_transform * proj_trans_ptr = m_.get_proj_transform(mat.proj0_.params(), mat.proj1_.params()); proj_transform const* proj_trans_ptr = m_.get_proj_transform(mat.proj0_.params(), mat.proj1_.params());
box2d<double> query_ext = extent; // unbuffered box2d<double> query_ext = extent; // unbuffered
box2d<double> buffered_query_ext(query_ext); // buffered box2d<double> buffered_query_ext(query_ext); // buffered
@ -493,7 +493,7 @@ void feature_style_processor<Processor>::render_material(layer_rendering_materia
layer const& lay = mat.lay_; layer const& lay = mat.lay_;
std::vector<rule_cache> const & rule_caches = mat.rule_caches_; std::vector<rule_cache> const & rule_caches = mat.rule_caches_;
proj_transform * proj_trans_ptr = m_.get_proj_transform(mat.proj0_.params(), mat.proj1_.params()); proj_transform const* proj_trans_ptr = m_.get_proj_transform(mat.proj0_.params(), mat.proj1_.params());
bool cache_features = lay.cache_features() && active_styles.size() > 1; bool cache_features = lay.cache_features() && active_styles.size() > 1;
datasource_ptr ds = lay.datasource(); datasource_ptr ds = lay.datasource();

View file

@ -38,9 +38,6 @@
MAPNIK_DISABLE_WARNING_PUSH MAPNIK_DISABLE_WARNING_PUSH
#include <mapnik/warning_ignore.hpp> #include <mapnik/warning_ignore.hpp>
#include <boost/optional.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 MAPNIK_DISABLE_WARNING_POP
// stl // stl
@ -56,36 +53,11 @@ using featureset_ptr = std::shared_ptr<Featureset>;
class feature_type_style; class feature_type_style;
class view_transform; class view_transform;
class layer; class layer;
struct proj_transform_cache;
class MAPNIK_DECL Map : boost::equality_comparable<Map> class MAPNIK_DECL Map : boost::equality_comparable<Map>
{ {
public: 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 enum aspect_fix_mode
{ {
// grow the width or height of the specified geo bbox to fill the map size. default behaviour. // grow the width or height of the specified geo bbox to fill the map size. default behaviour.
@ -131,9 +103,8 @@ private:
boost::optional<std::string> font_directory_; boost::optional<std::string> font_directory_;
freetype_engine::font_file_mapping_type font_file_mapping_; freetype_engine::font_file_mapping_type font_file_mapping_;
freetype_engine::font_memory_cache_type font_memory_cache_; freetype_engine::font_memory_cache_type font_memory_cache_;
thread_local static proj_cache_type proj_cache_; std::unique_ptr<proj_transform_cache> proj_cache_ = {};
public: public:
using const_style_iterator = std::map<std::string,feature_type_style>::const_iterator; using const_style_iterator = std::map<std::string,feature_type_style>::const_iterator;
using style_iterator = std::map<std::string,feature_type_style>::iterator; using style_iterator = std::map<std::string,feature_type_style>::iterator;
using const_fontset_iterator = std::map<std::string,font_set>::const_iterator; using const_fontset_iterator = std::map<std::string,font_set>::const_iterator;
@ -530,7 +501,8 @@ public:
return font_memory_cache_; return font_memory_cache_;
} }
proj_transform * get_proj_transform(std::string const& source, std::string const& dest) const; proj_transform const* get_proj_transform(std::string const& source, std::string const& dest) const;
private: private:
friend void swap(Map & rhs, Map & lhs); friend void swap(Map & rhs, Map & lhs);
void fixAspectRatio(); void fixAspectRatio();

View file

@ -0,0 +1,76 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2021 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
#ifndef MAPNIK_PROJ_TRANSFORM_CACHE_HPP
#define MAPNIK_PROJ_TRANSFORM_CACHE_HPP
#include <mapnik/util/noncopyable.hpp>
#include <mapnik/proj_transform.hpp>
MAPNIK_DISABLE_WARNING_PUSH
#include <mapnik/warning_ignore.hpp>
#include <boost/functional/hash.hpp>
#include <boost/unordered_map.hpp>
#include <boost/utility/string_view.hpp>
MAPNIK_DISABLE_WARNING_POP
namespace mapnik {
struct proj_transform_cache : util::noncopyable
{
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 cache_type = boost::unordered_map<key_type, std::unique_ptr<proj_transform>, compatible_hash>;
proj_transform_cache() = default;
thread_local static cache_type cache_;
void init(std::string const& source, std::string const& dest) const;
proj_transform const* get(std::string const& source, std::string const& dest) const;
};
}
#endif // MAPNIK_PROJ_TRANSFORM_CACHE_HPP

View file

@ -214,6 +214,7 @@ source = Split(
twkb.cpp twkb.cpp
projection.cpp projection.cpp
proj_transform.cpp proj_transform.cpp
proj_transform_cache.cpp
scale_denominator.cpp scale_denominator.cpp
simplify.cpp simplify.cpp
parse_transform.cpp parse_transform.cpp

View file

@ -31,7 +31,7 @@
#include <mapnik/map.hpp> #include <mapnik/map.hpp>
#include <mapnik/datasource.hpp> #include <mapnik/datasource.hpp>
#include <mapnik/projection.hpp> #include <mapnik/projection.hpp>
#include <mapnik/proj_transform.hpp> #include <mapnik/proj_transform_cache.hpp>
#include <mapnik/view_transform.hpp> #include <mapnik/view_transform.hpp>
#include <mapnik/filter_featureset.hpp> #include <mapnik/filter_featureset.hpp>
#include <mapnik/hit_test_filter.hpp> #include <mapnik/hit_test_filter.hpp>
@ -63,6 +63,7 @@ static const char * aspect_fix_mode_strings[] = {
IMPLEMENT_ENUM( aspect_fix_mode_e, aspect_fix_mode_strings ) IMPLEMENT_ENUM( aspect_fix_mode_e, aspect_fix_mode_strings )
Map::Map() Map::Map()
: width_(400), : width_(400),
height_(400), height_(400),
@ -136,11 +137,11 @@ Map::Map(Map && rhs)
extra_params_(std::move(rhs.extra_params_)), extra_params_(std::move(rhs.extra_params_)),
font_directory_(std::move(rhs.font_directory_)), font_directory_(std::move(rhs.font_directory_)),
font_file_mapping_(std::move(rhs.font_file_mapping_)), font_file_mapping_(std::move(rhs.font_file_mapping_)),
font_memory_cache_(std::move(rhs.font_memory_cache_)) {} font_memory_cache_(std::move(rhs.font_memory_cache_)),
proj_cache_(std::move(rhs.proj_cache_)) {}
Map::~Map() {} Map::~Map() {}
thread_local Map::proj_cache_type Map::proj_cache_ = proj_cache_type();
Map& Map::operator=(Map rhs) Map& Map::operator=(Map rhs)
{ {
@ -327,21 +328,6 @@ size_t Map::layer_count() const
return layers_.size(); 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())
{
mapnik::projection srs1(source, true);
mapnik::projection srs2(dest, true);
return proj_cache_.emplace(std::make_pair(source, dest),
std::make_unique<proj_transform>(srs1, srs2)).first->second.get();
}
return itr->second.get();
}
void Map::add_layer(layer const& l) void Map::add_layer(layer const& l)
{ {
init_proj_transform(srs_, l.srs()); init_proj_transform(srs_, l.srs());
@ -366,7 +352,6 @@ void Map::remove_all()
fontsets_.clear(); fontsets_.clear();
font_file_mapping_.clear(); font_file_mapping_.clear();
font_memory_cache_.clear(); font_memory_cache_.clear();
proj_cache_.clear();
} }
layer const& Map::get_layer(size_t index) const layer const& Map::get_layer(size_t index) const
@ -551,7 +536,7 @@ void Map::zoom_all()
if (layer.active()) if (layer.active())
{ {
std::string const& layer_srs = layer.srs(); std::string const& layer_srs = layer.srs();
proj_transform * proj_trans_ptr = get_proj_transform(srs_, layer_srs);; proj_transform const* proj_trans_ptr = proj_cache_->get(srs_, layer_srs);;
box2d<double> layer_ext = layer.envelope(); box2d<double> layer_ext = layer.envelope();
if (proj_trans_ptr->backward(layer_ext, PROJ_ENVELOPE_POINTS)) if (proj_trans_ptr->backward(layer_ext, PROJ_ENVELOPE_POINTS))
{ {
@ -731,7 +716,7 @@ featureset_ptr Map::query_point(unsigned index, double x, double y) const
mapnik::datasource_ptr ds = layer.datasource(); mapnik::datasource_ptr ds = layer.datasource();
if (ds) if (ds)
{ {
proj_transform * proj_trans_ptr = get_proj_transform(layer.srs(), srs_); proj_transform const* proj_trans_ptr = proj_cache_->get(layer.srs(), srs_);
double z = 0; double z = 0;
if (!proj_trans_ptr->equal() && !proj_trans_ptr->backward(x,y,z)) if (!proj_trans_ptr->equal() && !proj_trans_ptr->backward(x,y,z))
{ {
@ -794,18 +779,14 @@ void Map::set_extra_parameters(parameters& params)
extra_params_ = params; extra_params_ = params;
} }
mapnik::proj_transform const* Map::get_proj_transform(std::string const& source, std::string const& dest) const
{
return proj_cache_->get(source, dest);
}
void Map::init_proj_transform(std::string const& source, std::string const& dest) void Map::init_proj_transform(std::string const& source, std::string const& dest)
{ {
compatible_key_type key = std::make_pair<boost::string_view, boost::string_view>(source, dest); proj_cache_->init(source, dest);
auto itr = proj_cache_.find(key, compatible_hash{}, compatible_predicate{});
if (itr == proj_cache_.end())
{
mapnik::projection p0(source, true);
mapnik::projection p1(dest, true);
proj_cache_.emplace(std::make_pair(source, dest),
std::make_unique<proj_transform>(p0, p1));
}
} }
void Map::init_proj_transforms() void Map::init_proj_transforms()

View file

@ -0,0 +1,58 @@
/*****************************************************************************
*
* This file is part of Mapnik (c++ mapping toolkit)
*
* Copyright (C) 2021 Artem Pavlenko
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*****************************************************************************/
#include <mapnik/proj_transform_cache.hpp>
namespace mapnik {
thread_local proj_transform_cache::cache_type proj_transform_cache::cache_ = cache_type();
void proj_transform_cache::init(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 = cache_.find(key, compatible_hash{}, compatible_predicate{});
if (itr == cache_.end())
{
mapnik::projection p0(source, true);
mapnik::projection p1(dest, true);
cache_.emplace(std::make_pair(source, dest),
std::make_unique<proj_transform>(p0, p1));
}
}
proj_transform const* proj_transform_cache::get(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 = cache_.find(key, compatible_hash{}, compatible_predicate{});
if (itr == cache_.end())
{
mapnik::projection srs1(source, true);
mapnik::projection srs2(dest, true);
return cache_.emplace(std::make_pair(source, dest),
std::make_unique<proj_transform>(srs1, srs2)).first->second.get();
}
return itr->second.get();
}
} // namespace mapnik

View file

@ -8,7 +8,6 @@
#include <mapnik/expression.hpp> #include <mapnik/expression.hpp>
#include <mapnik/layer.hpp> #include <mapnik/layer.hpp>
#include <mapnik/rule.hpp> #include <mapnik/rule.hpp>
#include <mapnik/feature_style_processor.hpp>
#include <mapnik/feature_style_processor_impl.hpp> #include <mapnik/feature_style_processor_impl.hpp>
#include <mapnik/value/types.hpp> #include <mapnik/value/types.hpp>
#include <mapnik/symbolizer.hpp> #include <mapnik/symbolizer.hpp>