From 27e2577f1fc56fa42919071a49863bba8b72fe7e Mon Sep 17 00:00:00 2001 From: Jochen Topf Date: Fri, 30 May 2025 20:27:34 +0200 Subject: [PATCH 1/2] Use smaller ints as basis for some enum classes And reorder class members in one case to make the memory footprint smaller. --- src/middle-pgsql.cpp | 6 +++--- src/output-flex.hpp | 2 +- src/taginfo.hpp | 3 ++- tests/test-output-flex-nodes.cpp | 2 +- 4 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/middle-pgsql.cpp b/src/middle-pgsql.cpp index 7bae3ec67..8aa2194ea 100644 --- a/src/middle-pgsql.cpp +++ b/src/middle-pgsql.cpp @@ -235,18 +235,18 @@ class member_list_json_builder } private: - osmium::builder::RelationMemberListBuilder *m_builder; std::string m_role; + osmium::builder::RelationMemberListBuilder *m_builder; osmium::object_id_type m_ref = 0; osmium::item_type m_type = osmium::item_type::undefined; - enum class next_val + enum class next_val : std::uint8_t { none, type, ref, role } m_next_val = next_val::none; -}; +}; // class member_list_json_builder template void pgsql_parse_json_members(char const *string, diff --git a/src/output-flex.hpp b/src/output-flex.hpp index c803d3a30..23dceab08 100644 --- a/src/output-flex.hpp +++ b/src/output-flex.hpp @@ -40,7 +40,7 @@ struct options_t; * When C++ code is called from the Lua code we sometimes need to know * in what context this happens. These are the possible contexts. */ -enum class calling_context +enum class calling_context : std::uint8_t { main = 0, ///< In main context, i.e. the Lua script outside any callbacks process_node = 1, ///< In the process_node() callback diff --git a/src/taginfo.hpp b/src/taginfo.hpp index 75fcbefb0..75977541e 100644 --- a/src/taginfo.hpp +++ b/src/taginfo.hpp @@ -10,10 +10,11 @@ * For a full list of authors see the git log. */ +#include #include #include -enum class ColumnType +enum class ColumnType : std::uint8_t { INT, REAL, diff --git a/tests/test-output-flex-nodes.cpp b/tests/test-output-flex-nodes.cpp index 80d9ba732..3dae49be8 100644 --- a/tests/test-output-flex-nodes.cpp +++ b/tests/test-output-flex-nodes.cpp @@ -53,7 +53,7 @@ TEST_CASE("add nodes") CHECK(1 == conn.get_count("osm2pgsql_test_t1", "node_id = 17")); } -enum class node_relationship +enum class node_relationship : std::uint8_t { none, in_way, From dde38e0520a5e7ec2b32ee5a114ae626ccdb0345 Mon Sep 17 00:00:00 2001 From: Jochen Topf Date: Sat, 31 May 2025 09:50:33 +0200 Subject: [PATCH 2/2] Remove use of enum for constants This was a useful trick back in the days, but in C++17 it is not needed any more. The constexpr expression is more clear in its intent. See for instance here: https://stackoverflow.com/questions/37259807/static-constexpr-int-vs-old-fashioned-enum-when-and-why --- src/db-copy.hpp | 57 ++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/src/db-copy.hpp b/src/db-copy.hpp index 6a6e46bd1..f843aad8a 100644 --- a/src/db-copy.hpp +++ b/src/db-copy.hpp @@ -15,6 +15,7 @@ #include "pgsql-params.hpp" #include +#include #include #include #include @@ -74,12 +75,11 @@ class db_target_descr_t */ class db_deleter_by_id_t { - enum - { - // There is a trade-off here between sending as few DELETE SQL as - // possible and keeping the size of the deletable vector managable. - Max_entries = 1000000 - }; + /** + * There is a trade-off here between sending as few DELETE SQL as + * possible and keeping the size of the deletable vector managable. + */ + static constexpr std::size_t Max_entries = 1000000; public: bool has_data() const noexcept { return !m_deletables.empty(); } @@ -101,12 +101,11 @@ class db_deleter_by_id_t */ class db_deleter_by_type_and_id_t { - enum - { - // There is a trade-off here between sending as few DELETE SQL as - // possible and keeping the size of the deletable vector managable. - Max_entries = 1000000 - }; + /** + * There is a trade-off here between sending as few DELETE SQL as + * possible and keeping the size of the deletable vector managable. + */ + static constexpr std::size_t Max_entries = 1000000; struct item_t { @@ -140,23 +139,23 @@ class db_deleter_by_type_and_id_t struct db_cmd_copy_t { - enum - { - /** Size of a single buffer with COPY data for Postgresql. - * This is a trade-off between memory usage and sending large chunks - * to speed up processing. Currently a one-size fits all value. - * Needs more testing and individual values per queue. - */ - Max_buf_size = 10 * 1024 * 1024, - /** Maximum length of the queue with COPY data. - * In the usual case, PostgreSQL should be faster processing the - * data than it can be produced and there should only be one element - * in the queue. If PostgreSQL is slower, then the queue will always - * be full and it is better to keep the queue smaller to reduce memory - * usage. Current value is just assumed to be a reasonable trade off. - */ - Max_buffers = 10 - }; + /** + * Size of a single buffer with COPY data for Postgresql. + * This is a trade-off between memory usage and sending large chunks + * to speed up processing. Currently a one-size fits all value. + * Needs more testing and individual values per queue. + */ + static constexpr std::size_t Max_buf_size = 10 * 1024 * 1024; + + /** + * Maximum length of the queue with COPY data. + * In the usual case, PostgreSQL should be faster processing the + * data than it can be produced and there should only be one element + * in the queue. If PostgreSQL is slower, then the queue will always + * be full and it is better to keep the queue smaller to reduce memory + * usage. Current value is just assumed to be a reasonable trade off. + */ + static constexpr std::size_t Max_buffers = 10; /// Name of the target table for the copy operation std::shared_ptr target;