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

Merged
merged 1 commit into from
Jan 30, 2025
Merged
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}));
Copy link
Collaborator

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

There are tons of integration tests in https://github.com/bytecodealliance/wit-bindgen/tree/main/tests

Currently we don't check any into source, but having something like the that does have some value. I typically pull a PR and take a look at the source changes if I am unsure so would find this useful.

@alexcrichton any thoughts on this? I suppose the Rust version is a macro so you don't really have source to check in?

Copy link
Member

Choose a reason for hiding this comment

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

It's possible to finagle but would require some frobbing of the infrastructure. We do this in Wasmtime for example for the output of the wasmtime::component::bindgen! macro and we could do something similar for the generators in this repository. Most generators are already running "codegen tests" to ensure the output compiles and we could probably update that all to save/restore snapshots in-tree. Probably wouldn't run the tests themselves but you'd still be able to see diffs in PRs.

Personally I like having "golden snapshot" tests like that in-tree and I also like to review output changes as opposed to changes-of-how-to-output-code. The only downside is "big PRs" but I find it pretty easy to skim over the generated code changes.

Basically that's a long-winded way of saying that I think it'd be quite reasonable to add this to the repo. Basically run bindings generation for everything in tests/codegen and check in the results in-tree.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I was thinking that we didn't need to have every test in tests/codegen but maybe something like the flavorful example checked in? I think it might be overwhelming to have to many files changed in a PR. Anyways I will open an issue to track.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

}}

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