Skip to content

Commit b6d02a4

Browse files
committed
Use osmium::index::IdSetSmall instead of id_tracker in flex output
We can only iterate over an id_tracker object once, because it removes entries when reading. We will soon need to iterate more than once. Using the libosmium IdSetSmall also should save quite a bit of memory when using small sets of ids, so this is even better.
1 parent 36324d8 commit b6d02a4

File tree

2 files changed

+23
-24
lines changed

2 files changed

+23
-24
lines changed

src/output-flex.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ int output_flex_t::app_mark()
518518
osmium::object_id_type const id = luaL_checkinteger(lua_state(), 2);
519519

520520
if (type_name[0] == 'w') {
521-
m_stage2_ways_tracker->mark(id);
521+
m_stage2_way_ids->set(id);
522522
}
523523

524524
return 0;
@@ -1207,8 +1207,7 @@ output_flex_t::clone(std::shared_ptr<middle_query_t> const &mid,
12071207
{
12081208
return std::make_shared<output_flex_t>(
12091209
mid, *get_options(), copy_thread, true, m_lua_state, m_process_node,
1210-
m_process_way, m_process_relation, m_tables,
1211-
m_stage2_ways_tracker);
1210+
m_process_way, m_process_relation, m_tables, m_stage2_way_ids);
12121211
}
12131212

12141213
output_flex_t::output_flex_t(
@@ -1218,9 +1217,9 @@ output_flex_t::output_flex_t(
12181217
prepared_lua_function_t process_way,
12191218
prepared_lua_function_t process_relation,
12201219
std::shared_ptr<std::vector<flex_table_t>> tables,
1221-
std::shared_ptr<id_tracker> ways_tracker)
1220+
std::shared_ptr<idset_t> stage2_way_ids)
12221221
: output_t(mid, o), m_tables(std::move(tables)),
1223-
m_stage2_ways_tracker(std::move(ways_tracker)), m_copy_thread(copy_thread),
1222+
m_stage2_way_ids(std::move(stage2_way_ids)), m_copy_thread(copy_thread),
12241223
m_lua_state(std::move(lua_state)), m_builder(o.projection),
12251224
m_expire(o.expire_tiles_zoom, o.expire_tiles_max_bbox, o.projection),
12261225
m_buffer(32768, osmium::memory::Buffer::auto_grow::yes),
@@ -1325,7 +1324,7 @@ void output_flex_t::init_lua(std::string const &filename)
13251324

13261325
void output_flex_t::stage2_proc()
13271326
{
1328-
if (m_stage2_ways_tracker->empty()) {
1327+
if (m_stage2_way_ids->empty()) {
13291328
fmt::print(stderr, "Skipping stage 2 (no marked ways).\n");
13301329
return;
13311330
}
@@ -1358,19 +1357,19 @@ void output_flex_t::stage2_proc()
13581357
lua_setfield(lua_state(), -2, "stage");
13591358
lua_pop(lua_state(), 1); // osm2pgsql
13601359

1361-
osmid_t id;
1362-
13631360
fmt::print(stderr, "Entering stage 2 processing of {} ways...\n"_format(
1364-
m_stage2_ways_tracker->size()));
1361+
m_stage2_way_ids->size()));
13651362

1366-
while (id_tracker::is_valid((id = m_stage2_ways_tracker->pop_mark()))) {
1363+
m_stage2_way_ids->sort_unique();
1364+
for (osmid_t const id : *m_stage2_way_ids) {
13671365
m_buffer.clear();
13681366
if (!m_mid->way_get(id, m_buffer)) {
13691367
continue;
13701368
}
13711369
auto &way = m_buffer.get<osmium::Way>(0);
13721370
way_add(&way);
13731371
}
1372+
m_stage2_way_ids->clear();
13741373
}
13751374

13761375
void output_flex_t::merge_expire_trees(output_t *other)

src/output-flex.hpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
#include "flex-table.hpp"
88
#include "format.hpp"
99
#include "geom-transform.hpp"
10-
#include "id-tracker.hpp"
1110
#include "osmium-builder.hpp"
1211
#include "output.hpp"
1312
#include "table.hpp"
1413
#include "tagtransform.hpp"
1514

15+
#include <osmium/index/id_set.hpp>
1616
#include <osmium/osm/item_type.hpp>
1717

1818
extern "C"
@@ -26,6 +26,8 @@ extern "C"
2626
#include <utility>
2727
#include <vector>
2828

29+
using idset_t = osmium::index::IdSetSmall<osmid_t>;
30+
2931
/**
3032
* The flex output calls several user-defined Lua functions. They are
3133
* "prepared" by putting the function pointers on the Lua stack. Objects
@@ -58,18 +60,16 @@ class output_flex_t : public output_t
5860
{
5961

6062
public:
61-
output_flex_t(std::shared_ptr<middle_query_t> const &mid,
62-
options_t const &options,
63-
std::shared_ptr<db_copy_thread_t> const &copy_thread,
64-
bool is_clone = false,
65-
std::shared_ptr<lua_State> lua_state = nullptr,
66-
prepared_lua_function_t process_node = {},
67-
prepared_lua_function_t process_way = {},
68-
prepared_lua_function_t process_relation = {},
69-
std::shared_ptr<std::vector<flex_table_t>> tables =
70-
std::make_shared<std::vector<flex_table_t>>(),
71-
std::shared_ptr<id_tracker> ways_tracker =
72-
std::make_shared<id_tracker>());
63+
output_flex_t(
64+
std::shared_ptr<middle_query_t> const &mid, options_t const &options,
65+
std::shared_ptr<db_copy_thread_t> const &copy_thread,
66+
bool is_clone = false, std::shared_ptr<lua_State> lua_state = nullptr,
67+
prepared_lua_function_t process_node = {},
68+
prepared_lua_function_t process_way = {},
69+
prepared_lua_function_t process_relation = {},
70+
std::shared_ptr<std::vector<flex_table_t>> tables =
71+
std::make_shared<std::vector<flex_table_t>>(),
72+
std::shared_ptr<idset_t> stage2_way_ids = std::make_shared<idset_t>());
7373

7474
output_flex_t(output_flex_t const &) = delete;
7575
output_flex_t &operator=(output_flex_t const &) = delete;
@@ -164,7 +164,7 @@ class output_flex_t : public output_t
164164
std::shared_ptr<std::vector<flex_table_t>> m_tables;
165165
std::vector<table_connection_t> m_table_connections;
166166

167-
std::shared_ptr<id_tracker> m_stage2_ways_tracker;
167+
std::shared_ptr<idset_t> m_stage2_way_ids;
168168

169169
std::shared_ptr<db_copy_thread_t> m_copy_thread;
170170

0 commit comments

Comments
 (0)