Sitelet https://github.com/starkware-libs/cairo/commit/3d9e6b395dc307f64a54d5f664f48e92d28bb6c4
Skip to content

Commit 3d9e6b3

Browse files
committed
performance(strings): Using less interim strings.
1 parent 8914649 commit 3d9e6b3

9 files changed

Lines changed: 34 additions & 36 deletions

File tree

‎crates/cairo-lang-lowering/src/lower/flow_control/graph.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ impl<'db> std::fmt::Debug for EnumMatch<'db> {
118118
f,
119119
"EnumMatch {{ matched_var: {:?}, variants: {}}}",
120120
self.matched_var,
121-
self.variants.iter().map(|(_, node, var)| format!("({node:?}, {var:?})")).format(", ")
121+
self.variants
122+
.iter()
123+
.format_with(", ", |(_, node, var), f| f(&format_args!("({node:?}, {var:?})")))
122124
)
123125
}
124126
}

‎crates/cairo-lang-lowering/src/optimizations/cse.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ pub fn cse<'db>(lowered: &mut Lowered<'db>) {
189189
"Some blocks were not processed: [{}]",
190190
block_expression_map
191191
.iter_sorted_by_key(|(k, _)| k.0)
192-
.map(|(k, _)| format!("{k:?}"))
193-
.join(", ")
192+
.format_with(", ", |(k, _), f| f(&format_args!("{k:?}")))
194193
);
195194
let CseContext { var_replacements: renamed_vars, .. } = ctx;
196195
let mut renamer = VarRenamer { renamed_vars };

‎crates/cairo-lang-plugins/src/plugins/derive/destruct.rs‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,11 @@ pub fn handle_destruct(info: &PluginTypeInfo<'_>) -> String {
3232
format!(
3333
"let {ty} {{ {} }} = self;{}",
3434
info.members_info.iter().map(|member| &member.name).format(", "),
35-
info.members_info
36-
.iter()
37-
.map(|member| format!(
38-
"\n{imp}::destruct({});",
39-
member.name,
40-
imp = member.impl_name(DESTRUCT_TRAIT),
41-
))
42-
.format(""),
35+
info.members_info.iter().format_with("", |member, f| f(&format_args!(
36+
"\n{imp}::destruct({});",
37+
member.name,
38+
imp = member.impl_name(DESTRUCT_TRAIT),
39+
))),
4340
)
4441
}
4542
},

‎crates/cairo-lang-plugins/src/plugins/derive/panic_destruct.rs‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,11 @@ pub fn handle_panic_destruct(info: &PluginTypeInfo<'_>) -> String {
3232
format!(
3333
"let {ty} {{ {} }} = self;{}",
3434
info.members_info.iter().map(|member| &member.name).format(", "),
35-
info.members_info
36-
.iter()
37-
.map(|member| format!(
38-
"\n{imp}::panic_destruct({}, ref panic);",
39-
member.name,
40-
imp = member.impl_name(PANIC_DESTRUCT_TRAIT),
41-
))
42-
.format(""),
35+
info.members_info.iter().format_with("", |member, f| f(&format_args!(
36+
"\n{imp}::panic_destruct({}, ref panic);",
37+
member.name,
38+
imp = member.impl_name(PANIC_DESTRUCT_TRAIT),
39+
))),
4340
)
4441
}
4542
},

