From 96d86f888652e766b74cf1b9a8e11e482802ad54 Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 2 Aug 2023 18:34:39 +0200 Subject: perf: reduce number of allocations in SynPathExt::to_string --- macros/src/util/syn_path.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'macros/src/util') 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 + }, + ) } } -- cgit v1.2.3-18-g5258