slint icon indicating copy to clipboard operation
slint copied to clipboard

Compile Slow

Open Adancurusul opened this issue 2 years ago • 11 comments

Whether in linux or windows. rust is very slow at the end of the compilation Even if I don't modify any files he still takes more than 2 minutes The following two graphs show the output of the compilation in linux without changing the code. 1a8001861f698e10b7870225e82494d f8136598a8d7f4dc2c988e6d3babedd Here are two screenshots of the output using --timings image fb92ef9ad2c76a5bf564056ec98ca29

This slows down my development. Is this due to a problem with my code?I can provide my source code if needed.

Adancurusul avatar Dec 05 '23 07:12 Adancurusul

Here is the code : https://github.com/Adancurusul/slint_bug

Adancurusul avatar Dec 05 '23 08:12 Adancurusul

Slint does generate a lot of code, especially when embedding resources, which can take long to process.

For me it is 19s in the build script and 45s in the build itself.

hunger avatar Dec 05 '23 13:12 hunger

rustc self-profile from the above project (anything above 1% of time, there is a log tail of functions below this still):

139➜  summarize summarize hot_cold_plate-0643622.mm_profdata
+-------------------------------------------------------------------------+-----------+-----------------+----------+------------+-----------------------+---------------------------------+
| Item                                                                    | Self time | % of total time | Time     | Item count | Incremental load time | Incremental result hashing time |
+-------------------------------------------------------------------------+-----------+-----------------+----------+------------+-----------------------+---------------------------------+
| live_symbols_and_ignored_derived_traits                                 | 20.42s    | 29.497          | 20.71s   | 1          | 0.00ns                | 1.50ms                          |
+-------------------------------------------------------------------------+-----------+-----------------+----------+------------+-----------------------+---------------------------------+
| LLVM_module_codegen_emit_obj                                            | 13.68s    | 19.759          | 13.68s   | 3          | 0.00ns                | 0.00ns                          |
+-------------------------------------------------------------------------+-----------+-----------------+----------+------------+-----------------------+---------------------------------+
| LLVM_passes                                                             | 9.20s     | 13.288          | 9.20s    | 1          | 0.00ns                | 0.00ns                          |
+-------------------------------------------------------------------------+-----------+-----------------+----------+------------+-----------------------+---------------------------------+
| finish_ongoing_codegen                                                  | 5.68s     | 8.208           | 5.69s    | 1          | 0.00ns                | 0.00ns                          |
+-------------------------------------------------------------------------+-----------+-----------------+----------+------------+-----------------------+---------------------------------+
| expand_crate                                                            | 2.57s     | 3.707           | 3.01s    | 1          | 0.00ns                | 0.00ns                          |
+-------------------------------------------------------------------------+-----------+-----------------+----------+------------+-----------------------+---------------------------------+
| early_lint_checks                                                       | 2.24s     | 3.235           | 2.28s    | 1          | 0.00ns                | 1.53µs                          |
+-------------------------------------------------------------------------+-----------+-----------------+----------+------------+-----------------------+---------------------------------+
| codegen_module                                                          | 2.01s     | 2.898           | 2.45s    | 2          | 0.00ns                | 0.00ns                          |
+-------------------------------------------------------------------------+-----------+-----------------+----------+------------+-----------------------+---------------------------------+
| run_linker                                                              | 1.45s     | 2.097           | 1.45s    | 1          | 0.00ns                | 0.00ns                          |
+-------------------------------------------------------------------------+-----------+-----------------+----------+------------+-----------------------+---------------------------------+
| monomorphization_collector_graph_walk                                   | 1.29s     | 1.861           | 2.64s    | 1          | 0.00ns                | 0.00ns                          |
+-------------------------------------------------------------------------+-----------+-----------------+----------+------------+-----------------------+---------------------------------+
| hir_crate                                                               | 936.88ms  | 1.353           | 3.34s    | 1          | 0.00ns                | 1.57µs                          |
+-------------------------------------------------------------------------+-----------+-----------------+----------+------------+-----------------------+---------------------------------+
| late_resolve_crate                                                      | 750.38ms  | 1.084           | 751.23ms | 1          | 0.00ns                | 0.00ns                          |
+-------------------------------------------------------------------------+-----------+-----------------+----------+------------+-----------------------+---------------------------------+

hunger avatar Dec 05 '23 13:12 hunger

That's weird, what CPU are you using. I'm using an AMD 5800u laptop and a 5900X desktop. Both tests came back with this effect. My friend with an intel CPU tests the build time to be much shorter. And the screenshots I've taken are from code I haven't changed, so theoretically it shouldn't take too long to compile. Moreover, the CPU usage is very low during the compilation process. This is very confusing to me.

Adancurusul avatar Dec 06 '23 00:12 Adancurusul

I have a "AMD Ryzen 9 3900X 12-Core Processor" according to /proc/cpuinfo.

hunger avatar Dec 06 '23 10:12 hunger

With my app, adding space at the end of slint file takes 33seconds(7 build script, 26 second rest) - i7 4770 - 4/8 HT But I use by default mold linker which should be faster than llvm linker - https://github.com/rui314/mold

Also I use cranelift - https://github.com/rust-lang/rustc_codegen_cranelift which decrease time to compile entire project from 7 minutes to 2m 30s

cargo build

become

CARGO_PROFILE_DEV_CODEGEN_BACKEND=cranelift cargo +nightly build -Zcodegen-backend

qarmin avatar Dec 09 '23 21:12 qarmin

I'm not sure why I didn't notice this before(I don't think it occurred before), but building an application in release mode takes now over 87 minutes, of which it takes about 83 minutes to compile my application. In debug mode, the whole thing(which also means compiling all dependencies) takes 8 minutes, which is not a big problem.

Cargo timings

qarmin avatar Feb 20 '24 12:02 qarmin

https://github.com/rust-lang/rust/issues/121354#issuecomment-1955037792

https://gist.github.com/Nilstrieb/b3ec10b0408b75bfc5d68a3871d3d230 This is one of the files that slint helpfully generates for us for the main_window file, I assume. I.. certainly don't blame SROA (which "ungroups" local variable structs into one local variable for every field) for being pathologically slow here.... that is.. a whole very lot of fields.. The fix here probably is to make SROA stop after some number of operations, to stop it from running off and turning this enormous mess into some rainbow wonderland where everything is a local variable after 80 minutes.

But there's also something pretty weird with the code. This struct has dozens of thousands of nested fields and a size of 241KB, which seems.. fun. Never having used slint, your source code looks reasonable to me, but slint generates quite a monstrosity from it... In the meantime, you can use -Zmir-enable-passes=-ScalarReplacementOfAggregates on the nightly compiler to disable this pass (though the compile times are still pretty bad, understandably).

qarmin avatar Feb 20 '24 21:02 qarmin

Slint 1.7 contains some optimization that reduce a bit the size of the generated code and that helps a bit with the compilation time.

ogoffart avatar Jul 30 '24 13:07 ogoffart

It is indeed much faster than 1.5 Here is a comparison of the compilation of an old project cargo run -r --timings

  • before (1.5 img_v3_02cr_ae28ab4d-effa-4eaa-9607-7aee5fdee88g img_v3_02cr_1208dcc1-abae-45dc-81a1-0c65cd4dc3dg
  • after using 1.7 img_v3_02da_bf753c9f-3d6b-4933-b426-7e7fae6aa08g img_v3_02da_ee4e1f99-9374-45c8-b212-db09f860d12g

Thanks a lot!

Adancurusul avatar Jul 31 '24 00:07 Adancurusul