Skip to content

Commit

Permalink
Migrate the forwardJourneysSteps and reverseJourneysSteps from a map …
Browse files Browse the repository at this point in the history
…to a vector

The vector is indexed by the Node::uid like the similar vectors in calculator.hpp

We gain about 5% to 10% in benchmarks with this change
  • Loading branch information
greenscientist committed Oct 27, 2023
1 parent 60c4fb6 commit ffb62e4
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 13 deletions.
4 changes: 2 additions & 2 deletions connection_scan_algorithm/include/calculator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ namespace TrRouting

std::vector<NodeTimeDistance> accessFootpaths; // pair: accessNodeIndex, walkingTravelTimeSeconds, walkingDistanceMeters
std::vector<NodeTimeDistance> egressFootpaths; // pair: egressNodeIndex, walkingTravelTimeSeconds, walkingDistanceMeters
std::unordered_map<Node::uid_t, JourneyStep> forwardJourneysSteps;
std::unordered_map<Node::uid_t, JourneyStep> reverseJourneysSteps;
std::vector<JourneyStep> forwardJourneysSteps; // indexed by Node::uid
std::vector<JourneyStep> reverseJourneysSteps; // indexed by Node::uid

};

Expand Down
1 change: 1 addition & 0 deletions connection_scan_algorithm/include/journey_step.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace TrRouting {
transferTravelTime(_transferTravelTime),
sameNodeTransfer(_sameNodeTransfer),
transferDistance(_transferDistance) { }
JourneyStep() : JourneyStep(std::nullopt, std::nullopt, std::nullopt, -1, false, -1) {}

//TODO Why is there Final in the function name. Is that appropriate?
std::optional<std::reference_wrapper<const Connection>> getFinalEnterConnection() const {return finalEnterConnection;}
Expand Down
4 changes: 2 additions & 2 deletions connection_scan_algorithm/src/forward_calculation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ namespace TrRouting
nodesTentativeTime[transferableNode.node.uid] = footpathTravelTime + connectionArrivalTime;

//TODO DO we need a make_optional here??
forwardJourneysSteps.insert_or_assign(transferableNode.node.uid, JourneyStep(currentTripQueryOverlay.enterConnection, *connection, std::cref(trip), footpathTravelTime, (nodeArrival == transferableNode.node), footpathDistance));
forwardJourneysSteps.at(transferableNode.node.uid) = JourneyStep(currentTripQueryOverlay.enterConnection, *connection, std::cref(trip), footpathTravelTime, (nodeArrival == transferableNode.node), footpathDistance);
}

if (
Expand Down Expand Up @@ -316,7 +316,7 @@ namespace TrRouting
nodesTentativeTime[transferableNode.node.uid] = footpathTravelTime + connectionArrivalTime;

//TODO DO we need a make_optional here??
forwardJourneysSteps.insert_or_assign(transferableNode.node.uid, JourneyStep(currentTripQueryOverlay.enterConnection, *connection, std::cref(trip), footpathTravelTime, (nodeArrival == transferableNode.node), footpathDistance));
forwardJourneysSteps.at(transferableNode.node.uid) = JourneyStep(currentTripQueryOverlay.enterConnection, *connection, std::cref(trip), footpathTravelTime, (nodeArrival == transferableNode.node), footpathDistance);
}

