From afc7096329f30244eca4ad8cab0ed37ad691ef7d Mon Sep 17 00:00:00 2001 From: myk002 Date: Fri, 10 Sep 2021 20:16:37 -0700 Subject: [PATCH] use vector instead of map for great memory savings --- plugins/blueprint.cpp | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/plugins/blueprint.cpp b/plugins/blueprint.cpp index 49d0da5d2..816d2f12d 100644 --- a/plugins/blueprint.cpp +++ b/plugins/blueprint.cpp @@ -630,12 +630,9 @@ static bool get_filename(string &fname, return true; } -typedef map bp_row; -typedef map bp_area; -typedef map bp_volume; - -static const bp_area NEW_AREA; -static const bp_row NEW_ROW; +typedef vector bp_row; // index is x coordinate +typedef map bp_area; // key is y coordinate +typedef map bp_volume; // key is z coordinate typedef const char * (get_tile_fn)(const df::coord &pos, const tile_context &ctx); @@ -666,11 +663,15 @@ static void write_minimal(ofstream &ofile, const blueprint_options &opts, for (auto row : area.second) { for ( ; yprev < row.first; ++yprev) ofile << endl; - int16_t xprev = 0; - for (auto tile : row.second) { - for ( ; xprev < tile.first; ++xprev) + size_t xprev = 0; + auto &tiles = row.second; + size_t rowsize = tiles.size(); + for (size_t x = 0; x < rowsize; ++x) { + if (!tiles[x]) + continue; + for ( ; xprev < x; ++xprev) ofile << ","; - ofile << tile.second; + ofile << tiles[x]; } } ofile << endl; @@ -692,7 +693,7 @@ static void write_pretty(ofstream &ofile, const blueprint_options &opts, row = &area->at(y); for (int16_t x = 0; x < opts.width; ++x) { const char *tile = NULL; - if (row && row->count(x)) + if (row) tile = row->at(x); ofile << (tile ? tile : " ") << ","; } @@ -742,6 +743,10 @@ static bool do_transform(color_ostream &out, const df::coord &start, const df::coord &end, const blueprint_options &opts, vector &filenames) { + // empty map instances to pass to emplace() below + static const bp_area EMPTY_AREA; + static const bp_row EMPTY_ROW; + vector processors; if (opts.auto_phase || opts.dig) @@ -779,10 +784,13 @@ static bool do_transform(color_ostream &out, if (tile_str) { // ensure our z-index is in the order we want to write auto area = processor.mapdata.emplace(abs(z - start.z), - NEW_AREA); + EMPTY_AREA); auto row = area.first->second.emplace(y - start.y, - NEW_ROW); - row.first->second[x - start.x] = tile_str; + EMPTY_ROW); + auto &tiles = row.first->second; + if (row.second) + tiles.resize(opts.width); + tiles[x - start.x] = tile_str; } } }