Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

Commit 273ad43

Browse files
committed
[core] Avoid copying feature properties
1 parent e6e1e15 commit 273ad43

9 files changed

Lines changed: 19 additions & 9 deletions

File tree

src/mbgl/layout/symbol_feature.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class SymbolFeature : public GeometryTileFeature {
1818

1919
FeatureType getType() const override { return feature->getType(); }
2020
optional<Value> getValue(const std::string& key) const override { return feature->getValue(key); };
21-
std::unordered_map<std::string,Value> getProperties() const override { return feature->getProperties(); };
21+
const PropertyMap& getProperties() const override { return feature->getProperties(); };
2222
FeatureIdentifier getID() const override { return feature->getID(); };
2323
const GeometryCollection& getGeometries() const override { return geometry; };
2424

src/mbgl/style/expression/compound_expression.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,8 @@ const auto& propertiesCompoundExpression() {
423423
};
424424
}
425425
std::unordered_map<std::string, Value> result;
426-
const PropertyMap properties = params.feature->getProperties();
426+
const PropertyMap& properties = params.feature->getProperties();
427+
result.reserve(properties.size());
427428
for (const auto& entry : properties) {
428429
result[entry.first] = toExpressionValue(entry.second);
429430
}

src/mbgl/style/expression/expression.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class GeoJSONFeature : public GeometryTileFeature {
1515
FeatureType getType() const override {
1616
return apply_visitor(ToFeatureType(), feature.geometry);
1717
}
18-
PropertyMap getProperties() const override { return feature.properties; }
18+
const PropertyMap& getProperties() const override { return feature.properties; }
1919
FeatureIdentifier getID() const override { return feature.id; }
2020
optional<mbgl::Value> getValue(const std::string& key) const override {
2121
auto it = feature.properties.find(key);

src/mbgl/tile/geojson_tile_data.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class GeoJSONTileFeature : public GeometryTileFeature {
1717
return apply_visitor(ToFeatureType(), feature.geometry);
1818
}
1919

20-
PropertyMap getProperties() const override {
20+
const PropertyMap& getProperties() const override {
2121
return feature.properties;
2222
}
2323

src/mbgl/tile/geometry_tile_data.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,11 @@ Feature convertFeature(const GeometryTileFeature& geometryTileFeature, const Can
179179
return feature;
180180
}
181181

182+
const PropertyMap& GeometryTileFeature::getProperties() const {
183+
static const PropertyMap dummy;
184+
return dummy;
185+
}
186+
182187
const GeometryCollection& GeometryTileFeature::getGeometries() const {
183188
static const GeometryCollection dummy;
184189
return dummy;

src/mbgl/tile/geometry_tile_data.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class GeometryTileFeature {
4949
virtual ~GeometryTileFeature() = default;
5050
virtual FeatureType getType() const = 0;
5151
virtual optional<Value> getValue(const std::string& key) const = 0;
52-
virtual PropertyMap getProperties() const { return PropertyMap(); }
52+
virtual const PropertyMap& getProperties() const;
5353
virtual FeatureIdentifier getID() const { return NullValue {}; }
5454
virtual const GeometryCollection& getGeometries() const;
5555
};

src/mbgl/tile/vector_tile_data.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ optional<Value> VectorTileFeature::getValue(const std::string& key) const {
2626
return value->is<NullValue>() ? nullopt : std::move(value);
2727
}
2828

29-
std::unordered_map<std::string, Value> VectorTileFeature::getProperties() const {
30-
return feature.getProperties();
29+
const PropertyMap& VectorTileFeature::getProperties() const {
30+
if (!properties) {
31+
properties = feature.getProperties();
32+
}
33+
return *properties;
3134
}
3235

3336
FeatureIdentifier VectorTileFeature::getID() const {

src/mbgl/tile/vector_tile_data.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@ class VectorTileFeature : public GeometryTileFeature {
1515

1616
FeatureType getType() const override;
1717
optional<Value> getValue(const std::string& key) const override;
18-
std::unordered_map<std::string, Value> getProperties() const override;
18+
const PropertyMap& getProperties() const override;
1919
FeatureIdentifier getID() const override;
2020
const GeometryCollection& getGeometries() const override;
2121

2222
private:
2323
mapbox::vector_tile::feature feature;
2424
mutable optional<GeometryCollection> lines;
25+
mutable optional<PropertyMap> properties;
2526
};
2627

2728
class VectorTileLayer : public GeometryTileLayer {

test/tile/vector_tile.test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ TEST(VectorTileData, ParseResults) {
9898
ASSERT_TRUE(feature->getID().is<uint64_t>());
9999
ASSERT_EQ(feature->getID().get<uint64_t>(), 1u);
100100

101-
std::unordered_map<std::string, Value> properties = feature->getProperties();
101+
const std::unordered_map<std::string, Value>& properties = feature->getProperties();
102102
ASSERT_EQ(properties.size(), 3u);
103103
ASSERT_EQ(properties.at("disputed"), *feature->getValue("disputed"));
104104

0 commit comments

Comments
 (0)