Skip to content

Commit

Permalink
move a lot of small strings to compact str
Browse files Browse the repository at this point in the history
  • Loading branch information
kylerchin committed Oct 4, 2024
1 parent 8184420 commit 6ee9aab
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 71 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ via-rail-gtfsrt = "0.1.1"
wyhash2 = "0.2.1"
console-subscriber = "0.4.0"
strumbra = "0.4.0"
compact_str = { version = "0.8.0", features = ["serde"] }
compact_str = { version = "0.8.0", features = ["serde", "diesel"] }
[[bin]]
name = "maple"
path = "src/maple/main.rs"
Expand Down
2 changes: 1 addition & 1 deletion src/aspen/import_alpenrose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ pub async fn new_rt_data(
let trip = trip_id_to_trip.get(&trip_id.clone());
match trip {
Some(trip) => {
trip.trip_short_name.clone()
trip.trip_short_name.as_ref().map(|x| x.to_string())
},
None => None
}
Expand Down
11 changes: 6 additions & 5 deletions src/birch/get_vehicle_trip_information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use catenary::schema::gtfs::trips_compressed as trips_compressed_pg_schema;
use catenary::EtcdConnectionIps;
use chrono::TimeZone;
use chrono_tz::Tz;
use compact_str::CompactString;
use diesel::query_dsl::methods::FilterDsl;
use diesel::query_dsl::methods::SelectDsl;
use diesel::ExpressionMethods;
Expand Down Expand Up @@ -255,7 +256,7 @@ struct TripIntroductionInformation {
}
#[derive(Deserialize, Serialize, Clone, Debug)]
struct StopTimeIntroduction {
pub stop_id: String,
pub stop_id: CompactString,
pub name: Option<String>,
pub translations: Option<BTreeMap<String, String>>,
pub platform_code: Option<String>,
Expand Down Expand Up @@ -549,7 +550,7 @@ pub async fn get_trip_init(

//convert shape data into polyline

let stop_ids_to_lookup: Vec<String> =
let stop_ids_to_lookup: Vec<CompactString> =
itin_rows_to_use.iter().map(|x| x.stop_id.clone()).collect();

let (stops_data, shape_lookup): (
Expand Down Expand Up @@ -729,7 +730,7 @@ pub async fn get_trip_init(
};

for row in itin_rows_to_use {
let stop = stops_data_map.get(&row.stop_id);
let stop = stops_data_map.get(row.stop_id.as_str());

if stop.is_none() {
eprintln!("Stop {} not found", row.stop_id);
Expand All @@ -739,7 +740,7 @@ pub async fn get_trip_init(
let stop = stop.unwrap();

let stop_time = StopTimeIntroduction {
stop_id: stop.gtfs_id.clone(),
stop_id: (&stop.gtfs_id).into(),
name: stop.name.clone(),
translations: None,
platform_code: stop.platform_code.clone(),
Expand Down Expand Up @@ -982,7 +983,7 @@ pub async fn get_trip_init(
wheelchair_accessible: trip_compressed.wheelchair_accessible,
has_frequencies: trip_compressed.has_frequencies,
trip_headsign: itin_meta.trip_headsign,
trip_short_name: trip_compressed.trip_short_name,
trip_short_name: trip_compressed.trip_short_name.map(|x| x.into()),
route_long_name: route.long_name,
route_short_name: route.short_name,
vehicle,
Expand Down
67 changes: 34 additions & 33 deletions src/birch/nearby_departures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use catenary::schema::gtfs::trips_compressed;
use catenary::CalendarUnified;
use catenary::EtcdConnectionIps;
use chrono::TimeZone;
use compact_str::CompactString;
use diesel::dsl::sql;
use diesel::dsl::sql_query;
use diesel::query_dsl::methods::FilterDsl;
Expand Down Expand Up @@ -75,16 +76,16 @@ struct DeparturesDebug {

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct DepartingTrip {
pub trip_id: String,
pub gtfs_frequency_start_time: Option<String>,
pub trip_id: CompactString,
pub gtfs_frequency_start_time: Option<CompactString>,
pub gtfs_schedule_start_day: chrono::NaiveDate,
pub is_frequency: bool,
pub departure_schedule: Option<u64>,
pub departure_realtime: Option<u64>,
pub arrival_schedule: Option<u64>,
pub arrival_realtime: Option<u64>,
pub stop_id: String,
pub trip_short_name: Option<String>,
pub stop_id: CompactString,
pub trip_short_name: Option<CompactString>,
pub tz: String,
pub is_interpolated: bool,
pub cancelled: bool,
Expand All @@ -100,10 +101,10 @@ pub struct DepartingHeadsignGroup {
#[derive(Deserialize, Serialize, Clone, Debug)]
pub struct DepartureRouteGroup {
pub chateau_id: String,
pub route_id: String,
pub color: Option<String>,
pub text_color: Option<String>,
pub short_name: Option<String>,
pub route_id: CompactString,
pub color: Option<CompactString>,
pub text_color: Option<CompactString>,
pub short_name: Option<CompactString>,
pub long_name: Option<String>,
pub route_type: i16,
pub directions: HashMap<String, DepartingHeadsignGroup>,
Expand All @@ -113,17 +114,17 @@ pub struct DepartureRouteGroup {
#[derive(Clone, Debug, Serialize)]
pub struct ValidTripSet {
pub chateau_id: String,
pub trip_id: String,
pub trip_id: CompactString,
pub frequencies: Option<Vec<gtfs_structures::Frequency>>,
pub trip_service_date: chrono::NaiveDate,
pub itinerary_options: Vec<ItineraryPatternRowNearbyLookup>,
pub reference_start_of_service_date: chrono::DateTime<chrono_tz::Tz>,
pub itinerary_pattern_id: String,
pub direction_pattern_id: String,
pub route_id: String,
pub route_id: CompactString,
pub timezone: Option<chrono_tz::Tz>,
pub trip_start_time: u32,
pub trip_short_name: Option<String>,
pub trip_short_name: Option<CompactString>,
}

// final datastructure ideas?
Expand Down Expand Up @@ -173,7 +174,7 @@ stop_reference: stop_id -> stop

#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct StopOutput {
pub gtfs_id: String,
pub gtfs_id: CompactString,
pub name: String,
pub lat: f64,
pub lon: f64,
Expand All @@ -187,7 +188,7 @@ pub struct DepartingTripsDataAnswer {
pub bus_limited_metres: f64,
pub rail_and_other_limited_metres: f64,
pub departures: Vec<DepartureRouteGroup>,
pub stop: HashMap<String, HashMap<String, StopOutput>>,
pub stop: HashMap<String, HashMap<CompactString, StopOutput>>,
}

#[actix_web::get("/nearbydeparturesfromcoords")]
Expand Down Expand Up @@ -332,7 +333,7 @@ pub async fn nearby_from_coords(
let directions_rows = directions_fetch_sql.unwrap();

//store the direction id and the index
let mut stops_to_directions: HashMap<(String, String), Vec<(u64, u32)>> = HashMap::new();
let mut stops_to_directions: HashMap<(String, CompactString), Vec<(u64, u32)>> = HashMap::new();

for d in directions_rows {
let id = d.direction_pattern_id.parse::<u64>().unwrap();
Expand Down Expand Up @@ -367,16 +368,16 @@ pub async fn nearby_from_coords(

//sorting finished

let mut directions_to_closest_stop: HashMap<(String, u64), (String, u32)> = HashMap::new();
let mut directions_to_closest_stop: HashMap<(String, u64), (CompactString, u32)> = HashMap::new();

for ((chateau, stop_id), distance_m) in sorted_order_stops.iter() {
let direction_at_this_stop = stops_to_directions.get(&(chateau.clone(), stop_id.clone()));
let direction_at_this_stop = stops_to_directions.get(&(chateau.clone(), stop_id.into()));

if let Some(direction_at_this_stop) = direction_at_this_stop {
for (direction_id, sequence) in direction_at_this_stop {
match directions_to_closest_stop.entry((chateau.clone(), *direction_id)) {
Entry::Vacant(ve) => {
ve.insert((stop_id.clone(), *sequence));
ve.insert((stop_id.into(), *sequence));
}
_ => {}
}
Expand Down Expand Up @@ -526,7 +527,7 @@ pub async fn nearby_from_coords(

let mut compressed_trips_table: HashMap<String, Vec<CompressedTrip>> = HashMap::new();

let mut services_to_lookup_table: HashMap<String, BTreeSet<String>> = HashMap::new();
let mut services_to_lookup_table: HashMap<String, BTreeSet<CompactString>> = HashMap::new();

let mut routes_to_lookup_table: HashMap<String, BTreeSet<String>> = HashMap::new();

Expand All @@ -538,7 +539,7 @@ pub async fn nearby_from_coords(
let service_ids = compressed_trip_group
.iter()
.map(|x| x.service_id.clone())
.collect::<BTreeSet<String>>();
.collect::<BTreeSet<CompactString>>();

let route_ids = compressed_trip_group
.iter()
Expand Down Expand Up @@ -681,7 +682,7 @@ pub async fn nearby_from_coords(
Ok(calendar_structure) => {
// iterate through all trips and produce a timezone and timeoffset.

let mut stops_answer: HashMap<String, HashMap<String, StopOutput>> = HashMap::new();
let mut stops_answer: HashMap<String, HashMap<CompactString, StopOutput>> = HashMap::new();
let mut departures: Vec<DepartureRouteGroup> = vec![];

for (chateau_id, calendar_in_chateau) in calendar_structure.iter() {
Expand Down Expand Up @@ -731,7 +732,7 @@ pub async fn nearby_from_coords(
direction_id: itin_ref.direction_pattern_id.clone(),
};

let service = calendar_in_chateau.get(&trip.service_id);
let service = calendar_in_chateau.get(trip.service_id.as_str());

if let Some(service) = service {
let dates = catenary::find_service_ranges(
Expand All @@ -746,7 +747,7 @@ pub async fn nearby_from_coords(
for date in dates {
let t = ValidTripSet {
chateau_id: chateau_id.clone(),
trip_id: trip.trip_id.clone(),
trip_id: (&trip.trip_id).into(),
timezone: chrono_tz::Tz::from_str(itin_ref.timezone.as_str())
.ok(),
frequencies: freq_converted.clone(),
Expand All @@ -755,7 +756,7 @@ pub async fn nearby_from_coords(
reference_start_of_service_date: date.1,
itinerary_pattern_id: itin_ref.itinerary_pattern_id.clone(),
direction_pattern_id: itin_ref.direction_pattern_id.clone(),
route_id: itin_ref.route_id.clone(),
route_id: (&itin_ref.route_id).into(),
trip_start_time: trip.start_time,
trip_short_name: trip.trip_short_name.clone(),
};
Expand Down Expand Up @@ -809,17 +810,17 @@ pub async fn nearby_from_coords(
// temp_answer.insert(chateau_id.clone(), valid_trips);

for (trip_id, trip_grouping) in valid_trips {
let route = routes.get(&trip_grouping[0].route_id).unwrap();
let route = routes.get(trip_grouping[0].route_id.as_str()).unwrap();

if !directions_route_group_for_this_chateau.contains_key(&route.route_id) {
directions_route_group_for_this_chateau.insert(
route.route_id.clone(),
DepartureRouteGroup {
chateau_id: chateau_id.clone(),
route_id: route.route_id.clone(),
color: route.color.clone(),
text_color: route.text_color.clone(),
short_name: route.short_name.clone(),
route_id: (&route.route_id).into(),
color: route.color.as_ref().map(|x| x.into()),
text_color: route.text_color.as_ref().map(|x| x.into()),
short_name: route.short_name.as_ref().map(|x| x.into()),
long_name: route.long_name.clone(),
route_type: route.route_type,
directions: HashMap::new(),
Expand Down Expand Up @@ -867,7 +868,7 @@ pub async fn nearby_from_coords(
if let Some(gtfs_trip_aspenised) = gtfs_trips_aspenised.as_ref() {
if let Some(trip_update_ids) = gtfs_trip_aspenised
.trip_id_to_trip_update_ids
.get(&trip.trip_id)
.get(trip.trip_id.as_str())
{
if !trip_update_ids.is_empty() {
// let trip_update_id = trip_rt[0].clone();
Expand Down Expand Up @@ -941,7 +942,7 @@ pub async fn nearby_from_coords(
departure_realtime: departure_time_rt,
arrival_schedule: None,
arrival_realtime: None,
stop_id: trip.itinerary_options[0].stop_id.clone(),
stop_id: (&trip.itinerary_options[0].stop_id).into(),
trip_short_name: trip.trip_short_name.clone(),
tz: trip.timezone.as_ref().unwrap().name().to_string(),
is_frequency: trip.frequencies.is_some(),
Expand Down Expand Up @@ -984,7 +985,7 @@ pub async fn nearby_from_coords(
.sort_by_key(|x| x.departure_schedule.unwrap_or(0));

let stop = stops_table
.get(&(chateau_id.clone(), headsign_group.trips[0].stop_id.clone()).clone())
.get(&(chateau_id.clone(), headsign_group.trips[0].stop_id.to_string()).clone())
.unwrap();

if !stops_answer.contains_key(chateau_id) {
Expand All @@ -993,11 +994,11 @@ pub async fn nearby_from_coords(

let stop_group = stops_answer.get_mut(chateau_id).unwrap();

if !stop_group.contains_key(&headsign_group.trips[0].stop_id) {
if !stop_group.contains_key(headsign_group.trips[0].stop_id.as_str()) {
stop_group.insert(
headsign_group.trips[0].stop_id.clone(),
StopOutput {
gtfs_id: stop.0.gtfs_id.clone(),
gtfs_id: (&stop.0.gtfs_id).into(),
name: stop.0.name.clone().unwrap_or("".to_string()),
lat: stop.0.point.as_ref().unwrap().x,
lon: stop.0.point.as_ref().unwrap().y,
Expand Down
3 changes: 2 additions & 1 deletion src/birch/route_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use catenary::aspen_dataset::AspenisedAlert;
use catenary::models::DirectionPatternMeta;
use catenary::models::DirectionPatternRow;
use catenary::EtcdConnectionIps;
use compact_str::CompactString;
use diesel::ExpressionMethods;
use diesel::QueryDsl;
use diesel::SelectableHelper;
Expand Down Expand Up @@ -262,7 +263,7 @@ pub async fn route_info(
let list_of_stop_ids = direction_rows
.iter()
.map(|x| x.stop_id.clone())
.collect::<HashSet<String>>();
.collect::<HashSet<CompactString>>();

let stops_pg: Vec<catenary::models::Stop> = catenary::schema::gtfs::stops::dsl::stops
.filter(catenary::schema::gtfs::stops::dsl::chateau.eq(&query.chateau))
Expand Down
4 changes: 2 additions & 2 deletions src/maple/gtfs_process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ pub async fn gtfs_process_feed(
let stop_points = direction_pattern
.stop_sequence
.iter()
.filter_map(|stop_id| gtfs.stops.get(stop_id))
.filter_map(|stop_id| gtfs.stops.get(stop_id.as_str()))
.filter_map(|stop| match (stop.latitude, stop.longitude) {
(Some(latitude), Some(longitude)) => Some(postgis_diesel::types::Point {
y: latitude,
Expand All @@ -262,7 +262,7 @@ pub async fn gtfs_process_feed(

//insert into shapes and shapes_not_bus

let route = gtfs.routes.get(&direction_pattern.route_id);
let route = gtfs.routes.get(direction_pattern.route_id.as_str());

if let Some(route) = route {
let _ = insert_stop_to_stop_geometry(
Expand Down
Loading

0 comments on commit 6ee9aab

Please sign in to comment.