-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
sort: Replace malloc and 0 fill with huge reserve & min 0 fill #10975
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
oech3
wants to merge
1
commit into
uutils:main
Choose a base branch
from
oech3:sort-malloc0
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ use crate::{ | |
| GeneralBigDecimalParseResult, GlobalSettings, Line, SortMode, numeric_str_cmp::NumInfo, | ||
| }; | ||
|
|
||
| const MAYBE_L1_CACHE_SIZE: usize = 64 * 1024; | ||
| const MAX_TOKEN_BUFFER_BYTES: usize = 4 * 1024 * 1024; | ||
| const MAX_TOKEN_BUFFER_ELEMS: usize = MAX_TOKEN_BUFFER_BYTES / size_of::<Range<usize>>(); | ||
|
|
||
|
|
@@ -180,7 +181,13 @@ pub fn read<T: Read>( | |
| mut buffer, | ||
| } = recycled_chunk; | ||
| if buffer.len() < carry_over.len() { | ||
| buffer.resize(carry_over.len() + 10 * 1024, 0); | ||
| // keep cost of 0 fill minimal | ||
| // but avoid cost of allocation by reserving huge size too | ||
| buffer.resize(carry_over.len(), 0); | ||
| let new_len = (carry_over.len() * 2) | ||
| .max(MAYBE_L1_CACHE_SIZE) | ||
| .min(carry_over.len() + 16 * 1024 * 1024); | ||
| buffer.reserve(new_len - buffer.len()); | ||
| } | ||
| buffer[..carry_over.len()].copy_from_slice(carry_over); | ||
| let (read, should_continue) = read_to_buffer( | ||
|
|
@@ -252,9 +259,6 @@ fn parse_lines<'a>( | |
| assert!(line_data.parsed_floats.is_empty()); | ||
| assert!(line_data.line_num_floats.is_empty()); | ||
| token_buffer.clear(); | ||
| if token_buffer.capacity() > MAX_TOKEN_BUFFER_ELEMS { | ||
| token_buffer.shrink_to(MAX_TOKEN_BUFFER_ELEMS); | ||
| } | ||
| const SMALL_CHUNK_BYTES: usize = 64 * 1024; | ||
| let mut estimated = (*line_count_hint).max(1); | ||
| let mut exact_line_count = None; | ||
|
|
@@ -267,8 +271,8 @@ fn parse_lines<'a>( | |
| exact_line_count = Some(count); | ||
| estimated = count; | ||
| } else if estimated == 1 { | ||
| const LINE_LEN_HINT: usize = 32; | ||
| estimated = (read.len() / LINE_LEN_HINT).max(1); | ||
| const LINE_LEN_HINT: usize = 128; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mattsu2020 how did you decide to use
This comment was marked as off-topic.
Sorry, something went wrong. |
||
| estimated = (read.len() / LINE_LEN_HINT).clamp(1, 1024); | ||
| } | ||
| lines.reserve(estimated); | ||
| if settings.precomputed.selections_per_line > 0 { | ||
|
|
@@ -349,12 +353,9 @@ fn read_to_buffer<T: Read>( | |
| if max_buffer_size > buffer.len() { | ||
| // we can grow the buffer | ||
| let prev_len = buffer.len(); | ||
| let target = if buffer.len() < max_buffer_size / 2 { | ||
| buffer.len().saturating_mul(2) | ||
| } else { | ||
| max_buffer_size | ||
| }; | ||
| buffer.resize(target.min(max_buffer_size), 0); | ||
| let grow_by = (max_buffer_size - prev_len).min(MAYBE_L1_CACHE_SIZE); | ||
| buffer.reserve(grow_by); | ||
| buffer.resize(prev_len + MAYBE_L1_CACHE_SIZE, 0); | ||
| read_target = &mut buffer[prev_len..]; | ||
| continue; | ||
| } | ||
|
|
@@ -374,8 +375,9 @@ fn read_to_buffer<T: Read>( | |
|
|
||
| // We need to read more lines | ||
| let len = buffer.len(); | ||
| let grow_by = (len / 2).max(1024 * 1024); | ||
| buffer.resize(len + grow_by, 0); | ||
| let grow_by = len.clamp(MAYBE_L1_CACHE_SIZE, 16 * 1024 * 1024); | ||
| buffer.reserve(grow_by); | ||
| buffer.resize(len + MAYBE_L1_CACHE_SIZE, 0); | ||
| read_target = &mut buffer[len..]; | ||
| } else { | ||
| // This file has been fully read. | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add a comment explaining why
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added