Skip to content

Commit 48257d7

Browse files
committed
sort: because of perf, only enable try_init_collator when relevant
1 parent 888eb44 commit 48257d7

2 files changed

Lines changed: 29 additions & 5 deletions

File tree

src/uu/sort/src/sort.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,12 +1038,31 @@ fn get_rlimit() -> UResult<usize> {
10381038

10391039
const STDIN_FILE: &str = "-";
10401040

1041+
/// Check if locale-aware collation will be needed based on sort settings and locale
1042+
fn will_need_locale_collation(settings: &GlobalSettings) -> bool {
1043+
// First check if we're using the C locale (DEFAULT_LOCALE), which doesn't need collator
1044+
let (locale, _) = uucore::i18n::get_collating_locale();
1045+
if *locale == uucore::i18n::DEFAULT_LOCALE {
1046+
return false;
1047+
}
1048+
1049+
// Check each selector to see if any would use locale comparison
1050+
for selector in &settings.selectors {
1051+
let key_settings = &selector.settings;
1052+
if key_settings.mode == SortMode::Default
1053+
&& !key_settings.ignore_case
1054+
&& !key_settings.ignore_non_printing
1055+
&& !key_settings.dictionary_order
1056+
{
1057+
return true;
1058+
}
1059+
}
1060+
false
1061+
}
1062+
10411063
#[uucore::main]
10421064
#[allow(clippy::cognitive_complexity)]
10431065
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
1044-
// Initialize locale-aware collator for string comparisons
1045-
uucore::i18n::collator::try_init_collator(CollatorOptions::default());
1046-
10471066
let mut settings = GlobalSettings::default();
10481067

10491068
let matches = uucore::clap_localization::handle_clap_result_with_exit_code(uu_app(), args, 2)?;
@@ -1322,6 +1341,11 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
13221341

13231342
settings.init_precomputed();
13241343

1344+
// Initialize locale-aware collator only if needed for string comparisons
1345+
if will_need_locale_collation(&settings) {
1346+
uucore::i18n::collator::try_init_collator(CollatorOptions::default());
1347+
}
1348+
13251349
let result = exec(&mut files, &settings, output, &mut tmp_dir);
13261350
// Wait here if `SIGINT` was received,
13271351
// for signal handler to do its work and terminate the program.

src/uucore/src/lib/features/i18n/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub enum UEncoding {
2020
Utf8,
2121
}
2222

23-
const DEFAULT_LOCALE: Locale = locale!("en-US-posix");
23+
pub const DEFAULT_LOCALE: Locale = locale!("en-US-posix");
2424

2525
/// Look at 3 environment variables in the following order
2626
///
@@ -64,7 +64,7 @@ fn get_locale_from_env(locale_name: &str) -> (Locale, UEncoding) {
6464
}
6565

6666
/// Get the collating locale from the environment
67-
fn get_collating_locale() -> &'static (Locale, UEncoding) {
67+
pub fn get_collating_locale() -> &'static (Locale, UEncoding) {
6868
static COLLATING_LOCALE: OnceLock<(Locale, UEncoding)> = OnceLock::new();
6969

7070
COLLATING_LOCALE.get_or_init(|| get_locale_from_env("LC_COLLATE"))

0 commit comments

Comments
 (0)