Skip to content

Commit 665572e

Browse files
committed
perf: use LinkedList to store list of paths
1 parent a679644 commit 665572e

3 files changed

Lines changed: 16 additions & 20 deletions

File tree

src/hardlink/link_path_list.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,26 @@ pub use reflection::Reflection;
66

77
pub use Reflection as LinkPathListReflection;
88

9-
use std::path::PathBuf;
9+
use pipe_trait::Pipe;
10+
use std::{collections::LinkedList, path::PathBuf};
1011

1112
/// List of different hardlinks to the same file.
1213
///
1314
/// **Serialization and deserialization:** _(feature: `json`)_ `LinkPathList` does not implement
1415
/// `Serialize` and `Deserialize` traits directly, instead, it can be converted into/from a
1516
/// [`Reflection`] which implements these traits.
1617
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
17-
pub struct LinkPathList(
18-
Vec<PathBuf>, // TODO: benchmark against LinkedList<PathBuf>
19-
);
18+
pub struct LinkPathList(LinkedList<PathBuf>);
2019

2120
impl LinkPathList {
2221
/// Create a list of a single path.
2322
pub(crate) fn single(path: PathBuf) -> Self {
24-
LinkPathList(vec![path])
23+
[path].pipe(LinkedList::from).pipe(LinkPathList)
2524
}
2625

2726
/// Add a path to the list.
2827
pub(crate) fn add(&mut self, path: PathBuf) {
29-
self.0.push(path)
28+
self.0.push_back(path);
3029
}
3130

3231
/// Get the number of paths inside the list.

src/hardlink/link_path_list/iter.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use super::LinkPathList;
22
use pipe_trait::Pipe;
3-
use std::{iter::FusedIterator, path::PathBuf, slice};
3+
use std::{collections::linked_list, iter::FusedIterator, path::PathBuf};
44

55
/// [Iterator] over the paths inside a [`LinkPathList`].
66
#[derive(Debug, Clone)]
7-
pub struct Iter<'a>(slice::Iter<'a, PathBuf>);
7+
pub struct Iter<'a>(linked_list::Iter<'a, PathBuf>);
88

99
impl LinkPathList {
1010
/// Iterate over the paths inside the list.
@@ -26,16 +26,6 @@ impl<'a> Iterator for Iter<'a> {
2626
self.0.size_hint()
2727
}
2828

29-
#[inline]
30-
fn count(self) -> usize {
31-
self.0.count()
32-
}
33-
34-
#[inline]
35-
fn nth(&mut self, n: usize) -> Option<Self::Item> {
36-
self.0.nth(n)
37-
}
38-
3929
#[inline]
4030
fn last(self) -> Option<Self::Item> {
4131
self.0.last()

src/hardlink/link_path_list/reflection.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
use super::LinkPathList;
22
use derive_more::{From, Into, IntoIterator};
33
use pipe_trait::Pipe;
4-
use std::{collections::HashSet, path::PathBuf};
4+
use std::{
5+
collections::{HashSet, LinkedList},
6+
path::PathBuf,
7+
};
58

69
#[cfg(feature = "json")]
710
use serde::{Deserialize, Serialize};
@@ -41,6 +44,10 @@ impl From<LinkPathList> for Reflection {
4144

4245
impl From<Reflection> for LinkPathList {
4346
fn from(value: Reflection) -> Self {
44-
value.0.into_iter().collect::<Vec<_>>().pipe(LinkPathList)
47+
value
48+
.0
49+
.into_iter()
50+
.collect::<LinkedList<_>>()
51+
.pipe(LinkPathList)
4552
}
4653
}

0 commit comments

Comments
 (0)