Skip to content

Commit

Permalink
Avoid copying strings/tag maps (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
cldellow authored Nov 12, 2023
1 parent 8c059d9 commit 8b45a8a
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
4 changes: 2 additions & 2 deletions include/osm_lua_processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -249,7 +249,7 @@ class OsmLuaProcessing {
class LayerDefinition &layers;

std::deque<std::pair<OutputObjectRef, AttributeStoreRef>> outputs; ///< All output objects that have been created
boost::container::flat_map<std::string, std::string> currentTags;
const boost::container::flat_map<std::string, std::string>* currentTags;
};

#endif //_OSM_LUA_PROCESSING_H
1 change: 1 addition & 0 deletions include/read_pbf.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class PbfReader
using tag_map_t = boost::container::flat_map<std::string, std::string>;
template<typename T>
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++) {
Expand Down
18 changes: 10 additions & 8 deletions src/osm_lua_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -30,6 +31,7 @@ OsmLuaProcessing::OsmLuaProcessing(
osmMemTiles(osmMemTiles),
attributeStore(attributeStore),
config(configIn),
currentTags(NULL),
layers(layers) {

// ---- Initialise Lua
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 4 additions & 2 deletions src/read_pbf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::string> tags;

nodes.push_back(std::make_pair(static_cast<NodeID>(nodeId), node));

if (significant) {
// For tagged nodes, call Lua, then save the OutputObject
boost::container::flat_map<std::string, std::string> tags;
tags.reserve(kvPos / 2);

for (uint n=kvStart; n<kvPos-1; n+=2) {
tags[pb.stringtable().s(dense.keys_vals(n))] = pb.stringtable().s(dense.keys_vals(n+1));
}
Expand Down

0 comments on commit 8b45a8a

Please sign in to comment.