if (
Expand Down
8 changes: 4 additions & 4 deletions connection_scan_algorithm/src/resets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace TrRouting
int footpathTravelTimeSeconds;
int footpathDistanceMeters;
nodesAccess.clear();
forwardJourneysSteps.clear();
forwardJourneysSteps.assign(Node::getMaxUid() + 1, JourneyStep());
nodesTentativeTime.assign(Node::getMaxUid() + 1, MAX_INT); //Assign default values to all indexes

for (auto & accessFootpath : accessFootpaths)
Expand All @@ -90,7 +90,7 @@ namespace TrRouting
nodesAccess.emplace(accessFootpath.node.uid, NodeTimeDistance(accessFootpath.node,
footpathTravelTimeSeconds,
footpathDistanceMeters));
forwardJourneysSteps.insert_or_assign(accessFootpath.node.uid, JourneyStep(std::nullopt, std::nullopt, std::nullopt, footpathTravelTimeSeconds, false, footpathDistanceMeters));
forwardJourneysSteps.at(accessFootpath.node.uid) = JourneyStep(std::nullopt, std::nullopt, std::nullopt, footpathTravelTimeSeconds, false, footpathDistanceMeters);
nodesTentativeTime[accessFootpath.node.uid] = departureTimeSeconds + footpathTravelTimeSeconds;
if (footpathTravelTimeSeconds < minAccessTravelTime)
{
Expand All @@ -115,7 +115,7 @@ namespace TrRouting
int footpathTravelTimeSeconds;
int footpathDistanceMeters;
nodesEgress.clear();
reverseJourneysSteps.clear();
reverseJourneysSteps.assign(Node::getMaxUid() + 1, JourneyStep());
nodesReverseTentativeTime.assign(Node::getMaxUid() + 1, -1); //Assign default values to all indexes
for (auto & egressFootpath : egressFootpaths)
{
Expand All @@ -126,7 +126,7 @@ namespace TrRouting
footpathTravelTimeSeconds,
footpathDistanceMeters));

reverseJourneysSteps.insert_or_assign(egressFootpath.node.uid, JourneyStep(std::nullopt, std::nullopt, std::nullopt, footpathTravelTimeSeconds, false, footpathDistanceMeters));
reverseJourneysSteps.at(egressFootpath.node.uid) = JourneyStep(std::nullopt, std::nullopt, std::nullopt, footpathTravelTimeSeconds, false, footpathDistanceMeters);
nodesReverseTentativeTime[egressFootpath.node.uid] = arrivalTimeSeconds - footpathTravelTimeSeconds;
if (footpathTravelTimeSeconds > maxEgressTravelTime)
{
Expand Down
9 changes: 4 additions & 5 deletions connection_scan_algorithm/src/reverse_calculation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,10 @@ namespace TrRouting
{

// Make sure the arrival of the connection can unboard and is a candidate for the journey. Nodes are candidate if they can either reach the destination by foot, or a connection that can reach the destination within the specified parameters.
auto rjsIte = reverseJourneysSteps.find(nodeArrival.uid);
if ((*connection).get().canUnboard() && rjsIte != reverseJourneysSteps.end())
if ((*connection).get().canUnboard())
{
// Extract journeyStep once from map
const JourneyStep & reverseStepAtArrival = rjsIte->second;
const JourneyStep & reverseStepAtArrival = reverseJourneysSteps.at(nodeArrival.uid);
if (!tripExitConnection.has_value()) // <= to make sure we get the same result as forward calculation, which uses >
{
currentTripQueryOverlay.exitConnection = *connection;
Expand Down Expand Up @@ -144,7 +143,7 @@ namespace TrRouting
footpathDistance = nodeDeparture.reverseTransferableNodes.at(footpathIndex).distance;
nodesReverseTentativeTime[transferableNode.node.uid] = connectionDepartureTime - footpathTravelTime - connectionMinWaitingTimeSeconds;
//TODO Do we need a make_optional<...>(connection) ??
reverseJourneysSteps.insert_or_assign(transferableNode.node.uid, JourneyStep(*connection, currentTripQueryOverlay.exitConnection, std::cref(trip), footpathTravelTime, (nodeDeparture == transferableNode.node), footpathDistance));
reverseJourneysSteps.at(transferableNode.node.uid) = JourneyStep(*connection, currentTripQueryOverlay.exitConnection, std::cref(trip), footpathTravelTime, (nodeDeparture == transferableNode.node), footpathDistance);
}
if (
nodeDeparture == transferableNode.node
Expand Down Expand Up @@ -344,7 +343,7 @@ namespace TrRouting
footpathDistance = nodeDeparture.reverseTransferableNodes.at(footpathIndex).distance;
nodesReverseTentativeTime[transferableNode.node.uid] = connectionDepartureTime - footpathTravelTime - connectionMinWaitingTimeSeconds;
//TODO Do we need a make_optional<...>(connection) ??
reverseJourneysSteps.insert_or_assign(transferableNode.node.uid, JourneyStep(*connection, currentTripQueryOverlay.exitConnection, std::cref(trip), footpathTravelTime, (nodeDeparture == transferableNode.node), footpathDistance));
reverseJourneysSteps.at(transferableNode.node.uid) = JourneyStep(*connection, currentTripQueryOverlay.exitConnection, std::cref(trip), footpathTravelTime, (nodeDeparture == transferableNode.node), footpathDistance);
}
if (
nodeDeparture == transferableNode.node
Expand Down

0 comments on commit ffb62e4

Please sign in to comment.