-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
replace @setRuntimeSafety with @optimizeFor #978
Comments
@andrewrk Just a small correction, I believe the settings I used for ReleaseSmall are equivalent to -Oz. |
Can you expand on the use case for leaving in You mention this cool idea:
which is how precisely Go maps behave. But is that perhaps more of a safety feature than an optimization (or debugging) feature? |
@hryx it would be really useful for debugging to be able to turn of optimizations for a specific part of code. You can do this in C. Also it doesn't seem to be addressed here, but a struct has to be the same for the whole program, so where is its mode set? |
Not quite, you can ask not to optimize some functions ( |
I think runtime safety should be independent of the mode of optimization. For example, in a release build it may make sense to compile hot spots with |
love it! would recommend |
Hello, my suggestion is replacing the generic optimization attribute by something like
|
This proposal does not clarify how the standard syntax for tests would then look like. It would be very ugly/annoying, if it is more than 1 line. As of now it is |
the automatic security is great, I like most of the warnings. What I mean: sometimes you want to have one field for two different states.
But this semantics shows explicit memory copying. But we weren't going to copy the memory, were we? however, it is obvious that I do not need copying ... If the structure implies one field, it is obvious that it should be read, i.e.: Using release doesn't suit me because the debug mod with all the other built-in checks is exactly what I need at the development stage. Rather, even I would like to have some checks in release mode too.
I read about extern and packed unions, they do not suit me because:
Now I have placed a lot of |
This code is overcomplicated. You can simply write
Optimizations naturally suppress copies of data in basically every language. In C, for instance, Moreover, the point of this rule is that there could be copying. Zig reserves the right to lay out auto-layout (i.e. not |
Right now you can use
@setRuntimeSafety(false)
in Debug and ReleaseSafe modes to allow the optimizer to speed up particular blocks of code. This would make sense to do in a well tested high performance crypto algorithm for example. Or in general the bottlenecks of your library or program.So the pattern here is that the build mode specifies the default, and you can override it at a per-block (or function) level. Here's a table of the parameters and the defaults that the build modes set:
With
@setRuntimeSafety
we can make a block of code adhere to the principles of Debug even in a ReleaseFast build, and vice versa. ...to some extent - it does not affect optimizations.But it could! LLVM supports choosing
-O3
,-Os
, or-O0
at least at the function level.You can probably guess what I'm leading up to here. Really instead of specifically asking to turn off safety, what we really want to communicate to the compiler is what we want to optimize for.
@optimizeFor(comptime mode: BuildMode)
Where
BuildMode
is already defined as@import("builtin").BuildMode
:And then the other issue we need to solve here is that the same intent should be communicable for types. Zig reserves the right to order fields and insert padding and such for structs, and therefore we should also allow the programmer to tell Zig what to optimize for.
For example Debug might randomly order fields to make sure one does not accidentally depend on field order. ReleaseFast might arrange fields in an order most likely to have better cache performance. ReleaseSmall might sort fields by descending alignment to minimize padding.
One little tidbit:
ReleaseSmall
is about generated binary size, not runtime memory size. So there is a slight disconnect here. But "small" is definitely something you would potentially want to optimize for in a struct, union, or enum. We can think about how to name the release modes and the optimization targets more coherently.The text was updated successfully, but these errors were encountered: