diff --git a/src/command-line-parser.cpp b/src/command-line-parser.cpp index a89370d73..b1b7e0051 100644 --- a/src/command-line-parser.cpp +++ b/src/command-line-parser.cpp @@ -229,14 +229,16 @@ void check_options_expire(options_t *options) { // Zoom level 31 is the technical limit because we use 32-bit integers for // the x and y index of a tile ID. - if (options->expire_tiles_zoom_min > 31) { - options->expire_tiles_zoom_min = 31; + constexpr uint32_t ZOOM_MAX = 31; + + if (options->expire_tiles_zoom_min > ZOOM_MAX) { + options->expire_tiles_zoom_min = ZOOM_MAX; log_warn("Minimum zoom level for tile expiry is too " "large and has been set to 31."); } - if (options->expire_tiles_zoom > 31) { - options->expire_tiles_zoom = 31; + if (options->expire_tiles_zoom > ZOOM_MAX) { + options->expire_tiles_zoom = ZOOM_MAX; log_warn("Maximum zoom level for tile expiry is too " "large and has been set to 31."); } diff --git a/src/middle-ram.cpp b/src/middle-ram.cpp index 8fe2eff8b..e45a47a99 100644 --- a/src/middle-ram.cpp +++ b/src/middle-ram.cpp @@ -113,29 +113,29 @@ void middle_ram_t::stop() { assert(m_middle_state == middle_state::done); - auto const mbyte = 1024 * 1024; + constexpr auto MBYTE = 1024 * 1024; if (m_persistent_cache) { log_debug("Middle 'ram': Node locations on disk: size={} bytes={}M", m_persistent_cache->size(), - m_persistent_cache->used_memory() / mbyte); + m_persistent_cache->used_memory() / MBYTE); } else { log_debug("Middle 'ram': Node locations in memory: size={} bytes={}M", m_node_locations.size(), - m_node_locations.used_memory() / mbyte); + m_node_locations.used_memory() / MBYTE); } log_debug("Middle 'ram': Way nodes data: size={} capacity={} bytes={}M", m_way_nodes_data.size(), m_way_nodes_data.capacity(), - m_way_nodes_data.capacity() / mbyte); + m_way_nodes_data.capacity() / MBYTE); log_debug("Middle 'ram': Way nodes index: size={} capacity={} bytes={}M", m_way_nodes_index.size(), m_way_nodes_index.capacity(), - m_way_nodes_index.used_memory() / mbyte); + m_way_nodes_index.used_memory() / MBYTE); log_debug("Middle 'ram': Object data: size={} capacity={} bytes={}M", m_object_buffer.committed(), m_object_buffer.capacity(), - m_object_buffer.capacity() / mbyte); + m_object_buffer.capacity() / MBYTE); std::size_t index_size = 0; std::size_t index_capacity = 0; @@ -146,13 +146,13 @@ void middle_ram_t::stop() index_mem += index.used_memory(); } log_debug("Middle 'ram': Object indexes: size={} capacity={} bytes={}M", - index_size, index_capacity, index_mem / mbyte); + index_size, index_capacity, index_mem / MBYTE); log_debug("Middle 'ram': Memory used overall: {}MBytes", (m_node_locations.used_memory() + m_way_nodes_data.capacity() + m_way_nodes_index.used_memory() + m_object_buffer.capacity() + index_mem) / - mbyte); + MBYTE); m_node_locations.clear(); diff --git a/src/tile.cpp b/src/tile.cpp index 87d45f66b..ff6592abd 100644 --- a/src/tile.cpp +++ b/src/tile.cpp @@ -36,7 +36,8 @@ geom::point_t tile_t::to_world_coords(geom::point_t p, geom::point_t tile_t::center() const noexcept { - return to_world_coords({0.5, 0.5}, 1); + constexpr double MIDDLE = 0.5; + return to_world_coords({MIDDLE, MIDDLE}, 1); } namespace { @@ -44,6 +45,8 @@ namespace { // Quadkey implementation uses bit interleaving code from // https://github.com/lemire/Code-used-on-Daniel-Lemire-s-blog/blob/master/2018/01/08/interleave.c +// NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) + uint64_t interleave_uint32_with_zeros(uint32_t input) noexcept { uint64_t word = input; @@ -66,6 +69,8 @@ uint32_t deinterleave_lowuint32(uint64_t word) noexcept return static_cast(word); } +// NOLINTEND(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers) + uint32_t parse_num_with_max(std::string const &str, uint32_t max) { std::size_t pos = 0; diff --git a/src/util.cpp b/src/util.cpp index a9193454a..9faeb5b5b 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -23,19 +23,22 @@ namespace util { std::string human_readable_duration(uint64_t seconds) { - if (seconds < 60UL) { + constexpr uint64_t SECONDS_IN_MINUTE = 60UL; + constexpr uint64_t MINUTES_IN_HOUR = 60UL; + + if (seconds < SECONDS_IN_MINUTE) { return fmt::format("{}s", seconds); } - if (seconds < (60UL * 60UL)) { - return fmt::format("{}s ({}m {}s)", seconds, seconds / 60, - seconds % 60); + uint64_t const mins = seconds / SECONDS_IN_MINUTE; + uint64_t const secs = seconds % SECONDS_IN_MINUTE; + + if (seconds < (SECONDS_IN_MINUTE * MINUTES_IN_HOUR)) { + return fmt::format("{}s ({}m {}s)", seconds, mins, secs); } - auto const secs = seconds % 60; - auto const mins = seconds / 60; - return fmt::format("{}s ({}h {}m {}s)", seconds, mins / 60, mins % 60, - secs); + return fmt::format("{}s ({}h {}m {}s)", seconds, mins / MINUTES_IN_HOUR, + mins % MINUTES_IN_HOUR, secs); } std::string human_readable_duration(std::chrono::microseconds duration) diff --git a/src/wkb.cpp b/src/wkb.cpp index 629bb4d59..ccfac5946 100644 --- a/src/wkb.cpp +++ b/src/wkb.cpp @@ -209,6 +209,10 @@ class make_ewkb_visitor_t std::string operator()(geom::point_t const &geom) const { + constexpr std::size_t SIZE_POINT_HEADER_WITH_SRID = 1UL + 4UL + 4UL; + constexpr std::size_t SIZE_POINT = + SIZE_POINT_HEADER_WITH_SRID + SIZE_COORDINATE_PAIR; + std::string data; if (m_ensure_multi) { @@ -216,11 +220,9 @@ class make_ewkb_visitor_t write_length(&data, 1); write_point(&data, geom); } else { - // 9 byte header plus one set of coordinates - constexpr std::size_t SIZE = 9 + 2 * 8; - data.reserve(SIZE); + data.reserve(SIZE_POINT); write_point(&data, geom, m_srid); - assert(data.size() == SIZE); + assert(data.size() == SIZE_POINT); } return data; @@ -228,18 +230,27 @@ class make_ewkb_visitor_t std::string operator()(geom::linestring_t const &geom) const { + constexpr std::size_t SIZE_HEADER_WITH_COUNT = 1UL + 4UL + 4UL; + constexpr std::size_t SIZE_HEADER_WITH_COUNT_AND_SRID = + SIZE_HEADER_WITH_COUNT + 4UL; + std::string data; + std::size_t const coords_size = geom.size() * SIZE_COORDINATE_PAIR; + if (m_ensure_multi) { - // Two 13 bytes headers plus n sets of coordinates - data.reserve(2UL * 13UL + geom.size() * (2UL * 8UL)); + data.reserve(SIZE_HEADER_WITH_COUNT_AND_SRID + + SIZE_HEADER_WITH_COUNT + coords_size); write_header(&data, wkb_multi_line, m_srid); write_length(&data, 1); write_linestring(&data, geom); + assert(data.size() == SIZE_HEADER_WITH_COUNT_AND_SRID + + SIZE_HEADER_WITH_COUNT + coords_size); } else { - // 13 byte header plus n sets of coordinates - data.reserve(13UL + geom.size() * (2UL * 8UL)); + data.reserve(SIZE_HEADER_WITH_COUNT_AND_SRID + coords_size); write_linestring(&data, geom, m_srid); + assert(data.size() == + SIZE_HEADER_WITH_COUNT_AND_SRID + coords_size); } return data; @@ -289,6 +300,8 @@ class make_ewkb_visitor_t } private: + static constexpr std::size_t SIZE_COORDINATE_PAIR = 2UL * sizeof(double); + uint32_t m_srid; bool m_ensure_multi;