[BurstCompile(OptimizeFor = ...)]

From the 2021.2.0a15 changelog:

  • Burst: Added an OptimizeFor option to [BurstCompile], allowing users to say they want fast code, small code, or fastly compiled code.
  • Burst: Assemblies are now allowed to have an [assembly: BurstCompile()] attribute to let users specify compile options that should apply assembly wide (for instance [assembly: BurstCompile(OptimizeFor = OptimizeFor.FastCompilation)]).

Is this in some way bound to the build configuration (debug/release/master, editor)?

I’m asking because when I iterate in the editor or build a debug Player, I’d prefer OptimizeFor.FastCompilation. When I build a release/master Player, I’d prefer OptimizeFor.Fast.

How would I do this? Do you want us to #ifdef it?

[BurstCompile(OptimizeFor =
#if DEVELOPMENT_BUILD || UNITY_EDITOR
  OptimizeFor.FastCompilation
#else
  OptimizeFor.Fast
#endif
)]
5 Likes

You’d probably use the assembly option for that. So your AssemblyInfo.cs would look like:

#if DEVELOPMENT_BUILD || UNITY_EDITOR
[assembly: BurstCompile(OptimizeFor.FastCompilation)]
#else
[assembly: BurstCompile(OptimizeFor.Fast)]
#endif
1 Like

I don’t really care about the compilation times with Burst at all, but I do care about this:

There totally should be a way to set the default for player (in Burst AOT Settings) and default for editor. You can’t expect everyone to put extra boilerplate on code for every place where you use BurstCompile, that’s just not cool at all.

Yes, we agree. That’s why you should keep OptimizeFor.Default for most of the cases, because that will be the one that we will be able to redirect easily to Fast or FastCompilation. If you explicitly set it to something different from Default, we won’t be able to change it.

3 Likes

Thank you for the response, it did ease my concerns :slight_smile: Is there any estimate when we could expect the settings for these? eg. for 2021.2 or 2022+?

The changes are in Burst 1.6, so we should be able to land this as part of Burst 1.6 as it is a kind of usability bug fix. So hopefully in the coming weeks.

[Edit] Note that Burst 1.6 is still in RC (release candidate) until 2021.2 is final and we will likely have several RC before a final version later this year.

2 Likes

It might be more convenient to #if a const value:

#if DEVELOPMENT_BUILD || UNITY_EDITOR
    public const OptimizeFor OptimizeForDevelopment = OptimizeFor.FastCompilation;
#else
    public const OptimizeFor OptimizeForDevelopment = OptimizeFor.Fast;
#endif

So you can then just use

[BurstCompile(OptimizeFor = OptimizeForDevelopment)]

everywhere. Ultimately though, proper built-in support for player/editor defaults would be far superior.

2 Likes