Skip to content

Commit

Permalink
Lazily initialize layer key and values maps when actually needed
Browse files Browse the repository at this point in the history
  • Loading branch information
e-n-f committed Jan 24, 2024
1 parent b91908c commit 4a2e668
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions mvt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,13 +278,6 @@ bool mvt_tile::decode(std::string &message, bool &was_compressed) {
}
}

for (size_t i = 0; i < layer.keys.size(); i++) {
layer.key_map.insert(std::pair<std::string, size_t>(layer.keys[i], i));
}
for (size_t i = 0; i < layer.values.size(); i++) {
layer.value_map.insert(std::pair<mvt_value, size_t>(layer.values[i], i));
}

layers.push_back(layer);
break;
}
Expand Down Expand Up @@ -593,6 +586,18 @@ std::string mvt_value::toString() const {
void mvt_layer::tag(mvt_feature &feature, std::string key, mvt_value value) {
size_t ko, vo;

// initialize lazily the first time anyone tags an attribute
// to save the time of doing it in decode, which never actually matters.
// only tile writers actually need this.
if (key_map.size() == 0) {
for (size_t i = 0; i < keys.size(); i++) {
key_map.insert(std::pair<std::string, size_t>(keys[i], i));
}
for (size_t i = 0; i < values.size(); i++) {
value_map.insert(std::pair<mvt_value, size_t>(values[i], i));
}
}

std::unordered_map<std::string, size_t>::iterator ki = key_map.find(key);
std::unordered_map<mvt_value, size_t>::iterator vi = value_map.find(value);

Expand Down

0 comments on commit 4a2e668

Please sign in to comment.