From 427773d31a5b1ad156b923aaf44ea9d2015f5be9 Mon Sep 17 00:00:00 2001 From: Bruno de Oliveira Abinader Date: Thu, 30 Jun 2016 13:55:14 +0300 Subject: [core] Micro-optimizations in geometry code --- src/mbgl/style/source_impl.cpp | 11 +++++++++-- src/mbgl/style/style.cpp | 5 ++++- src/mbgl/util/grid_index.cpp | 17 ++++++++++------- 3 files changed, 23 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/mbgl/style/source_impl.cpp b/src/mbgl/style/source_impl.cpp index 4223f2b50d..95363ca699 100644 --- a/src/mbgl/style/source_impl.cpp +++ b/src/mbgl/style/source_impl.cpp @@ -207,6 +207,11 @@ static Point coordinateToTilePoint(const UnwrappedTileID& tileID, const } std::unordered_map> Source::Impl::queryRenderedFeatures(const QueryParameters& parameters) const { + std::unordered_map> result; + if (renderTiles.empty()) { + return result; + } + LineString queryGeometry; for (const auto& p : parameters.geometry) { @@ -214,9 +219,11 @@ std::unordered_map> Source::Impl::queryRendere parameters.transformState, 0, { p.x, parameters.transformState.getHeight() - p.y }).p); } - mapbox::geometry::box box = mapbox::geometry::envelope(queryGeometry); + if (queryGeometry.empty()) { + return result; + } - std::unordered_map> result; + mapbox::geometry::box box = mapbox::geometry::envelope(queryGeometry); for (const auto& tilePtr : renderTiles) { const RenderTile& tile = tilePtr.second; diff --git a/src/mbgl/style/style.cpp b/src/mbgl/style/style.cpp index 9073894cc2..aae0e38d51 100644 --- a/src/mbgl/style/style.cpp +++ b/src/mbgl/style/style.cpp @@ -337,6 +337,7 @@ RenderData Style::getRenderData(MapDebugOptions debugOptions) const { } std::vector Style::queryRenderedFeatures(const QueryParameters& parameters) const { + std::vector result; std::unordered_map> resultsByLayer; for (const auto& source : sources) { @@ -344,7 +345,9 @@ std::vector Style::queryRenderedFeatures(const QueryParameters& paramet std::move(sourceResults.begin(), sourceResults.end(), std::inserter(resultsByLayer, resultsByLayer.begin())); } - std::vector result; + if (resultsByLayer.empty()) { + return result; + } // Combine all results based on the style layer order. for (const auto& layer : layers) { diff --git a/src/mbgl/util/grid_index.cpp b/src/mbgl/util/grid_index.cpp index cf1fcd68c9..b3afd3fdc8 100644 --- a/src/mbgl/util/grid_index.cpp +++ b/src/mbgl/util/grid_index.cpp @@ -1,5 +1,6 @@ #include #include +#include #include @@ -28,9 +29,10 @@ void GridIndex::insert(T&& t, const BBox& bbox) { auto cx2 = convertToCellCoord(bbox.max.x); auto cy2 = convertToCellCoord(bbox.max.y); - for (int32_t x = cx1; x <= cx2; x++) { - for (int32_t y = cy1; y <= cy2; y++) { - auto cellIndex = d * y + x; + int32_t x, y, cellIndex; + for (x = cx1; x <= cx2; ++x) { + for (y = cy1; y <= cy2; ++y) { + cellIndex = d * y + x; cells[cellIndex].push_back(uid); } } @@ -48,9 +50,10 @@ std::vector GridIndex::query(const BBox& queryBBox) const { auto cx2 = convertToCellCoord(queryBBox.max.x); auto cy2 = convertToCellCoord(queryBBox.max.y); - for (int32_t x = cx1; x <= cx2; x++) { - for (int32_t y = cy1; y <= cy2; y++) { - auto cellIndex = d * y + x; + int32_t x, y, cellIndex; + for (x = cx1; x <= cx2; ++x) { + for (y = cy1; y <= cy2; ++y) { + cellIndex = d * y + x; for (auto uid : cells[cellIndex]) { if (seenUids.count(uid) == 0) { seenUids.insert(uid); @@ -75,7 +78,7 @@ std::vector GridIndex::query(const BBox& queryBBox) const { template int32_t GridIndex::convertToCellCoord(int32_t x) const { - return std::max(0.0, std::min(d - 1.0, std::floor(x * scale) + padding)); + return util::max(0.0, util::min(d - 1.0, std::floor(x * scale) + padding)); } template class GridIndex; -- cgit v1.2.1