-
Notifications
You must be signed in to change notification settings - Fork 56
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
[native_toolchain_c] Optimization level #1267
Comments
Note from @mkustermann: It should probably be |
I don't mind optimizing by default, but maybe we should have at least an option to do -Os. In my experience the performance difference is in most cases negligible and the binary size, especially on mobile, matters. As for Rust, the flags for profiles (debug, release) are configured inside Cargo.toml, the builder should not interfere with that other than choosing the profile. Right now it builds debug build for debug configurations, which we might want to change to build release always. |
Great idea. Yeah so an enum with descriptive messages makes sense. (We don't really have to share them across the different native_toolchain packages, but it would be good if they're called the same.)
Yeah, otherwise we get the same gotcha as with the C++ program in |
Whatever compiler flags Now The API in e.g. We discussed offline also that |
For the Rust builder it doesn't make sense to have an I agree with @mkustermann that the I think the question here is - should the I think especially on Windows if we build the native library with MSVC the debug builds tend to perform much worse than clang and the likelyhood of end-user needed to debug a native assets are quite low. |
The
So maybe we should add Then the
👍 |
Technically we can override cargo profile optimisation levels specified in |
Yeah, so for rust it makes maybe more sense to use profiles. Profiles are user-defined. So that means the RustBuilder probably needs a That then also means that it would need a |
If we (in the future) want the ability to statically link the compiled dart app together with native code from packages, we'd need to ensure the code is compiled in a compatible way (same toolchain, sysroot, ... ?). If one then wants to allow tree shaking the result, one has to pass special flags to compiler & linker (e.g. by passing We may want to support that use case via a different asset kind (which we can introduce in the future) but we should ensure the system is flexible enough to handle that (i.e. a way to pass this compiler-related information down to hook). Of course not every |
I think for now we can just add a class to /// Optimization level for code compilation.
final class OptimizationLevel {
/// The optimization level.
final String _level;
const OptimizationLevel._(this._level);
/// No optimization; prioritize fast compilation.
static const OptimizationLevel O0 = OptimizationLevel._('O0');
/// Basic optimizations; balance compilation speed and code size.
static const OptimizationLevel O1 = OptimizationLevel._('O1');
/// More aggressive optimizations; prioritize code size reduction.
static const OptimizationLevel O2 = OptimizationLevel._('O2');
/// The most aggressive optimizations; prioritize runtime performance.
static const OptimizationLevel O3 = OptimizationLevel._('O3');
/// Optimize for code size, even if it impacts runtime performance.
static const OptimizationLevel Os = OptimizationLevel._('Os');
/// Optimize aggressively for code size, potentially at the cost of
/// compilation time and debugging capabilities.
static const OptimizationLevel Oz = OptimizationLevel._('Oz');
/// Unspecified optimization level; the default or compiler-chosen level.
static const OptimizationLevel unspecified = OptimizationLevel._('unspecified');
/// Returns the string representation of the optimization level.
@override
String toString() => _level;
} Add it as optional parameter to We can add Dart/Flutter passing in a preferred optimization level in the Then we at least (1) by default compile fast, and (2) provide a convenient way for developers to change the optimization level when working on their own package hook. |
This looks good to me, even though I'd probably prefer |
Maybe we should not have a const value in the parameter-list and default to |
Sorry if I step in, but I agree with @knopp since imho the optimization shouldn't be platform dependent but it changes depending on the purpose of your code |
I don't have a super strong opinion about the default. Users can always specify it in the |
I think we should do #1267 (comment), preferably before FlutterconUSA. Otherwise, the next group of enthusiasts is going to be wondering why their native code is slow. Happy to receive a PR, otherwise I'll do it. |
[Not all linkers support `-Os`](https://logs.chromium.org/logs/dart/buildbucket/cr-buildbucket/8730301908709004769/+/u/test_results/new_test_failures__logs_), so defaulting to `-O3` is more portable. Issue: * #1267
Using the
CBuilder
in a build hook does currently not set an optimization flags for the C compiler. This means that by default C/C++ code gets compiled unoptimized, which is a rather bad gotcha in release builds.By comparison, the
plugin_ffi
template compiles C/C++ code optimized in Flutter release builds and unoptimized in development runs (which also turns out to be a gotcha as illustrated by @SaltySpaghetti at #ftcon24eu).Wishlist:
CBuilder
for optimization level, enum-style or opaque style that is automatically mapped to the different compilers. This is more discoverable than compiler flags.OptimizationLevel.unspecified
to be able to pass the optimization level in compiler-specific compiler flags withCBuilder.flags
.CBuilder
constructor, the opt-level should probably be-o3
forBuildMode.release
and-o1
forBuildMode.debug
. (Dart standalone is alwaysBuildMode.release
at the moment, Flutter passes it's--release
and--debug
.)Any other suggestions welcome.
Test requirements:
@knopp @csnewman Maybe we should introduce something similar for the Rust and Go builders.
The text was updated successfully, but these errors were encountered: