Skip to content

Disappointingly small speed-ups on Rust files #257

Closed

Description

@nnethercote

I'm building Firefox on Linux, on my fast i9 machine with 14 physical cores and 28 virtual cores. Here is my mozconfig:

ac_add_options --enable-optimize
ac_add_options --enable-debug
ac_add_options --enable-linker=lld
ac_add_options --disable-elf-hack     # needed because of bug 1423822
ac_add_options --with-ccache=sccache
mk_add_options MOZ_OBJDIR=@TOPSRCDIR@/o64xlldscc

I do a full build (mach build), then do touch servo/components/style/stylist.rs followed by mach build binaries. This rebuilds four rust crates (style, geckoservo, gkrust-shared, gkrust) and then links libxul.so.

sccache kicks in for the rebuild -- sccache -s confirms it:

Compile requests                 5
Compile requests executed        4
Cache hits                       5
Cache misses                     0
Cache timeouts                   0
Cache read errors                0
Forced recaches                  0
Cache write errors               0
Compilation failures             0
Cache errors                     0
Non-cacheable compilations       0
Non-cacheable calls              0
Non-compilation calls            1
Unsupported compiler calls       0
Average cache write          0.000 s
Average cache read miss      0.000 s
Average cache read hit       0.000 s
Cache location             Local disk: "/home/njn/.cache/sccache"
Cache size                      10 GiB
Max cache size                  10 GiB

Here is the time for the sccache rebuild:

real  0m25.376s
user  0m17.324s
sys   0m8.773s

Compare it to the equivalent rebuild when mozconfig doesn't enable sccache:

real  0m28.248s
user  0m36.627s
sys   0m12.212s

With sccache enabled, less work is being done, but the wall time is only 3 seconds better. This is a small enough improvement that I initially thought sccache wasn't working.

