Skip to content

Commit 1507315

Browse files
committed
perf: geocode minimize string allocations
also used .to_string instead of .to_owned when the target type is a string
1 parent 872ade1 commit 1507315

File tree

1 file changed

+25
-25
lines changed

1 file changed

+25
-25
lines changed

src/cmd/geocode.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,7 +1336,7 @@ async fn geocode_main(args: Args) -> CliResult<()> {
13361336
}
13371337

13381338
/// check if index_file exists and ends with a .rkyv extension
1339-
fn check_index_file(index_file: &String) -> CliResult<()> {
1339+
fn check_index_file(index_file: &str) -> CliResult<()> {
13401340
// check if index_file is a u16 with the values 500, 1000, 5000 or 15000
13411341
// if it is, return OK
13421342
if let Ok(i) = index_file.parse::<u16>() {
@@ -1722,7 +1722,7 @@ fn search_index(
17221722
}
17231723

17241724
// not a valid lat, long
1725-
return None;
1725+
None
17261726
}
17271727

17281728
#[cached]
@@ -1766,19 +1766,19 @@ fn add_dyncols(
17661766
if let Some(state_fips_code) = admin1.code.strip_prefix("US.") {
17671767
// admin1 code is a US state code, the two-letter state code
17681768
// is the last two characters of the admin1 code
1769-
state_fips_code.to_string()
1769+
state_fips_code
17701770
} else {
17711771
// admin1 code is not a US state code
17721772
// set to empty string
1773-
String::new()
1773+
""
17741774
}
17751775
} else {
17761776
// no admin1 code
17771777
// set to empty string
1778-
String::new()
1778+
""
17791779
};
17801780
// lookup US state FIPS code
1781-
record.push_field(lookup_us_state_fips_code(&us_state_code).unwrap_or_default());
1781+
record.push_field(lookup_us_state_fips_code(us_state_code).unwrap_or_default());
17821782
},
17831783
"us_county_fips_code" => {
17841784
let us_county_fips_code = if let Some(admin2) = cityrecord.admin2_division.as_ref()
@@ -1788,7 +1788,7 @@ fn add_dyncols(
17881788
// is the last three characters of the admin2 code
17891789
// start at index 7 to skip the US. prefix
17901790
// e.g. US.NY.061 -> 061
1791-
format!("{:0>3}", admin2.code[7..].to_string())
1791+
format!("{:0>3}", &admin2.code[7..])
17921792
} else {
17931793
// admin2 code is not a US county code
17941794
// set to empty string
@@ -1864,11 +1864,11 @@ fn format_result(
18641864
},
18651865
"%state" | "%admin1" => nameslang.admin1name.clone(),
18661866
"%county" | "%admin2" => nameslang.admin2name.clone(),
1867-
"%country" => country.to_owned(),
1867+
"%country" => country.to_string(),
18681868
"%country_name" => nameslang.countryname.clone(),
1869-
"%id" => format!("{}", cityrecord.id),
1870-
"%capital" => capital.to_owned(),
1871-
"%population" => format!("{}", cityrecord.population),
1869+
"%id" => cityrecord.id.to_string(),
1870+
"%capital" => capital.to_string(),
1871+
"%population" => cityrecord.population.to_string(),
18721872
"%timezone" => cityrecord.timezone.to_string(),
18731873
"%cityrecord" => format!("{cityrecord:?}"),
18741874
"%admin1record" => format!("{:?}", cityrecord.admin_division),
@@ -1882,7 +1882,7 @@ fn format_result(
18821882
serde_json::to_string(cityrecord).unwrap_or_else(|_| "null".to_string());
18831883
let country_json =
18841884
serde_json::to_string(countryrecord).unwrap_or_else(|_| "null".to_string());
1885-
let us_fips_codes_json = get_us_fips_codes(cityrecord, nameslang).to_string();
1885+
let us_fips_codes_json = get_us_fips_codes(cityrecord, nameslang);
18861886
format!(
18871887
"{{\"cityrecord\":{cr_json}, \"countryrecord\":{country_json} \
18881888
\"us_fips_codes\":{us_fips_codes_json}}}",
@@ -1955,13 +1955,13 @@ fn format_result(
19551955
"name" => cityrecord_map.insert("name", nameslang.cityname.clone()),
19561956
"latitude" => cityrecord_map.insert("latitude", cityrecord.latitude.to_string()),
19571957
"longitude" => cityrecord_map.insert("longitude", cityrecord.longitude.to_string()),
1958-
"country" => cityrecord_map.insert("country", country.to_owned()),
1958+
"country" => cityrecord_map.insert("country", country.to_string()),
19591959
"country_name" => {
19601960
cityrecord_map.insert("country_name", nameslang.countryname.clone())
19611961
},
19621962
"admin1" => cityrecord_map.insert("admin1", nameslang.admin1name.clone()),
19631963
"admin2" => cityrecord_map.insert("admin2", nameslang.admin2name.clone()),
1964-
"capital" => cityrecord_map.insert("capital", capital.to_owned()),
1964+
"capital" => cityrecord_map.insert("capital", capital.to_string()),
19651965
"timezone" => cityrecord_map.insert("timezone", cityrecord.timezone.to_string()),
19661966
"population" => {
19671967
cityrecord_map.insert("population", cityrecord.population.to_string())
@@ -1974,21 +1974,21 @@ fn format_result(
19741974
if let Some(state_fips_code) = admin1.code.strip_prefix("US.") {
19751975
// admin1 code is a US state code, the two-letter state code
19761976
// is the last two characters of the admin1 code
1977-
state_fips_code.to_string()
1977+
state_fips_code
19781978
} else {
19791979
// admin1 code is not a US state code
19801980
// set to empty string
1981-
String::new()
1981+
""
19821982
}
19831983
} else {
19841984
// no admin1 code
19851985
// set to empty string
1986-
String::new()
1986+
""
19871987
};
19881988
cityrecord_map.insert(
19891989
"us_state_fips_code",
1990-
lookup_us_state_fips_code(&us_state_code)
1991-
.unwrap_or_default()
1990+
lookup_us_state_fips_code(us_state_code)
1991+
.unwrap_or("")
19921992
.to_string(),
19931993
)
19941994
},
@@ -2002,7 +2002,7 @@ fn format_result(
20022002
// is the last three characters of the admin2 code
20032003
// start at index 7 to skip the US. prefix
20042004
// e.g. US.NY.061 -> 061
2005-
format!("{:0>3}", admin2.code[7..].to_string())
2005+
format!("{:0>3}", &admin2.code[7..])
20062006
} else {
20072007
// admin2 code is not a US county code
20082008
// set to empty string
@@ -2238,18 +2238,18 @@ fn get_us_fips_codes(cityrecord: &CitiesRecord, nameslang: &NamesLang) -> serde_
22382238
if let Some(state_fips_code) = admin1.code.strip_prefix("US.") {
22392239
// admin1 code is a US state code, the two-letter state code
22402240
// is the last two characters of the admin1 code
2241-
state_fips_code.to_string()
2241+
state_fips_code
22422242
} else {
22432243
// admin1 code is not a US state code
22442244
// set to empty string
2245-
String::new()
2245+
""
22462246
}
22472247
} else {
22482248
// no admin1 code
22492249
// set to empty string
2250-
String::new()
2250+
""
22512251
};
2252-
let us_state_fips_code = lookup_us_state_fips_code(&us_state_code).unwrap_or("null");
2252+
let us_state_fips_code = lookup_us_state_fips_code(us_state_code).unwrap_or("null");
22532253

22542254
let us_county_code = match cityrecord.admin2_division.as_ref() {
22552255
Some(admin2) => {
@@ -2258,7 +2258,7 @@ fn get_us_fips_codes(cityrecord: &CitiesRecord, nameslang: &NamesLang) -> serde_
22582258
// is the last three characters of the admin2 code
22592259
// start at index 7 to skip the US. prefix
22602260
// e.g. US.NY.061 -> 061
2261-
format!("{:0>3}", admin2.code[7..].to_string())
2261+
format!("{:0>3}", &admin2.code[7..])
22622262
} else {
22632263
// admin2 code is not a US county code
22642264
// set to empty string

0 commit comments

Comments
 (0)