Skip to content

Commit 352cf79

Browse files
committed
sort: deduplicate SI unit constants to shared uucore module
1 parent 1aadae3 commit 352cf79

File tree

5 files changed

+22
-19
lines changed

5 files changed

+22
-19
lines changed

src/uu/sort/src/buffer_hint.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use std::ffi::OsString;
99
use crate::{
1010
FALLBACK_AUTOMATIC_BUF_SIZE, MAX_AUTOMATIC_BUF_SIZE, MIN_AUTOMATIC_BUF_SIZE, STDIN_FILE,
1111
};
12+
#[cfg(test)]
13+
use uucore::parser::parse_size::MEGA;
1214

1315
// Heuristics to size the external sort buffer without overcommit memory.
1416
pub(crate) fn automatic_buffer_size(files: &[OsString]) -> usize {
@@ -135,15 +137,15 @@ mod tests {
135137

136138
#[test]
137139
fn desired_buffer_matches_total_when_small() {
138-
let six_mebibytes = 6 * 1024 * 1024;
140+
let six_mebibytes = 6 * MEGA;
139141
let expected = ((six_mebibytes as u128) * 12)
140142
.clamp(six_mebibytes as u128, crate::MAX_AUTOMATIC_BUF_SIZE as u128);
141143
assert_eq!(desired_file_buffer_bytes(six_mebibytes as u128), expected);
142144
}
143145

144146
#[test]
145147
fn desired_buffer_caps_at_max_for_large_inputs() {
146-
let large = 256 * 1024 * 1024; // 256 MiB
148+
let large = 256 * MEGA; // 256 MiB
147149
assert_eq!(
148150
desired_file_buffer_bytes(large as u128),
149151
crate::MAX_AUTOMATIC_BUF_SIZE as u128

src/uu/sort/src/chunks.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ use uucore::error::{UResult, USimpleError};
2222
use crate::{
2323
GeneralBigDecimalParseResult, GlobalSettings, Line, SortMode, numeric_str_cmp::NumInfo,
2424
};
25+
use uucore::parser::parse_size::MEGA;
2526

26-
const MAX_TOKEN_BUFFER_BYTES: usize = 4 * 1024 * 1024;
27+
const MAX_TOKEN_BUFFER_BYTES: usize = 4 * MEGA;
2728
const MAX_TOKEN_BUFFER_ELEMS: usize = MAX_TOKEN_BUFFER_BYTES / std::mem::size_of::<Range<usize>>();
2829

2930
self_cell!(
@@ -374,7 +375,7 @@ fn read_to_buffer<T: Read>(
374375

375376
// We need to read more lines
376377
let len = buffer.len();
377-
let grow_by = (len / 2).max(1024 * 1024);
378+
let grow_by = (len / 2).max(MEGA);
378379
buffer.resize(len + grow_by, 0);
379380
read_target = &mut buffer[len..];
380381
} else {

src/uu/sort/src/ext_sort.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use crate::{
3535
compare_by, merge, sort_by,
3636
};
3737
use crate::{Line, print_sorted};
38+
use uucore::parser::parse_size::MEGA;
3839

3940
// Note: update `test_sort::test_start_buffer` if this size is changed
4041
const START_BUFFER_SIZE: usize = 8_000;
@@ -116,11 +117,11 @@ fn reader_writer<
116117
// Cap oversized buffer requests to avoid unnecessary allocations and give the automatic
117118
// heuristic room to grow when the user does not provide an explicit value.
118119
let mut buffer_size = match settings.buffer_size {
119-
size if size <= 512 * 1024 * 1024 => size,
120+
size if size <= 512 * MEGA => size,
120121
size => size / 2,
121122
};
122123
if !settings.buffer_size_is_explicit {
123-
buffer_size = buffer_size.max(8 * 1024 * 1024);
124+
buffer_size = buffer_size.max(8 * MEGA);
124125
}
125126
let read_result: ReadResult<Tmp> = read_write_loop(
126127
files,

src/uu/sort/src/sort.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ use uucore::i18n::collator::locale_cmp;
5252
use uucore::i18n::decimal::locale_decimal_separator;
5353
use uucore::line_ending::LineEnding;
5454
use uucore::parser::num_parser::{ExtendedParser, ExtendedParserError};
55+
#[cfg(test)]
56+
use uucore::parser::parse_size::{EXA, TERA};
57+
use uucore::parser::parse_size::{GIGA, KILO, MEGA};
5558
use uucore::parser::parse_size::{ParseSizeError, Parser};
5659
use uucore::parser::shortcut_value_parser::ShortcutValueParser;
5760
use uucore::posix::{MODERN, TRADITIONAL};
@@ -122,18 +125,6 @@ fn locale_decimal_pt() -> u8 {
122125
const NEGATIVE: &u8 = &b'-';
123126
const POSITIVE: &u8 = &b'+';
124127

125-
// Non-breaking space constants
126-
const UTF8_NBSP: &[u8] = &[0xc2, 0xa0]; // UTF-8 encoding of non-breaking space (U+00A0)
127-
const ISO_NBSP: u8 = 0xa0; // ISO 8859-1 non-breaking space
128-
129-
// SI unit constants for byte parsing
130-
const KILO: usize = 1024;
131-
const MEGA: usize = 1024 * 1024;
132-
const GIGA: usize = 1024 * 1024 * 1024;
133-
const TERA: usize = 1024 * 1024 * 1024 * 1024;
134-
const PETA: usize = 1024 * 1024 * 1024 * 1024 * 1024;
135-
const EXA: usize = 1024 * 1024 * 1024 * 1024 * 1024 * 1024;
136-
137128
// The automatic buffer heuristics clamp to this range to avoid
138129
// over-committing memory on constrained systems while still keeping
139130
// reasonably large chunks for typical workloads.
@@ -3207,7 +3198,7 @@ mod tests {
32073198
("1b", 1),
32083199
("1024b", KILO),
32093200
("1024Mb", KILO * MEGA), // NOTE: This might not be how GNU `sort` behaves for 'Mb'
3210-
("1", KILO), // K is default
3201+
("1", KILO), // K is default
32113202
("50", 50 * KILO),
32123203
("K", KILO),
32133204
("k", KILO),

src/uucore/src/lib/features/parser/parse_size.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66

77
//! Parser for sizes in SI or IEC units (multiples of 1000 or 1024 bytes).
88
9+
// SI unit constants for byte parsing (powers of 1024)
10+
pub const KILO: usize = 1024;
11+
pub const MEGA: usize = 1024 * 1024;
12+
pub const GIGA: usize = 1024 * 1024 * 1024;
13+
pub const TERA: usize = 1024 * 1024 * 1024 * 1024;
14+
pub const PETA: usize = 1024 * 1024 * 1024 * 1024 * 1024;
15+
pub const EXA: usize = 1024 * 1024 * 1024 * 1024 * 1024 * 1024;
16+
917
use std::error::Error;
1018
use std::fmt;
1119
use std::num::{IntErrorKind, ParseIntError};

0 commit comments

Comments
 (0)