‎crates/cairo-lang-semantic/src/diagnostic.rs‎

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,7 @@ impl<'db> DiagnosticEntry<'db> for SemanticDiagnostic<'db> {
663663
"Ambiguous path. Multiple matching items: {}",
664664
module_items
665665
.iter()
666-
.map(|item| format!("`{}`", item.full_path(db)))
667-
.format(", ")
666+
.format_with(", ", |item, f| f(&format_args!("`{}`", item.full_path(db))))
668667
)
669668
}
670669
SemanticDiagnosticKind::UseSelfNonMulti => {
@@ -718,10 +717,9 @@ impl<'db> DiagnosticEntry<'db> for SemanticDiagnostic<'db> {
718717
} else {
719718
format!(
720719
" through any of the modules: {}",
721-
containing_modules
722-
.iter()
723-
.map(|module_id| format!("`{}`", module_id.full_path(db)))
724-
.format(", ")
720+
containing_modules.iter().format_with(", ", |module_id, f| {
721+
f(&format_args!("`{}`", module_id.full_path(db)))
722+
})
725723
)
726724
}
727725
)
@@ -779,7 +777,9 @@ impl<'db> DiagnosticEntry<'db> for SemanticDiagnostic<'db> {
779777
SemanticDiagnosticKind::MissingItemsInImpl(item_names) => {
780778
format!(
781779
"Not all trait items are implemented. Missing: {}.",
782-
item_names.iter().map(|name| format!("'{}'", name.long(db))).format(", ")
780+
item_names
781+
.iter()
782+
.format_with(", ", |name, f| f(&format_args!("'{}'", name.long(db))))
783783
)
784784
}
785785
SemanticDiagnosticKind::PassPanicAsNopanic { impl_function_id, trait_id } => {
@@ -918,15 +918,13 @@ impl<'db> DiagnosticEntry<'db> for SemanticDiagnostic<'db> {
918918
if !relevant_traits.is_empty() {
919919
let suggestions = relevant_traits
920920
.iter()
921-
.map(|trait_path| format!("`{trait_path}`"))
922-
.format(", ");
921+
.format_with(", ", |trait_path, f| f(&format_args!("`{trait_path}`")));
923922

924923
format!(
925924
"Method `{}` not found on type `{}`. Consider importing one of the \
926-
following traits: {}.",
925+
following traits: {suggestions}.",
927926
method_name.long(db),
928927
ty.format(db),
929-
suggestions
930928
)
931929
} else {
932930
format!(

‎crates/cairo-lang-semantic/src/expr/inference/solver.rs‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,11 @@ impl<'db> Ambiguity<'db> {
8181
pub fn format(&self, db: &dyn Database) -> String {
8282
match self {
8383
Ambiguity::MultipleImplsFound { concrete_trait_id, impls } => {
84-
let impls_str =
85-
impls.iter().map(|imp| format!("`{}`", imp.format(db))).format(", ");
84+
let impls = impls
85+
.iter()
86+
.format_with(", ", |imp, f| f(&format_args!("`{}`", imp.format(db))));
8687
format!(
87-
"Trait `{:?}` has multiple implementations, in: {impls_str}",
88+
"Trait `{:?}` has multiple implementations, in: {impls}",
8889
concrete_trait_id.debug(db)
8990
)
9091
}

‎crates/cairo-lang-sierra-generator/src/program_generator.rs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,11 @@ impl<'db> DebugWithDb<'db> for SierraProgramWithDebug<'db> {
250250
&self.debug_info.statements_locations.locations.get(&StatementIdx(i))
251251
{
252252
let loc = get_location_marks(db, &loc.first().unwrap().span_in_file(db), true);
253-
writeln!(f, "{}", loc.split('\n').map(|l| format!("// {l}")).format("\n"))?;
253+
writeln!(
254+
f,
255+
"{}",
256+
loc.split('\n').format_with("\n", |l, f| f(&format_args!("// {l}")))
257+
)?;
254258
}
255259
}
256260
}

‎crates/cairo-lang-starknet/src/plugin/dispatcher.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -545,7 +545,7 @@ fn declaration_method_impl<'db>(
545545
RewriteNode::Text(if unwrap {
546546
ret_decode.clone()
547547
} else {
548-
ret_decode.split('\n').map(|x| format!(" {x}")).join("\n")
548+
ret_decode.split('\n').format_with("\n", |x, f| f(&format_args!(" {x}"))).to_string()
549549
})
550550
};
551551
let return_code = RewriteNode::interpolate_patched(

‎crates/cairo-lang-test-runner/src/lib.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ fn print_resource_map(m: impl ExactSizeIterator<Item = (String, usize)>, resourc
555555
if m.len() != 0 {
556556
println!(
557557
" {resource_type}: ({})",
558-
m.into_iter().sorted().map(|(k, v)| format!(r#""{k}": {v}"#)).format(", ")
558+
m.into_iter().sorted().format_with(", ", |(k, v), f| f(&format_args!(r#""{k}": {v}"#)))
559559
);
560560
}
561561
}

0 commit comments

Comments
 (0)