Skip to content

Commit

Permalink
Add an option to cache all connectionSets from all scenarios
Browse files Browse the repository at this point in the history
In a situation where you have multiple scenario and where you often switch between
them, having only one connection set in the cache does not offer lot of perf
improvement.

This add a --cacheAllConnectionSets parameter, which when set to true will
cache all connection cache.

This is implemented with a new ScenarioConnectionCacheAll class. (the old one renamed to ScenarioConnectionCacheOne)
The flag define which one the TransitData class will instanciate.

Issue #252
  • Loading branch information
greenscientist committed Aug 30, 2023
1 parent 8e7aeb9 commit d54fe37
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 19 deletions.
1 change: 1 addition & 0 deletions connection_scan_algorithm/include/program_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace TrRouting
std::string cachePath;
int port;
bool debug;
bool cacheAllConnectionSets;
std::string algorithm;
std::string dataFetcherShortname;
std::string osrmWalkingPort;
Expand Down
7 changes: 7 additions & 0 deletions connection_scan_algorithm/src/program_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace TrRouting {
("dataFetcher,data", boost::program_options::value<std::string>()->default_value("cache"), "data fetcher (csv, gtfs or cache)"); // only cache implemented for now
options.add_options()
("cachePath", boost::program_options::value<std::string>()->default_value("cache"), "cache path");
options.add_options()
("cacheAllConnectionSets", boost::program_options::value<bool>() ->default_value(false), "cache all connections set instead of the ones from the last used scenario");
options.add_options()
("osrmPort,osrmWalkPort,osrmWalkingPort", boost::program_options::value<std::string>()->default_value("5000"), "osrm walking port");
options.add_options()
Expand All @@ -40,6 +42,7 @@ namespace TrRouting {
debug = false;
dataFetcherShortname = "cache";
cachePath = "cache";
cacheAllConnectionSets = false;
osrmWalkingPort = "5000";
osrmCyclingPort = "8000";
osrmDrivingPort = "7000";
Expand Down Expand Up @@ -71,6 +74,10 @@ namespace TrRouting {
{
cachePath = variablesMap["cachePath"].as<std::string>();
}
if(variablesMap.count("cacheAllConnectionSets") == 1)
{
cacheAllConnectionSets = variablesMap["cacheAllConnectionSets"].as<bool>();
}

if(variablesMap.count("osrmWalkPort") == 1)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ int main(int argc, char** argv) {
}

spdlog::info("preparing calculator...");
TransitData transitData(*fetcher);
TransitData transitData(*fetcher, programOptions.cacheAllConnectionSets);
//TODO We wanted to handle error in the constructor, but later part of this code expect a dataStatus
// leaving as a todo
DataStatus dataStatus = transitData.getDataStatus();
Expand Down
52 changes: 43 additions & 9 deletions include/connection_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include <optional>
#include <boost/uuid/uuid.hpp>
#include <memory>

#include <map>

namespace TrRouting {

Expand All @@ -14,29 +14,63 @@ class ConnectionSet;


/**
* @brief Caches the connection sets used by different transit scenarios.
* @brief Interface for the caches of the connection sets used by different transit scenarios.
*
* // TODO Currently caches only the last queried scenario. We could make this a
* // TODO Currently caches only the last queried scenario or all of them. We could make this a
* real lru cache eventually, or not, if we find a better way to handle scenario
* query data.
*/
class ScenarioConnectionCache {

public:
ScenarioConnectionCache()
{

};
virtual ~ScenarioConnectionCache() {}

std::optional<std::shared_ptr<ConnectionSet>> get(boost::uuids::uuid uuid) const;
void set(boost::uuids::uuid uuid, std::shared_ptr<ConnectionSet> cache);
virtual std::optional<std::shared_ptr<ConnectionSet>> get(boost::uuids::uuid uuid) const = 0;
virtual void set(boost::uuids::uuid uuid, std::shared_ptr<ConnectionSet> cache) = 0;
};

/**
* @brief Caches the last connection sets used by different transit scenarios.
*
*/
class ScenarioConnectionCacheOne : public ScenarioConnectionCache {

public:
ScenarioConnectionCacheOne()
{

}
virtual ~ScenarioConnectionCacheOne() {}

virtual std::optional<std::shared_ptr<ConnectionSet>> get(boost::uuids::uuid uuid) const;
virtual void set(boost::uuids::uuid uuid, std::shared_ptr<ConnectionSet> cache);

private:
std::optional<boost::uuids::uuid> lastUuid;
std::shared_ptr<ConnectionSet> lastConnection;
};


/**
* @brief Caches the all connection sets used by different transit scenarios.
*
*/
class ScenarioConnectionCacheAll : public ScenarioConnectionCache {

public:
ScenarioConnectionCacheAll()
{

}
virtual ~ScenarioConnectionCacheAll(){}

virtual std::optional<std::shared_ptr<ConnectionSet>> get(boost::uuids::uuid uuid) const;
virtual void set(boost::uuids::uuid uuid, std::shared_ptr<ConnectionSet> cache);

private:
std::map<boost::uuids::uuid, std::shared_ptr<ConnectionSet> > connectionSets;
};

}

#endif // TR_CONNECTION_CACHE
6 changes: 4 additions & 2 deletions include/transit_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ namespace TrRouting {

class TransitData {
public:
TransitData(DataFetcher& dataFetcher);
TransitData(DataFetcher& dataFetcher, bool cacheAllScenarios = false);
virtual ~TransitData();
DataStatus getDataStatus() const;

// const data access functions
Expand Down Expand Up @@ -119,7 +120,8 @@ namespace TrRouting {
std::vector<std::reference_wrapper<const Connection>> forwardConnections; // Forward connections, sorted by departure time ascending
std::vector<std::reference_wrapper<const Connection>> reverseConnections; // Reverse connections, sorted by arrival time descending

mutable ScenarioConnectionCache scenarioConnectionCache = ScenarioConnectionCache();
//TODO Consider using a reference instead, making sure the object is always valid
mutable ScenarioConnectionCache *scenarioConnectionCache;
};

}
Expand Down
26 changes: 22 additions & 4 deletions src/connection_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,38 @@
#include "connection_set.hpp"
#include "spdlog/spdlog.h"
#include "connection.hpp"
#include <boost/uuid/uuid_io.hpp>


namespace TrRouting {

std::optional<std::shared_ptr<ConnectionSet>> ScenarioConnectionCache::get(boost::uuids::uuid uuid) const {
// ScenarioConnectionCacheOne
std::optional<std::shared_ptr<ConnectionSet>> ScenarioConnectionCacheOne::get(boost::uuids::uuid uuid) const {
if (uuid == lastUuid) {
return std::optional(lastConnection);
} else {
return std::nullopt;
}
}

void ScenarioConnectionCache::set(boost::uuids::uuid uuid, std::shared_ptr<ConnectionSet> cache) {
void ScenarioConnectionCacheOne::set(boost::uuids::uuid uuid, std::shared_ptr<ConnectionSet> cache) {
lastUuid = uuid;
lastConnection = cache;
}
}

// ScenarioConnectionCacheAll
std::optional<std::shared_ptr<ConnectionSet>> ScenarioConnectionCacheAll::get(boost::uuids::uuid uuid) const {
// Lookup the scenario uuid in the map. If found, returns it, if not, return a null_opt
auto connectionSetItr = connectionSets.find(uuid);
if (connectionSetItr != connectionSets.end()) {
return std::optional(connectionSetItr->second);
} else {
return std::nullopt;
}
}

void ScenarioConnectionCacheAll::set(boost::uuids::uuid uuid, std::shared_ptr<ConnectionSet> cache) {
spdlog::debug("Caching connection set for scenario {}", boost::uuids::to_string(uuid));

connectionSets[uuid] = cache;
}
}
17 changes: 14 additions & 3 deletions src/transit_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

namespace TrRouting {

TransitData::TransitData(DataFetcher& fetcher) :
TransitData::TransitData(DataFetcher& fetcher, bool cacheAllScenarios) :
dataFetcher(fetcher)
{
DataStatus loadStatus = loadAllData();
Expand All @@ -29,6 +29,17 @@ namespace TrRouting {
//throw std::exception("Incomplete transit data");
spdlog::error("TransitData loading had error, object will not be valid");
}
if (cacheAllScenarios) {
scenarioConnectionCache = new ScenarioConnectionCacheAll();
spdlog::info("Will cache all connectionSets");
} else {
scenarioConnectionCache = new ScenarioConnectionCacheOne();
spdlog::info("Will cache one connectionSet");
}
}

TransitData::~TransitData() {
delete scenarioConnectionCache;
}

DataStatus TransitData::getDataStatus() const {
Expand Down Expand Up @@ -332,7 +343,7 @@ namespace TrRouting {
}

std::shared_ptr<ConnectionSet> TransitData::getConnectionsForScenario(const Scenario & scenario) const {
std::optional<std::shared_ptr<ConnectionSet>> optCurrentCache = scenarioConnectionCache.get(scenario.uuid);
std::optional<std::shared_ptr<ConnectionSet>> optCurrentCache = scenarioConnectionCache->get(scenario.uuid);
if (optCurrentCache.has_value()) {
return optCurrentCache.value();
}
Expand Down Expand Up @@ -455,7 +466,7 @@ namespace TrRouting {
}

std::shared_ptr<ConnectionSet> currentCache = std::make_shared<ConnectionSet>(trips, scenarioForwardConnections, scenarioReverseConnections);
scenarioConnectionCache.set(scenario.uuid, currentCache);
scenarioConnectionCache->set(scenario.uuid, currentCache);
return currentCache;

}
Expand Down

0 comments on commit d54fe37

Please sign in to comment.