diff --git a/include/osm_lua_processing.h b/include/osm_lua_processing.h index c111c00b..66fe8872 100644 --- a/include/osm_lua_processing.h +++ b/include/osm_lua_processing.h @@ -92,7 +92,7 @@ class OsmLuaProcessing { bool Holds(const std::string& key) const; // Get an OSM tag for a given key (or return empty string if none) - std::string Find(const std::string& key) const; + const std::string& Find(const std::string& key) const; // ---- Spatial queries called from Lua @@ -249,7 +249,7 @@ class OsmLuaProcessing { class LayerDefinition &layers; std::deque> outputs; ///< All output objects that have been created - boost::container::flat_map currentTags; + const boost::container::flat_map* currentTags; }; #endif //_OSM_LUA_PROCESSING_H diff --git a/include/read_pbf.h b/include/read_pbf.h index 89077a6e..7761dc58 100644 --- a/include/read_pbf.h +++ b/include/read_pbf.h @@ -37,6 +37,7 @@ class PbfReader using tag_map_t = boost::container::flat_map; template void readTags(T &pbfObject, PrimitiveBlock const &pb, tag_map_t &tags) { + tags.reserve(pbfObject.keys_size()); auto keysPtr = pbfObject.mutable_keys(); auto valsPtr = pbfObject.mutable_vals(); for (uint n=0; n < pbfObject.keys_size(); n++) { diff --git a/src/osm_lua_processing.cpp b/src/osm_lua_processing.cpp index d368ed8d..0137851d 100644 --- a/src/osm_lua_processing.cpp +++ b/src/osm_lua_processing.cpp @@ -7,6 +7,7 @@ using namespace std; thread_local kaguya::State *g_luaState = nullptr; bool supportsRemappingShapefiles = false; +const std::string EMPTY_STRING = ""; int lua_error_handler(int errCode, const char *errMessage) { @@ -30,6 +31,7 @@ OsmLuaProcessing::OsmLuaProcessing( osmMemTiles(osmMemTiles), attributeStore(attributeStore), config(configIn), + currentTags(NULL), layers(layers) { // ---- Initialise Lua @@ -114,13 +116,13 @@ string OsmLuaProcessing::Id() const { // Check if there's a value for a given key bool OsmLuaProcessing::Holds(const string& key) const { - return currentTags.find(key) != currentTags.end(); + return currentTags->find(key) != currentTags->end(); } // Get an OSM tag for a given key (or return empty string if none) -string OsmLuaProcessing::Find(const string& key) const { - auto it = currentTags.find(key); - if(it == currentTags.end()) return ""; +const string& OsmLuaProcessing::Find(const string& key) const { + auto it = currentTags->find(key); + if(it == currentTags->end()) return EMPTY_STRING; return it->second; } @@ -546,7 +548,7 @@ bool OsmLuaProcessing::scanRelation(WayID id, const tag_map_t &tags) { originalOsmID = id; isWay = false; isRelation = true; - currentTags = tags; + currentTags = &tags; try { luaState["relation_scan_function"](this); } catch(luaProcessingException &e) { @@ -568,7 +570,7 @@ void OsmLuaProcessing::setNode(NodeID id, LatpLon node, const tag_map_t &tags) { isRelation = false; lon = node.lon; latp= node.latp; - currentTags = tags; + currentTags = &tags; //Start Lua processing for node try { @@ -617,7 +619,7 @@ void OsmLuaProcessing::setWay(WayID wayId, LatpLonVec const &llVec, const tag_ma throw std::out_of_range(ss.str()); } - currentTags = tags; + currentTags = &tags; bool ok = true; if (ok) { @@ -705,7 +707,7 @@ void OsmLuaProcessing::setRelation(int64_t relationId, WayVec const &outerWayVec llVecPtr = nullptr; outerWayVecPtr = &outerWayVec; innerWayVecPtr = &innerWayVec; - currentTags = tags; + currentTags = &tags; // Start Lua processing for relation if (!isNativeMP && !supportsWritingRelations) return; diff --git a/src/read_pbf.cpp b/src/read_pbf.cpp index bd800398..5b1795cb 100644 --- a/src/read_pbf.cpp +++ b/src/read_pbf.cpp @@ -44,12 +44,14 @@ bool PbfReader::ReadNodes(OsmLuaProcessing &output, PrimitiveGroup &pg, Primitiv } kvPos++; } - // For tagged nodes, call Lua, then save the OutputObject - boost::container::flat_map tags; nodes.push_back(std::make_pair(static_cast(nodeId), node)); if (significant) { + // For tagged nodes, call Lua, then save the OutputObject + boost::container::flat_map tags; + tags.reserve(kvPos / 2); + for (uint n=kvStart; n