Activity

  1. nnethercote commented on Jun 27, 2018

    @nnethercote
    ContributorAuthor

    After doing some more testing, I've come to conclusion that sccache should not be used for Rust files in Firefox for configurations where incremental compilation is enabled (i.e. non-release builds).

    Here are some timings for a nocache config, a --with-ccache=sccache config (which caches C++ and Rust), and a --enable-ccache (which only caches C++).

    Build from scratch:
    nocache 9m6s
    sccache 11m6s
    ccache 9m52s

    touch servo/components/style/stylist.rs + mach build binaries:
    nocache 0m28s
    sccache 0m25s
    ccache 0m29s

    Add comment at start of servo/components/style/stylist.rs + mach build binaries:
    nocache 0m37s
    sccache 1m53s
    ccache 0m37s

    Add comment at end of servo/components/style/stylist.rs + mach build binaries:
    nocache 0m27s
    sccache 1m22s
    ccache 0m28s

    sccache only wins in the unrealistic "touch" case, and in that case only by a small amount. But the costs in the other cases are large, especially the last two "minor change" cases.

  2. na-g commented on Jun 27, 2018

    @na-g

    @nnethercote, is mk_add_options "export RUSTC_WRAPPER=sccache" no longer needed?

  3. nnethercote commented on Jun 27, 2018

    @nnethercote
    ContributorAuthor

    @nnethercote, is mk_add_options "export RUSTC_WRAPPER=sccache" no longer needed?

    My understanding is that:

    1. export RUSTC_WRAPPER=sccache sets sccache as the wrapper for Rust compilation.
    2. ac_add_options --enable-ccache sets ccache as the wrapper for C/C++ compilation.
    3. ac_add_options --with-ccache=sccache sets sccache as the wrapper for Rust and C/C++ compilation.

    So it makes sense to combine 1 with 2, but it doesn't make sense to combine 1 with 3 or 2 with 3.

  4. upsuper commented on Jun 28, 2018

    @upsuper

    I personally only use 3, because ccache isn't claimed to work with MSVC as far as I know. And with 3, there is no way to disable sccache for Rust currently.

  5. luser commented on Jul 2, 2018

    @luser
    Contributor

    sccache only wins in the unrealistic "touch" case, and in that case only by a small amount. But the costs in the other cases are large, especially the last two "minor change" cases.

    I'd like to state that I take issue with your methodology, first (but that's OK, let's find the right practical solution). sccache doesn't have much benefit for incremental rebuilds, even if it's recompiling the same source--the bigger wins are for clobber builds where the same sources are built again, where we can pull them fully from cache.

    That being said, incremental compilation does change the calculus. In #236 @alexcrichton suggested that sccache should refuse to cache compilations where incremental compilation is enabled, which might be sufficient to avoid this--sccache could parse the commandline and refuse to cache, which would result in it simply running rustc every time with a much smaller overhead. If someone wanted to try that out, you'd just need to check for -C incremental=... in the Rust commandline parsing code right here.

    I don't think we should disable sccache entirely for Rust compilation in developer configurations, because cargo only enables incremental compilation for crates in path dependencies (i.e., not from crates.io), which makes sense, as developers are not editing the sources of their crates.io dependencies and doing incremental builds.

    Longer-term we have discussed making sccache able to cache the incremental compilation outputs so that it could do better in this scenario, but I need to have a discussion with @alexcrichton and @michaelwoerister to figure out the details of that and neither of them was at the SF All Hands so it hasn't happened yet. :)

  6. added a commit that references this issue on Jul 3, 2018
    b5c7d5d
  7. luser commented on Jul 3, 2018

    @luser
    Contributor

    OK, I wrote the patch I suggested. I did some tests using a simple test crate. I compared the build time of a fresh build (cargo clean; cargo build), then a build after touch src/lib.rs across rustc without sccache, rustc with sccache from current master, and sccache with the patch I wrote. @nnethercote if you could repeat your tests building Firefox with this version of sccache I would be interested to see the results. I think this should mitigate the overhead and let Rust's incremental compilation do its thing without harming the usefulness of sccache for crates compiled non-incrementally.

    luser@eye7:/build/snippet$ git rev-parse HEAD
    ed5cc54eadb144033d2a4a2a2e94fdf99363f4ef
    
    luser@eye7:/build/snippet$ rustc --version
    rustc 1.27.0 (3eda71b00 2018-06-19)
    
    #====================
    # no sccache
    
    luser@eye7:/build/snippet$ ./time-build.sh
    
    cargo clean; cargo build
    ++ cargo clean
    ++ cargo build
       Compiling itoa v0.3.4
       Compiling snippet v0.1.4-alpha.0 (file:///build/snippet)
        Finished dev [unoptimized + debuginfo] target(s) in 0.90s
    
    touch src/lib.rs; cargo build
    ++ touch src/lib.rs
    ++ cargo build
       Compiling snippet v0.1.4-alpha.0 (file:///build/snippet)
        Finished dev [unoptimized + debuginfo] target(s) in 0.42s
    
    #=====================
    # sccache, empty cache
    
    luser@eye7:/build/snippet$ rm -rf /tmp/sccache-cache; mkdir /tmp/sccache-cache; SCCACHE_DIR=/tmp/sccache-cache RUSTC_WRAPPER=sccache ./time-build.sh
    
    cargo clean; cargo build
    ++ cargo clean
    ++ cargo build
       Compiling itoa v0.3.4
       Compiling snippet v0.1.4-alpha.0 (file:///build/snippet)
        Finished dev [unoptimized + debuginfo] target(s) in 1.42s
    
    touch src/lib.rs; cargo build
    ++ touch src/lib.rs
    ++ cargo build
       Compiling snippet v0.1.4-alpha.0 (file:///build/snippet)
        Finished dev [unoptimized + debuginfo] target(s) in 0.49s
    
    #=====================
    # sccache, full cache
    
    luser@eye7:/build/snippet$ SCCACHE_DIR=/tmp/sccache-cache RUSTC_WRAPPER=sccache ./time-build.sh
    
    cargo clean; cargo build
    ++ cargo clean
    ++ cargo build
       Compiling itoa v0.3.4
       Compiling snippet v0.1.4-alpha.0 (file:///build/snippet)
        Finished dev [unoptimized + debuginfo] target(s) in 0.91s
    
    touch src/lib.rs; cargo build
    ++ touch src/lib.rs
    ++ cargo build
       Compiling snippet v0.1.4-alpha.0 (file:///build/snippet)
        Finished dev [unoptimized + debuginfo] target(s) in 0.48s
    
    #=============================
    # patched sccache, empty cache
    
    luser@eye7:/build/snippet$ rm -rf /tmp/sccache-cache; mkdir /tmp/sccache-cache; SCCACHE_DIR=/tmp/sccache-cache RUSTC_WRAPPER=/build/sccache/target/release/sccache ./time-build.sh
    
    cargo clean; cargo build
    ++ cargo clean
    ++ cargo build
       Compiling itoa v0.3.4
       Compiling snippet v0.1.4-alpha.0 (file:///build/snippet)
        Finished dev [unoptimized + debuginfo] target(s) in 1.24s
    
    touch src/lib.rs; cargo build
    ++ touch src/lib.rs
    ++ cargo build
       Compiling snippet v0.1.4-alpha.0 (file:///build/snippet)
        Finished dev [unoptimized + debuginfo] target(s) in 0.48s
    
    #=============================
    # patched sccache, full cache
    
    luser@eye7:/build/snippet$ SCCACHE_DIR=/tmp/sccache-cache RUSTC_WRAPPER=/build/sccache/target/release/sccache ./time-build.sh
    
    cargo clean; cargo build
    ++ cargo clean
    ++ cargo build
       Compiling itoa v0.3.4
       Compiling snippet v0.1.4-alpha.0 (file:///build/snippet)
        Finished dev [unoptimized + debuginfo] target(s) in 0.91s
    
    touch src/lib.rs; cargo build
    ++ touch src/lib.rs
    ++ cargo build
       Compiling snippet v0.1.4-alpha.0 (file:///build/snippet)
        Finished dev [unoptimized + debuginfo] target(s) in 0.49s
    
  8. nnethercote commented on Jul 5, 2018

    @nnethercote
    ContributorAuthor

    I tried the patch. I can't work out if I did something wrong, but I can't detect any difference between my installed sccache and my locally built one with the patch applied. For my "add a (unique) comment to the start of stylist.rs and recompile" use case:

    • no sccache: 43s
    • system sccache: 2m0s
    • patched sccache: 2m0s

    I added some logging to sccache to work out what was going on. The eprintln! I added to main() in src/main.rs was hit, but the one I added to the top-level parse_arguments() in src/compiler/rust.rs was not hit... which would explain the lack of effect -- the changed code isn't being executed, AFAICT.

    Here's my mozconfig for use with the patched sccache, in case that's useful:

    ac_add_options --enable-optimize
    ac_add_options --enable-debug
    ac_add_options --enable-linker=lld
    ac_add_options --disable-elf-hack
    ac_add_options --with-ccache=/home/njn/moz/sccache/target/release/sccache
    mk_add_options MOZ_OBJDIR=@TOPSRCDIR@/o64xlldscc2
    
  9. luser commented on Jul 5, 2018

    @luser
    Contributor

    Oh, since sccache spawns a background daemon, you'll want to run sccache --stop-server between tests if you're going to test different sccache versions.

  10. nnethercote commented on Jul 5, 2018

    @nnethercote
    ContributorAuthor

    That would explain it! Thanks for the tip. I won't be able to test this again until early next week, I'll report back then.

  11. nnethercote commented on Jul 11, 2018

    @nnethercote
    ContributorAuthor

    Ok, results with sccache --stop-server:

    touch servo/components/style/stylist.rs + mach build binaries:
    - no sccache
        real  0m34.555s
        user  0m46.579s
        sys   0m13.147s
    - sccache
        real  0m30.369s
        user  0m28.090s
        sys   0m8.908s
    - patched sccache
        real  0m33.426s
        user  0m46.755s
        sys   0m12.637s
    
    Add comment at start of servo/components/style/stylist.rs + mach build binaries:
    - no sccache
        real  0m42.899s
        user  1m17.157s
        sys   0m13.679s
    - sccache
        real  1m59.212s
        user  0m28.538s
        sys   0m9.292s
    - patched sccache
        real  0m42.774s
        user  1m16.826s
        sys   0m13.609s
    

    In short: it works! The patched sccache case has the same (good) behaviour as the no sccache case.

    @luser : I think this patch is good to land. And I think it's worth doing a new release for it, so Firefox devs can take advantage.

  12. luser commented on Jul 11, 2018

    @luser
    Contributor

    Thanks for testing! I will merge that and release a new version.

  13. added a commit that references this issue on Aug 6, 2018
    4904024
  14. added a commit that references this issue on Jan 3, 2025
    9e92e99
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions