Skip to content

Commit 898b833

Browse files
authored
C#: make keywords static to avoid allocations (bytecodealliance#1111)
* Minor clean up to files * use static list instead of regenerating each time * Clean up Signed-off-by: James Sturtevant <[email protected]> --------- Signed-off-by: James Sturtevant <[email protected]>
1 parent 9b91987 commit 898b833

File tree

4 files changed

+16
-23
lines changed

4 files changed

+16
-23
lines changed

crates/csharp/src/csharp_ident.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
use heck::{ToLowerCamelCase, ToUpperCamelCase};
22

33
pub(crate) trait ToCSharpIdent: ToOwned {
4-
fn csharp_keywords() -> Vec<&'static str>;
4+
fn csharp_keywords() -> &'static [&'static str];
55
fn to_csharp_ident(&self) -> Self::Owned;
66
fn to_csharp_ident_upper(&self) -> Self::Owned;
77
}
88

99
impl ToCSharpIdent for str {
1010
// Source: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/
11-
fn csharp_keywords() -> Vec<&'static str> {
12-
vec![
11+
fn csharp_keywords() -> &'static [&'static str] {
12+
static CSHARP_KEY_WORDS: &[&str] = &[
1313
"abstract",
1414
"as",
1515
"base",
@@ -87,7 +87,8 @@ impl ToCSharpIdent for str {
8787
"void",
8888
"volatile",
8989
"while",
90-
]
90+
];
91+
CSHARP_KEY_WORDS
9192
}
9293

9394
fn to_csharp_ident(&self) -> String {

crates/csharp/src/function.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
929929
1 => {
930930
let mut payload_is_void = false;
931931
let mut previous = operands[0].clone();
932-
let mut vars: Vec::<(String, Option<String>)> = Vec::with_capacity(self.results.len());
932+
let mut vars: Vec<(String, Option<String>)> = Vec::with_capacity(self.results.len());
933933
if let Direction::Import = self.interface_gen.direction {
934934
for ty in &self.results {
935935
let tmp = self.locals.tmp("tmp");
@@ -1132,7 +1132,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
11321132
);
11331133
self.fixed = self.fixed + 1;
11341134

1135-
return format!("{ptr}");
1135+
format!("{ptr}")
11361136
}
11371137
Direction::Export => {
11381138
self.interface_gen.csharp_gen.return_area_size =
@@ -1148,7 +1148,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
11481148
);
11491149
self.interface_gen.csharp_gen.needs_export_return_area = true;
11501150

1151-
return format!("{ptr}");
1151+
format!("{ptr}")
11521152
}
11531153
}
11541154
}
@@ -1183,8 +1183,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
11831183
self.blocks.push(Block {
11841184
body: mem::replace(&mut self.src, body),
11851185
results: mem::take(operands),
1186-
element: element,
1187-
base: base,
1186+
element,
1187+
base,
11881188
});
11891189
}
11901190

crates/csharp/src/interface.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ pub(crate) struct InterfaceTypeAndFragments {
3232
impl InterfaceTypeAndFragments {
3333
pub(crate) fn new(is_export: bool) -> Self {
3434
InterfaceTypeAndFragments {
35-
is_export: is_export,
35+
is_export,
3636
interface_fragments: Vec::<InterfaceFragment>::new(),
3737
}
3838
}

crates/csharp/src/world_generator.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ impl WorldGenerator for CSharp {
660660
//TODO: This is currently needed for mono even if it's built as a library.
661661
if self.opts.runtime == CSharpRuntime::Mono {
662662
files.push(
663-
&format!("MonoEntrypoint.cs",),
663+
&"MonoEntrypoint.cs".to_string(),
664664
indent(&format!(
665665
r#"
666666
{access} class MonoEntrypoint() {{
@@ -825,18 +825,10 @@ enum Stubs<'a> {
825825
// so for byte it would always use 1 regardless of the "Pack".
826826
pub fn dotnet_aligned_array(array_size: usize, required_alignment: usize) -> (usize, String) {
827827
match required_alignment {
828-
1 => {
829-
return (array_size, "byte".to_owned());
830-
}
831-
2 => {
832-
return ((array_size + 1) / 2, "ushort".to_owned());
833-
}
834-
4 => {
835-
return ((array_size + 3) / 4, "uint".to_owned());
836-
}
837-
8 => {
838-
return ((array_size + 7) / 8, "ulong".to_owned());
839-
}
828+
1 => (array_size, "byte".to_owned()),
829+
2 => ((array_size + 1) / 2, "ushort".to_owned()),
830+
4 => ((array_size + 3) / 4, "uint".to_owned()),
831+
8 => ((array_size + 7) / 8, "ulong".to_owned()),
840832
_ => todo!("unsupported return_area_align {}", required_alignment),
841833
}
842834
}

0 commit comments

Comments
 (0)