Skip to content

Commit 0738452

Browse files
Refactor out excessive borrowed value optimizations in macros (#1610)
### What Replace borrowed references with owned values in `impl_pub_methods` and `Fn`, which are internal functions and types in the soroban-sdk-macros. ### Why The excessive use of borrowed references and lifetime annotations was adding unnecessary complexity without meaningful performance benefits in macro expansion code that runs at compile time. Several times when I've worked with this code I have found it harder to evolve because of the limitation on data having to be owned externally to these functions.
1 parent eb1b7a8 commit 0738452

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

soroban-sdk-macros/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub fn contractspecfn(metadata: TokenStream, input: TokenStream) -> TokenStream
9595

9696
let derived: Result<proc_macro2::TokenStream, proc_macro2::TokenStream> = methods
9797
.iter()
98-
.map(|m| derive_fn_spec(&args.name, m.ident, m.attrs, m.inputs, m.output, export))
98+
.map(|m| derive_fn_spec(&args.name, &m.ident, &m.attrs, &m.inputs, &m.output, export))
9999
.collect();
100100

101101
match derived {
@@ -239,7 +239,7 @@ pub fn contractimpl(metadata: TokenStream, input: TokenStream) -> TokenStream {
239239
}
240240
.unwrap_or_else(|| "Client".to_string());
241241

242-
let pub_methods: Vec<_> = syn_ext::impl_pub_methods(&imp).collect();
242+
let pub_methods: Vec<_> = syn_ext::impl_pub_methods(&imp);
243243
let derived: Result<proc_macro2::TokenStream, proc_macro2::TokenStream> = pub_methods
244244
.iter()
245245
.map(|m| {
@@ -269,7 +269,7 @@ pub fn contractimpl(metadata: TokenStream, input: TokenStream) -> TokenStream {
269269
crate_path,
270270
ty,
271271
trait_ident,
272-
pub_methods.into_iter(),
272+
pub_methods.iter(),
273273
);
274274
output.extend(quote! { #cfs });
275275
output.into()

soroban-sdk-macros/src/syn_ext.rs

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,15 @@ use syn::{
1717
/// methods that are inherently implemented this is methods that have a pub
1818
/// visibility keyword. For methods that are implementing a trait the pub is
1919
/// assumed and so all methods are returned.
20-
pub fn impl_pub_methods(imp: &ItemImpl) -> impl Iterator<Item = &ImplItemFn> {
20+
pub fn impl_pub_methods(imp: &ItemImpl) -> Vec<ImplItemFn> {
2121
imp.items
2222
.iter()
2323
.filter_map(|i| match i {
24-
ImplItem::Fn(m) => Some(m),
24+
ImplItem::Fn(m) => Some(m.clone()),
2525
_ => None,
2626
})
2727
.filter(|m| imp.trait_.is_some() || matches!(m.vis, Visibility::Public(_)))
28+
.collect()
2829
}
2930

3031
/// Gets methods from the trait.
@@ -161,22 +162,23 @@ impl HasFnsItem {
161162
}
162163
}
163164

164-
pub fn fns(&self) -> Vec<Fn<'_>> {
165+
pub fn fns(&self) -> Vec<Fn> {
165166
match self {
166167
HasFnsItem::Trait(t) => trait_methods(t)
167168
.map(|m| Fn {
168-
ident: &m.sig.ident,
169-
attrs: &m.attrs,
170-
inputs: &m.sig.inputs,
171-
output: &m.sig.output,
169+
ident: m.sig.ident.clone(),
170+
attrs: m.attrs.clone(),
171+
inputs: m.sig.inputs.clone(),
172+
output: m.sig.output.clone(),
172173
})
173174
.collect(),
174175
HasFnsItem::Impl(i) => impl_pub_methods(i)
176+
.iter()
175177
.map(|m| Fn {
176-
ident: &m.sig.ident,
177-
attrs: &m.attrs,
178-
inputs: &m.sig.inputs,
179-
output: &m.sig.output,
178+
ident: m.sig.ident.clone(),
179+
attrs: m.attrs.clone(),
180+
inputs: m.sig.inputs.clone(),
181+
output: m.sig.output.clone(),
180182
})
181183
.collect(),
182184
}
@@ -210,16 +212,16 @@ impl ToTokens for HasFnsItem {
210212
}
211213
}
212214

213-
pub struct Fn<'a> {
214-
pub ident: &'a Ident,
215-
pub attrs: &'a [Attribute],
216-
pub inputs: &'a Punctuated<FnArg, Comma>,
217-
pub output: &'a ReturnType,
215+
pub struct Fn {
216+
pub ident: Ident,
217+
pub attrs: Vec<Attribute>,
218+
pub inputs: Punctuated<FnArg, Comma>,
219+
pub output: ReturnType,
218220
}
219221

220-
impl<'a> Fn<'a> {
222+
impl Fn {
221223
pub fn output(&self) -> Type {
222-
let t = match self.output {
224+
let t = match &self.output {
223225
ReturnType::Default => quote!(()),
224226
ReturnType::Type(_, typ) => match unpack_result(typ) {
225227
Some((t, _)) => quote!(#t),
@@ -229,7 +231,7 @@ impl<'a> Fn<'a> {
229231
Type::Verbatim(t)
230232
}
231233
pub fn try_output(&self, crate_path: &Path) -> Type {
232-
let (t, e) = match self.output {
234+
let (t, e) = match &self.output {
233235
ReturnType::Default => (quote!(()), quote!(#crate_path::Error)),
234236
ReturnType::Type(_, typ) => match unpack_result(typ) {
235237
Some((t, e)) => (quote!(#t), quote!(#e)),

0 commit comments

Comments
 (0)