Skip to content

Commit

Permalink
C#: make keywords static to avoid allocations (bytecodealliance#1111)
Browse files Browse the repository at this point in the history
* Minor clean up to files

* use static list instead of regenerating each time

* Clean up

Signed-off-by: James Sturtevant <jsturtevant@gmail.com>

---------

Signed-off-by: James Sturtevant <jsturtevant@gmail.com>
  • Loading branch information
jsturtevant authored Jan 6, 2025
1 parent 9b91987 commit 898b833
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 23 deletions.
9 changes: 5 additions & 4 deletions crates/csharp/src/csharp_ident.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use heck::{ToLowerCamelCase, ToUpperCamelCase};

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

impl ToCSharpIdent for str {
// Source: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/
fn csharp_keywords() -> Vec<&'static str> {
vec![
fn csharp_keywords() -> &'static [&'static str] {
static CSHARP_KEY_WORDS: &[&str] = &[
"abstract",
"as",
"base",
Expand Down Expand Up @@ -87,7 +87,8 @@ impl ToCSharpIdent for str {
"void",
"volatile",
"while",
]
];
CSHARP_KEY_WORDS
}

fn to_csharp_ident(&self) -> String {
Expand Down
10 changes: 5 additions & 5 deletions crates/csharp/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
1 => {
let mut payload_is_void = false;
let mut previous = operands[0].clone();
let mut vars: Vec::<(String, Option<String>)> = Vec::with_capacity(self.results.len());
let mut vars: Vec<(String, Option<String>)> = Vec::with_capacity(self.results.len());
if let Direction::Import = self.interface_gen.direction {
for ty in &self.results {
let tmp = self.locals.tmp("tmp");
Expand Down Expand Up @@ -1132,7 +1132,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
);
self.fixed = self.fixed + 1;

return format!("{ptr}");
format!("{ptr}")
}
Direction::Export => {
self.interface_gen.csharp_gen.return_area_size =
Expand All @@ -1148,7 +1148,7 @@ impl Bindgen for FunctionBindgen<'_, '_> {
);
self.interface_gen.csharp_gen.needs_export_return_area = true;

return format!("{ptr}");
format!("{ptr}")
}
}
}
Expand Down Expand Up @@ -1183,8 +1183,8 @@ impl Bindgen for FunctionBindgen<'_, '_> {
self.blocks.push(Block {
body: mem::replace(&mut self.src, body),
results: mem::take(operands),
element: element,
base: base,
element,
base,
});
}

Expand Down
2 changes: 1 addition & 1 deletion crates/csharp/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub(crate) struct InterfaceTypeAndFragments {
impl InterfaceTypeAndFragments {
pub(crate) fn new(is_export: bool) -> Self {
InterfaceTypeAndFragments {
is_export: is_export,
is_export,
interface_fragments: Vec::<InterfaceFragment>::new(),
}
}
Expand Down
18 changes: 5 additions & 13 deletions crates/csharp/src/world_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ impl WorldGenerator for CSharp {
//TODO: This is currently needed for mono even if it's built as a library.
if self.opts.runtime == CSharpRuntime::Mono {
files.push(
&format!("MonoEntrypoint.cs",),
&"MonoEntrypoint.cs".to_string(),
indent(&format!(
r#"
{access} class MonoEntrypoint() {{
Expand Down Expand Up @@ -825,18 +825,10 @@ enum Stubs<'a> {
// so for byte it would always use 1 regardless of the "Pack".
pub fn dotnet_aligned_array(array_size: usize, required_alignment: usize) -> (usize, String) {
match required_alignment {
1 => {
return (array_size, "byte".to_owned());
}
2 => {
return ((array_size + 1) / 2, "ushort".to_owned());
}
4 => {
return ((array_size + 3) / 4, "uint".to_owned());
}
8 => {
return ((array_size + 7) / 8, "ulong".to_owned());
}
1 => (array_size, "byte".to_owned()),
2 => ((array_size + 1) / 2, "ushort".to_owned()),
4 => ((array_size + 3) / 4, "uint".to_owned()),
8 => ((array_size + 7) / 8, "ulong".to_owned()),
_ => todo!("unsupported return_area_align {}", required_alignment),
}
}
Expand Down

0 comments on commit 898b833

Please sign in to comment.