diff options
author | HampusM <hampus@hampusmat.com> | 2023-08-02 18:34:39 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2023-08-02 18:34:39 +0200 |
commit | 96d86f888652e766b74cf1b9a8e11e482802ad54 (patch) | |
tree | 0fccb563d2363b5cb5ab49839742b8a382174b76 | |
parent | d483166ad2274c6654bb58f1a2b15e1ad740ee4b (diff) |
perf: reduce number of allocations in SynPathExt::to_string
-rw-r--r-- | macros/src/util/syn_path.rs | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/macros/src/util/syn_path.rs b/macros/src/util/syn_path.rs index fc301ab..88182cc 100644 --- a/macros/src/util/syn_path.rs +++ b/macros/src/util/syn_path.rs @@ -1,3 +1,5 @@ +use std::fmt::Write; + use quote::ToTokens; use syn::punctuated::Pair; @@ -11,20 +13,22 @@ impl SynPathExt for syn::Path { fn to_string(&self) -> String { - self.segments - .pairs() - .map(Pair::into_tuple) - .map(|(segment, opt_punct)| { + self.segments.pairs().map(Pair::into_tuple).fold( + String::new(), + |mut acc, (segment, opt_punct)| { let segment_ident = &segment.ident; - format!( - "{}{}", - segment_ident, - opt_punct.map_or_else(String::new, |punct| punct - .to_token_stream() - .to_string()) + write!( + acc, + "{segment_ident}{}", + opt_punct + .map(|punct| punct.to_token_stream().to_string()) + .unwrap_or_default() ) - }) - .collect() + .ok(); + + acc + }, + ) } } |