Skip to content

Conversation

@chqrlie
Copy link
Contributor

@chqrlie chqrlie commented Oct 18, 2025

  • make all local variables zero-initialized except if tagged as @(noinit)
  • update tests

@bvdberg
Copy link
Member

bvdberg commented Oct 20, 2025

The generated C code creates something like this for integers:

int32_t from = { 0 };

I was surprised that this was allowed at all, but I think it would be more readable to just generate

int32_t from = 0;

and only generate braces for structs, etc

I did measure the performance and saw no change

@chqrlie
Copy link
Contributor Author

chqrlie commented Oct 20, 2025

I agree this PR needs polishing to this regard. My goal is for you to test the performance impact.
The main issue is probably the initialization of local arrays, especially if they are large. This can be avoided with the @noinit attribute, but I would prefer the analyser to be able to determine that full initialization is unnecessary.

@chqrlie chqrlie force-pushed the zero-init branch 2 times, most recently from 48daab1 to a33c80a Compare October 20, 2025 20:59
@lerno
Copy link
Collaborator

lerno commented Oct 20, 2025

It should easily opt out zero init, already at -O1. This works fine here at least: https://godbolt.org/z/M4sTEr5zv

@chqrlie
Copy link
Contributor Author

chqrlie commented Oct 20, 2025

It should easily opt out zero init, already at -O1. This works fine here at least: https://godbolt.org/z/M4sTEr5zv

Redundant initialization seems easy to handle.

How about this one:

module test;
import stdio local;

fn void main() {
    char buf[4096];
    snprintf(buf, sizeof(buf), "Hello world");
    printf("%s\n", buf);
    return 0;
}

@lerno
Copy link
Collaborator

lerno commented Oct 20, 2025

That's something that would require @noinit. Note that it would not be correct to elide the implicit zeroing in this case, since the bytes not affected by snprintf will not be modified, and that might be what the programmer intended.

@chqrlie
Copy link
Contributor Author

chqrlie commented Oct 20, 2025

That's something that would require @noinit. Note that it would not be correct to elide the implicit zeroing in this case, since the bytes not affected by snprintf will not be modified, and that might be what the programmer intended.

Indeed snprintf does not affect the bytes beyond offset 11, but since printf never reads them, leaving them uninitialized is a palatable optimisation. This is very similar to the case of unused local variables: I suppose C3 does not initialize them either.

@lerno
Copy link
Collaborator

lerno commented Oct 20, 2025

It would be an optimization, but none that the compiler can do without inlining the functions. If it did, then the zero init would be removed I'm sure. Unused local variables are zero init:ed but -O1 will just remove them no problem.

@chqrlie
Copy link
Contributor Author

chqrlie commented Oct 20, 2025

It would be an optimization, but none that the compiler can do without inlining the functions. If it did, then the zero init would be removed I'm sure. Unused local variables are zero init:ed but -O1 will just remove them no problem.

I agree with this approach and that's what I would like to achieve through static analysis in many cases.

@chqrlie chqrlie force-pushed the zero-init branch 3 times, most recently from 15351ae to 0990889 Compare November 5, 2025 08:02
@chqrlie chqrlie force-pushed the zero-init branch 6 times, most recently from 9ebb3a6 to 1e4ddb0 Compare November 10, 2025 11:09
@chqrlie chqrlie force-pushed the zero-init branch 7 times, most recently from ca76deb to 3fda833 Compare November 23, 2025 21:53
* make all local variables zero-initialized except if tagged as `@(noinit)`
* update tests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants