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#: Use stack when the list is smaller than a given value #1144

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
20 changes: 17 additions & 3 deletions crates/csharp/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,13 +853,27 @@ impl Bindgen for FunctionBindgen<'_, '_> {
//TODO: wasm64
let align = self.interface_gen.csharp_gen.sizes.align(element).align_wasm32();

let (array_size, element_type) = crate::world_generator::dotnet_aligned_array(
size,
align,
);
let ret_area = self.locals.tmp("retArea");

self.needs_cleanup = true;
uwrite!(
self.src,
"
var {buffer_size} = {size} * (nuint){list}.Count;
var {address} = NativeMemory.AlignedAlloc({buffer_size}, {align});
cleanups.Add(()=> NativeMemory.AlignedFree({address}));
void* {address};
if (({size} * {list}.Count) < 1024) {{
var {ret_area} = stackalloc {element_type}[({array_size}*{list}.Count)+1];
{address} = (void*)(((int){ret_area}) + ({align} - 1) & -{align});
}}
else
{{
var {buffer_size} = {size} * (nuint){list}.Count;
{address} = NativeMemory.AlignedAlloc({buffer_size}, {align});
cleanups.Add(()=> NativeMemory.AlignedFree({address}));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wishful thinking - not blocked for this PR:

It would be awesome if we could see this in context of some generated sample.
Maybe we can have wit->C# integration test in which the C# file is committed into git ?
That would show up as delta in the generated code in the PR.

}}

for (int {index} = 0; {index} < {list}.Count; ++{index}) {{
{ty} {block_element} = {list}[{index}];
Expand Down
Loading