Skip to content

Commit cc248fe

Browse files
committed
refactor(component): reduce allocations in ComponentPart::encode()
1 parent b4323b7 commit cc248fe

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

src/dist/component/components.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,16 @@ impl ComponentPart {
198198
const PATH_SEP_MAIN: &str = std::path::MAIN_SEPARATOR_STR;
199199

200200
pub(crate) fn encode(&self) -> String {
201+
let mut buf = self.kind.to_string();
202+
buf.push(':');
203+
// Lossy conversion is safe here because we assume that `path` comes from
204+
// `ComponentPart::decode()`, i.e. from calling `Path::from()` on a `&str`.
201205
let mut path = self.path.to_string_lossy();
202206
if Self::PATH_SEP_MAIN != Self::PATH_SEP_MANIFEST {
203-
// Lossy conversion is safe here because we assume that `path` comes from
204-
// `ComponentPart::decode()`, i.e. from calling `Path::from()` on a `&str`.
205207
path = Cow::Owned(path.replace(Self::PATH_SEP_MAIN, Self::PATH_SEP_MANIFEST));
206208
};
207-
format!("{}:{path}", self.kind)
209+
buf.push_str(&path);
210+
buf
208211
}
209212

210213
pub(crate) fn decode(line: &str) -> Option<Self> {

0 commit comments

Comments
 (0)