From ef92d40e69413cadd721c8bf0fdd0b50d7ad6145 Mon Sep 17 00:00:00 2001 From: Pablo Hoch Date: Mon, 9 Dec 2024 15:23:10 +0100 Subject: [PATCH] share decompressed bitvecs between routing requests --- include/motis/gbfs/data.h | 10 ++++++++++ src/gbfs/data.cc | 24 ++++++++++++++++++++++++ src/gbfs/routing_data.cc | 4 +--- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/include/motis/gbfs/data.h b/include/motis/gbfs/data.h index 9f1effe2a..9c1f060e2 100644 --- a/include/motis/gbfs/data.h +++ b/include/motis/gbfs/data.h @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -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" @@ -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 get_products_routing_data( + osr::ways const& w, osr::lookup const& l, gbfs_products_ref); + std::shared_ptr>> standalone_feeds_{}; std::shared_ptr>> @@ -392,6 +397,11 @@ struct gbfs_data { point_rtree provider_rtree_{}; lru_cache cache_; + + // used to share decompressed routing data between routing requests + std::mutex products_routing_data_mutex_; + hash_map> + products_routing_data_{}; }; } // namespace motis::gbfs diff --git a/src/gbfs/data.cc b/src/gbfs/data.cc index d457796ad..808dc984f 100644 --- a/src/gbfs/data.cc +++ b/src/gbfs/data.cc @@ -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 { @@ -13,4 +17,24 @@ products_routing_data::products_routing_data( decompress_bitvec(compressed_.through_allowed_, through_allowed_); } +std::shared_ptr 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 diff --git a/src/gbfs/routing_data.cc b/src/gbfs/routing_data.cc index e3f0a7c67..6dedfcf78 100644 --- a/src/gbfs/routing_data.cc +++ b/src/gbfs/routing_data.cc @@ -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(); } }