Skip to content

Commit

Permalink
geocode: inline work var and change match to if let
Browse files Browse the repository at this point in the history
for this perf-sensitive lookup fn
  • Loading branch information
jqnatividad committed Oct 9, 2023
1 parent bfd81c3 commit 5d28580
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions src/cmd/geocode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ static DEFAULT_ADMIN2_CODES_URL: &str = "https://download.geonames.org/export/du

// ensure the state is sorted alphabetically
// as we use binary_search to lookup the state FIPS code
static US_STATES_FIPS_CODES_LOOKUP: &[(&str, &str)] = &[
static US_STATES_FIPS_CODES: &[(&str, &str)] = &[
("AK", "02"),
("AL", "01"),
("AR", "05"),
Expand Down Expand Up @@ -1551,7 +1551,8 @@ fn add_dyncols(
},
};
// lookup US state FIPS code
let us_state_fips_code = lookup_fips_code(&us_state_code).unwrap_or_default();
let us_state_fips_code =
lookup_us_state_fips_code(&us_state_code).unwrap_or_default();
record.push_field(us_state_fips_code);
},
"us_county_fips_code" => {
Expand Down Expand Up @@ -1722,7 +1723,7 @@ fn format_result(
String::new()
},
};
let us_state_fips_code = lookup_fips_code(&us_state_code).unwrap_or_default();
let us_state_fips_code = lookup_us_state_fips_code(&us_state_code).unwrap_or_default();
cityrecord_map.insert("us_state_fips_code", us_state_fips_code.to_string());

// set US county FIPS code
Expand Down Expand Up @@ -1930,10 +1931,10 @@ fn get_cityrecord_name_in_lang(cityrecord: &CitiesRecord, lang_lookup: &str) ->
}

#[inline]
fn lookup_fips_code(state: &str) -> Option<&str> {
let index = US_STATES_FIPS_CODES_LOOKUP.binary_search_by_key(&state, |&(abbrev, _)| abbrev);
match index {
Ok(i) => Some(US_STATES_FIPS_CODES_LOOKUP[i].1),
Err(_) => None,
fn lookup_us_state_fips_code(state: &str) -> Option<&str> {
if let Ok(i) = US_STATES_FIPS_CODES.binary_search_by_key(&state, |&(abbrev, _)| abbrev) {
Some(US_STATES_FIPS_CODES[i].1)
} else {
None
}
}

0 comments on commit 5d28580

Please sign in to comment.