Skip to content

Commit

Permalink
sort: migrate from ouroboros to self_cell
Browse files Browse the repository at this point in the history
  • Loading branch information
cakebaker committed Jun 19, 2023
1 parent 66723e0 commit 6ebfaf4
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 79 deletions.
67 changes: 7 additions & 60 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,6 @@ num-traits = "0.2.15"
number_prefix = "0.4"
once_cell = "1.18.0"
onig = { version = "~6.4", default-features = false }
ouroboros = "0.15.6"
parse_datetime = "0.4.0"
phf = "0.11.1"
phf_codegen = "0.11.1"
Expand All @@ -314,6 +313,7 @@ regex = "1.8.4"
rstest = "0.17.0"
rust-ini = "0.19.0"
same-file = "1.0.6"
self_cell = "1.0.0"
selinux = "0.4"
signal-hook = "0.3.15"
smallvec = { version = "1.10", features = ["union"] }
Expand Down
2 changes: 1 addition & 1 deletion src/uu/sort/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ ctrlc = { workspace = true }
fnv = { workspace = true }
itertools = { workspace = true }
memchr = { workspace = true }
ouroboros = { workspace = true }
rand = { workspace = true }
rayon = { workspace = true }
self_cell = { workspace = true }
tempfile = { workspace = true }
unicode-width = { workspace = true }
uucore = { workspace = true, features = ["fs"] }
Expand Down
31 changes: 16 additions & 15 deletions src/uu/sort/src/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,22 @@ use std::{
};

use memchr::memchr_iter;
use ouroboros::self_referencing;
use self_cell::self_cell;
use uucore::error::{UResult, USimpleError};

use crate::{numeric_str_cmp::NumInfo, GeneralF64ParseResult, GlobalSettings, Line, SortError};

/// The chunk that is passed around between threads.
/// `lines` consist of slices into `buffer`.
#[self_referencing(pub_extras)]
#[derive(Debug)]
pub struct Chunk {
pub buffer: Vec<u8>,
#[borrows(buffer)]
#[covariant]
pub contents: ChunkContents<'this>,
}
self_cell!(
/// The chunk that is passed around between threads.
pub struct Chunk {
owner: Vec<u8>,

#[covariant]
dependent: ChunkContents,
}

impl {Debug}
);

#[derive(Debug)]
pub struct ChunkContents<'a> {
Expand All @@ -48,7 +49,7 @@ pub struct LineData<'a> {
impl Chunk {
/// Destroy this chunk and return its components to be reused.
pub fn recycle(mut self) -> RecycledChunk {
let recycled_contents = self.with_contents_mut(|contents| {
let recycled_contents = self.with_dependent_mut(|_, contents| {
contents.lines.clear();
contents.line_data.selections.clear();
contents.line_data.num_infos.clear();
Expand Down Expand Up @@ -81,15 +82,15 @@ impl Chunk {
selections: recycled_contents.1,
num_infos: recycled_contents.2,
parsed_floats: recycled_contents.3,
buffer: self.into_heads().buffer,
buffer: self.into_owner(),
}
}

pub fn lines(&self) -> &Vec<Line> {
&self.borrow_contents().lines
&self.borrow_dependent().lines
}
pub fn line_data(&self) -> &LineData {
&self.borrow_contents().line_data
&self.borrow_dependent().line_data
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/uu/sort/src/ext_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ fn reader_writer<
/// The function that is executed on the sorter thread.
fn sorter(receiver: &Receiver<Chunk>, sender: &SyncSender<Chunk>, settings: &GlobalSettings) {
while let Ok(mut payload) = receiver.recv() {
payload.with_contents_mut(|contents| {
payload.with_dependent_mut(|_, contents| {
sort_by(&mut contents.lines, settings, &contents.line_data);
});
if sender.send(payload).is_err() {
Expand Down
2 changes: 1 addition & 1 deletion src/uu/sort/src/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ impl<'a> FileMerger<'a> {
file_number: file.file_number,
});

file.current_chunk.with_contents(|contents| {
file.current_chunk.with_dependent(|_, contents| {
let current_line = &contents.lines[file.line_idx];
if settings.unique {
if let Some(prev) = &prev {
Expand Down

0 comments on commit 6ebfaf4

Please sign in to comment.