Caches iterators instead of invoking function calls on every iteration.
This caches iterators, i.e. especially the end iterator when possible.
The problem:
for (auto it = begin(seq); it != end(seq); ++it)
this has to call `end(seq)` on every iteration, since the compiler is
not able to reason about the call's site effects (to bad, huh).
Instead do it like this:
for (auto it = begin(seq), end = end(seq); it != end; ++it)
caching the end iterator.
Of course, still better would be:
for (auto&& each : seq)
if all you want is value semantics.
Why `auto&&` you may ask? Because it binds to everything and never copies!
Skim the referenced proposal (that was rejected, but nevertheless) for a
detailed explanation on range-based for loops and why `auto&&` is great.
Reference:
- http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3853.htm
This commit is contained in:
@@ -169,9 +169,12 @@ void ExtractionContainers::PrepareNodes()
|
||||
external_to_internal_node_id_map.reserve(used_node_id_list.size());
|
||||
auto node_iter = all_nodes_list.begin();
|
||||
auto ref_iter = used_node_id_list.begin();
|
||||
const auto all_nodes_list_end = all_nodes_list.end();
|
||||
const auto used_node_id_list_end = used_node_id_list.end();
|
||||
auto internal_id = 0u;
|
||||
|
||||
// compute the intersection of nodes that were referenced and nodes we actually have
|
||||
while (node_iter != all_nodes_list.end() && ref_iter != used_node_id_list.end())
|
||||
while (node_iter != all_nodes_list_end && ref_iter != used_node_id_list_end)
|
||||
{
|
||||
if (node_iter->node_id < *ref_iter)
|
||||
{
|
||||
@@ -208,7 +211,11 @@ void ExtractionContainers::PrepareEdges(lua_State *segment_state)
|
||||
// Traverse list of edges and nodes in parallel and set start coord
|
||||
auto node_iterator = all_nodes_list.begin();
|
||||
auto edge_iterator = all_edges_list.begin();
|
||||
while (edge_iterator != all_edges_list.end() && node_iterator != all_nodes_list.end())
|
||||
|
||||
const auto all_edges_list_end = all_edges_list.end();
|
||||
const auto all_nodes_list_end = all_nodes_list.end();
|
||||
|
||||
while (edge_iterator != all_edges_list_end && node_iterator != all_nodes_list_end)
|
||||
{
|
||||
if (edge_iterator->result.source < node_iterator->node_id)
|
||||
{
|
||||
@@ -259,7 +266,10 @@ void ExtractionContainers::PrepareEdges(lua_State *segment_state)
|
||||
TIMER_START(compute_weights);
|
||||
node_iterator = all_nodes_list.begin();
|
||||
edge_iterator = all_edges_list.begin();
|
||||
while (edge_iterator != all_edges_list.end() && node_iterator != all_nodes_list.end())
|
||||
const auto all_edges_list_end_ = all_edges_list.end();
|
||||
const auto all_nodes_list_end_ = all_nodes_list.end();
|
||||
|
||||
while (edge_iterator != all_edges_list_end_ && node_iterator != all_nodes_list_end_)
|
||||
{
|
||||
// skip all invalid edges
|
||||
if (edge_iterator->result.source == SPECIAL_NODEID)
|
||||
@@ -475,7 +485,10 @@ void ExtractionContainers::WriteNodes(std::ofstream& file_out_stream) const
|
||||
// identify all used nodes by a merging step of two sorted lists
|
||||
auto node_iterator = all_nodes_list.begin();
|
||||
auto node_id_iterator = used_node_id_list.begin();
|
||||
while (node_id_iterator != used_node_id_list.end() && node_iterator != all_nodes_list.end())
|
||||
const auto used_node_id_list_end = used_node_id_list.end();
|
||||
const auto all_nodes_list_end = all_nodes_list.end();
|
||||
|
||||
while (node_id_iterator != used_node_id_list_end && node_iterator != all_nodes_list_end)
|
||||
{
|
||||
if (*node_id_iterator < node_iterator->node_id)
|
||||
{
|
||||
@@ -550,9 +563,11 @@ void ExtractionContainers::PrepareRestrictions()
|
||||
TIMER_START(fix_restriction_starts);
|
||||
auto restrictions_iterator = restrictions_list.begin();
|
||||
auto way_start_and_end_iterator = way_start_end_id_list.cbegin();
|
||||
const auto restrictions_list_end = restrictions_list.end();
|
||||
const auto way_start_end_id_list_end = way_start_end_id_list.cend();
|
||||
|
||||
while (way_start_and_end_iterator != way_start_end_id_list.cend() &&
|
||||
restrictions_iterator != restrictions_list.end())
|
||||
while (way_start_and_end_iterator != way_start_end_id_list_end &&
|
||||
restrictions_iterator != restrictions_list_end)
|
||||
{
|
||||
if (way_start_and_end_iterator->way_id < restrictions_iterator->restriction.from.way)
|
||||
{
|
||||
@@ -616,8 +631,11 @@ void ExtractionContainers::PrepareRestrictions()
|
||||
TIMER_START(fix_restriction_ends);
|
||||
restrictions_iterator = restrictions_list.begin();
|
||||
way_start_and_end_iterator = way_start_end_id_list.cbegin();
|
||||
while (way_start_and_end_iterator != way_start_end_id_list.cend() &&
|
||||
restrictions_iterator != restrictions_list.end())
|
||||
const auto way_start_end_id_list_end_ = way_start_end_id_list.cend();
|
||||
const auto restrictions_list_end_ = restrictions_list.end();
|
||||
|
||||
while (way_start_and_end_iterator != way_start_end_id_list_end_ &&
|
||||
restrictions_iterator != restrictions_list_end_)
|
||||
{
|
||||
if (way_start_and_end_iterator->way_id < restrictions_iterator->restriction.to.way)
|
||||
{
|
||||
|
||||
@@ -161,7 +161,7 @@ int extractor::run()
|
||||
{
|
||||
// create a vector of iterators into the buffer
|
||||
std::vector<osmium::memory::Buffer::const_iterator> osm_elements;
|
||||
for (auto iter = std::begin(buffer); iter != std::end(buffer); ++iter)
|
||||
for (auto iter = std::begin(buffer), end = std::end(buffer); iter != end; ++iter)
|
||||
{
|
||||
osm_elements.push_back(iter);
|
||||
}
|
||||
@@ -180,7 +180,7 @@ int extractor::run()
|
||||
ExtractionWay result_way;
|
||||
lua_State *local_state = scripting_environment.get_lua_state();
|
||||
|
||||
for (auto x = range.begin(); x != range.end(); ++x)
|
||||
for (auto x = range.begin(), end = range.end(); x != end; ++x)
|
||||
{
|
||||
const auto entity = osm_elements[x];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user