Skip to content

Commit

Permalink
Merge pull request #172 from dandi/compact-component
Browse files Browse the repository at this point in the history
Reduce heap usage of various small strings
  • Loading branch information
jwodder authored Jul 22, 2024
2 parents c981061 + e766c8b commit 8455136
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/paths/component.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use smartstring::alias::CompactString;
use thiserror::Error;

/// A nonempty path component that does not contain a forward slash or NUL nor
/// equals `.` or `..`
#[derive(Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub(crate) struct Component(pub(super) String);
pub(crate) struct Component(pub(super) CompactString);

fn validate(s: &str) -> Result<(), ParseComponentError> {
if s.is_empty() {
Expand Down
4 changes: 2 additions & 2 deletions src/paths/dirpath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl PureDirPath {
}

pub(crate) fn name(&self) -> Component {
Component(self.name_str().to_owned())
Component(self.name_str().into())
}

pub(crate) fn parent(&self) -> Option<PureDirPath> {
Expand Down Expand Up @@ -83,7 +83,7 @@ impl PureDirPath {

impl From<Component> for PureDirPath {
fn from(value: Component) -> PureDirPath {
let mut s = value.0;
let mut s = String::from(value);
s.push('/');
PureDirPath(s)
}
Expand Down
4 changes: 2 additions & 2 deletions src/paths/purepath.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl PurePath {
}

pub(crate) fn components(&self) -> impl Iterator<Item = Component> + '_ {
self.0.split('/').map(|c| Component(c.to_owned()))
self.0.split('/').map(|c| Component(c.into()))
}

pub(crate) fn push(&mut self, c: &Component) {
Expand All @@ -96,7 +96,7 @@ impl PurePath {

impl From<Component> for PurePath {
fn from(value: Component) -> PurePath {
PurePath(value.0)
PurePath(value.into())
}
}

Expand Down

0 comments on commit 8455136

Please sign in to comment.