Skip to content

Commit

Permalink
Reduce tile-join overzooming memory usage (#162)
Browse files Browse the repository at this point in the history
* 16 bits is enough for tile numbers

* Revert "16 bits is enough for tile numbers"

This reverts commit 71a0c4e.

* Check what child tiles each overzoomed tile will have

and don't queue further overzooming of empty tiles

* Clean up naming; use std::move to avoid copying large arrays
  • Loading branch information
e-n-f authored Nov 8, 2023
1 parent 2c56187 commit d359461
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 2.37.0

* Speed up tile-join overzooming and make it use less memory, by not including empty child tiles in the enumeration

# 2.36.0

* Make tile-join distrust the source tilesets' metadata maxzoom and minzoom
Expand Down
30 changes: 27 additions & 3 deletions clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,8 @@ static std::vector<std::pair<double, double>> clip_poly1(std::vector<std::pair<d
}

std::string overzoom(std::string s, int oz, int ox, int oy, int nz, int nx, int ny,
int detail, int buffer, std::set<std::string> const &keep, bool do_compress) {
int detail, int buffer, std::set<std::string> const &keep, bool do_compress,
std::vector<std::pair<unsigned, unsigned>> *next_overzoomed_tiles) {
mvt_tile tile;

try {
Expand All @@ -767,11 +768,12 @@ std::string overzoom(std::string s, int oz, int ox, int oy, int nz, int nx, int
exit(EXIT_PROTOBUF);
}

return overzoom(tile, oz, ox, oy, nz, nx, ny, detail, buffer, keep, do_compress);
return overzoom(tile, oz, ox, oy, nz, nx, ny, detail, buffer, keep, do_compress, next_overzoomed_tiles);
}

std::string overzoom(mvt_tile tile, int oz, int ox, int oy, int nz, int nx, int ny,
int detail, int buffer, std::set<std::string> const &keep, bool do_compress) {
int detail, int buffer, std::set<std::string> const &keep, bool do_compress,
std::vector<std::pair<unsigned, unsigned>> *next_overzoomed_tiles) {
mvt_tile outtile;

for (auto const &layer : tile.layers) {
Expand Down Expand Up @@ -890,6 +892,28 @@ std::string overzoom(mvt_tile tile, int oz, int ox, int oy, int nz, int nx, int
}
}

if (next_overzoomed_tiles != NULL) {
// will any child tiles have features in them?
// find out recursively from the tile we just made.
//
// (yes, we should keep them instead of remaking them
// later, but that first requires figuring out where to
// keep them.)

if (outtile.layers.size() > 0) {
for (size_t x = 0; x < 2; x++) {
for (size_t y = 0; y < 2; y++) {
std::string child = overzoom(outtile, nz, nx, ny,
nz + 1, nx * 2 + x, ny * 2 + y,
detail, buffer, keep, false, NULL);
if (child.size() > 0) {
next_overzoomed_tiles->emplace_back(nx * 2 + x, ny * 2 + y);
}
}
}
}
}

if (outtile.layers.size() > 0) {
std::string pbf = outtile.encode();

Expand Down
6 changes: 4 additions & 2 deletions geometry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,11 @@ int pnpoly(const drawvec &vert, size_t start, size_t nvert, long long testx, lon
double distance_from_line(long long point_x, long long point_y, long long segA_x, long long segA_y, long long segB_x, long long segB_y);

std::string overzoom(mvt_tile tile, int oz, int ox, int oy, int nz, int nx, int ny,
int detail, int buffer, std::set<std::string> const &keep, bool do_compress);
int detail, int buffer, std::set<std::string> const &keep, bool do_compress,
std::vector<std::pair<unsigned, unsigned>> *next_overzoomed_tiles);

std::string overzoom(std::string s, int oz, int ox, int oy, int nz, int nx, int ny,
int detail, int buffer, std::set<std::string> const &keep, bool do_compress);
int detail, int buffer, std::set<std::string> const &keep, bool do_compress,
std::vector<std::pair<unsigned, unsigned>> *next_overzoomed_tiles);

#endif
2 changes: 1 addition & 1 deletion overzoom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ int main(int argc, char **argv) {
exit(EXIT_FAILURE);
}

std::string out = overzoom(tile, oz, ox, oy, nz, nx, ny, detail, buffer, keep, true);
std::string out = overzoom(tile, oz, ox, oy, nz, nx, ny, detail, buffer, keep, true, NULL);
fwrite(out.c_str(), sizeof(char), out.size(), f);
fclose(f);

Expand Down
25 changes: 19 additions & 6 deletions tile-join.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,8 @@ struct tileset_reader {
// for overzooming
int maxzoom_so_far = -1;
std::vector<std::pair<unsigned, unsigned>> tiles_at_maxzoom_so_far;
std::vector<std::pair<unsigned, unsigned>> overzoomed_tiles;
std::vector<std::pair<unsigned, unsigned>> overzoomed_tiles; // tiles at `zoom`
std::vector<std::pair<unsigned, unsigned>> next_overzoomed_tiles; // tiles at `zoom + 1`
bool overzoom_consumed_at_this_zoom = false;

// parent tile cache
Expand Down Expand Up @@ -642,12 +643,24 @@ struct tileset_reader {

long long scale = (1LL << zoom) / (1LL << maxzoom_so_far);

for (auto const &xy : tiles_at_maxzoom_so_far) {
for (long long xx = 0; xx < scale; xx++) {
for (long long yy = 0; yy < scale; yy++) {
overzoomed_tiles.push_back(std::pair<unsigned, unsigned>(xy.first * scale + xx, xy.second * scale + yy));
// If this is the first overzoomed level, we don't know yet
// which tiles will be useful, so spell out all 4 child tiles
// from each parent tile.
//
// If it is further overzoomed than that, we have a list of
// which child tiles will have features in them, so use that.

if (zoom == maxzoom_so_far + 1) {
for (auto const &xy : tiles_at_maxzoom_so_far) {
for (long long xx = 0; xx < scale; xx++) {
for (long long yy = 0; yy < scale; yy++) {
overzoomed_tiles.push_back(std::pair<unsigned, unsigned>(xy.first * scale + xx, xy.second * scale + yy));
}
}
}
} else {
overzoomed_tiles = std::move(next_overzoomed_tiles);
next_overzoomed_tiles.clear();
}

std::sort(overzoomed_tiles.begin(), overzoomed_tiles.end(), tilecmp);
Expand Down Expand Up @@ -769,7 +782,7 @@ struct tileset_reader {
}

if (source.layers.size() != 0) {
std::string ret = overzoom(source, parent_tile.z, parent_tile.x, parent_tile.y, tile.z, tile.x, tile.y, -1, buffer, std::set<std::string>(), false);
std::string ret = overzoom(source, parent_tile.z, parent_tile.x, parent_tile.y, tile.z, tile.x, tile.y, -1, buffer, std::set<std::string>(), false, &next_overzoomed_tiles);
return ret;
}

Expand Down
2 changes: 1 addition & 1 deletion version.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#ifndef VERSION_HPP
#define VERSION_HPP

#define VERSION "v2.36.0"
#define VERSION "v2.37.0"

#endif

0 comments on commit d359461

Please sign in to comment.