Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C#: make keywords static to avoid allocations #1111

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading