Skip to content

Commit

Permalink
share decompressed bitvecs between routing requests
Browse files Browse the repository at this point in the history
  • Loading branch information
pablohoch committed Dec 9, 2024
1 parent efbc8b1 commit ef92d40
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
10 changes: 10 additions & 0 deletions include/motis/gbfs/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <filesystem>
#include <map>
#include <memory>
#include <mutex>
#include <optional>
#include <string>
#include <variant>
Expand All @@ -25,6 +26,7 @@
#include "osr/types.h"

#include "motis/config.h"
#include "motis/fwd.h"
#include "motis/point_rtree.h"
#include "motis/types.h"

Expand Down Expand Up @@ -382,6 +384,9 @@ struct aggregated_feed {
struct gbfs_data {
explicit gbfs_data(std::size_t const cache_size) : cache_{cache_size} {}

std::shared_ptr<products_routing_data> get_products_routing_data(
osr::ways const& w, osr::lookup const& l, gbfs_products_ref);

std::shared_ptr<std::vector<std::unique_ptr<provider_feed>>>
standalone_feeds_{};
std::shared_ptr<std::vector<std::unique_ptr<aggregated_feed>>>
Expand All @@ -392,6 +397,11 @@ struct gbfs_data {
point_rtree<gbfs_provider_idx_t> provider_rtree_{};

lru_cache<gbfs_provider_idx_t, provider_routing_data> cache_;

// used to share decompressed routing data between routing requests
std::mutex products_routing_data_mutex_;
hash_map<gbfs_products_ref, std::weak_ptr<products_routing_data>>
products_routing_data_{};
};

} // namespace motis::gbfs
24 changes: 24 additions & 0 deletions src/gbfs/data.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include "motis/gbfs/data.h"

#include "osr/lookup.h"
#include "osr/ways.h"

#include "motis/gbfs/compression.h"
#include "motis/gbfs/routing_data.h"

namespace motis::gbfs {

Expand All @@ -13,4 +17,24 @@ products_routing_data::products_routing_data(
decompress_bitvec(compressed_.through_allowed_, through_allowed_);
}

std::shared_ptr<products_routing_data> gbfs_data::get_products_routing_data(
osr::ways const& w,
osr::lookup const& l,
gbfs_products_ref const prod_ref) {
auto lock = std::unique_lock{products_routing_data_mutex_};

if (auto it = products_routing_data_.find(prod_ref);
it != end(products_routing_data_)) {
if (auto prod_rd = it->second.lock(); prod_rd) {
return prod_rd;
}
}

auto provider_rd = get_provider_routing_data(
w, l, *this, *providers_.at(prod_ref.provider_));
auto prod_rd = provider_rd->get_products_routing_data(prod_ref.products_);
products_routing_data_[prod_ref] = prod_rd;
return prod_rd;
}

} // namespace motis::gbfs
4 changes: 1 addition & 3 deletions src/gbfs/routing_data.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ products_routing_data* gbfs_routing_data::get_products_routing_data(
return it->second.get();
} else {
return products_
.emplace(prod_ref,
get_provider_routing_data(provider)->get_products_routing_data(
prod_idx))
.emplace(prod_ref, data_->get_products_routing_data(*w_, *l_, prod_ref))
.first->second.get();
}
}
Expand Down

0 comments on commit ef92d40

Please sign in to comment.