{"id":238017,"date":"2021-09-03T06:37:00","date_gmt":"2021-09-03T10:37:00","guid":{"rendered":"https:\/\/wordpress-756359-3782526.cloudwaysapps.com\/?p=238017"},"modified":"2024-09-30T07:26:34","modified_gmt":"2024-09-30T07:26:34","slug":"2021-09-03-rust","status":"publish","type":"post","link":"https:\/\/www.travis-ci.com\/blog\/2021-09-03-rust\/","title":{"rendered":"Speedy Builds with Rust"},"content":{"rendered":"\n<p>Rust is a multi-paradigm, high-level, general-purpose programming language designed for performance and safety, especially safe concurrency. In today\u2019s post we are using Rust&nbsp;<code>nightly<\/code>. You can also add more targets like&nbsp;<code>stable<\/code>&nbsp;or&nbsp;<code>beta<\/code>, but we are going to show you the power or utilizing caching when it comes to building in Rust on Travis.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"rust-and-travis\">Rust and Travis<\/h2>\n\n\n\n<p>Rust builds are notoriously slow, especially when compiling from a clean slate. This is less true than it once was, but it\u2019s still slow enough to be a significant problem for CI systems. So I thought why not write something that will help future Rust builders using Travis?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"slow-down-youre-going-too-fast\">Slow down, you\u2019re going too fast<\/h2>\n\n\n\n<p>Actually, you\u2019re doing just fine. The good news is, is that we can use&nbsp;<code>cache: cargo<\/code>, and a few other things up our builders sleeves to help the speed of builds in Rust. For example, take a look at this&nbsp;<code>.travis.yml<\/code>&nbsp;I wrote:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>---\ncache: cargo\ndist: xenial\nlanguage: rust\nmatrix:\n  allow_failures:\n    - rust: nightly\n  before_cache: \n    - rm -rf ~\/.cargo\/registry\/index\/ # Remove cache files \n    - rm -f  .\/target\/.rustc_info.json # Remove more cache files that are recursively brought back upon a triggered build\n    - find .\/target\/debug -maxdepth 1 -type f -delete # Delete loose files \n  fast_finish: true\nos: linux\nrust:\n  - stable\n  - beta\n  - nightly\nscript:\n  - \"cargo build --verbose --all\"\n  - \"cargo test --verbose --all\"<\/code><\/pre>\n\n\n\n<p>We are testing against 3 Rust channels here, on occassion&nbsp;<code>nightly<\/code>&nbsp;will fail due to the compiler, but since this post is about speeding up your builds, you\u2019ll notice I put in a conditional, that is, allowing failures from only one Rust channel, and that is&nbsp;<code>nightly<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>matrix:\n  allow_failures:\n    - rust: nightly<\/code><\/pre>\n\n\n\n<p>You\u2019ll notice&nbsp;<code>stable<\/code>&nbsp;and&nbsp;<code>beta<\/code>&nbsp;are not listed along with&nbsp;<code>nightly<\/code>. Now since speed is the center of this post, you may want to disable Cargo\u2019s incremental compilation, as it doesn\u2019t really give any more verbosity to the build, so let\u2019s try removing it for even faster builds. In order to do this, you\u2019ll need to set the&nbsp;<code>CARGO_INCREMENTAL<\/code>&nbsp;<code>env var<\/code>&nbsp;to&nbsp;<code>0<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"tests\">Tests<\/h2>\n\n\n\n<p>Now, let\u2019s make a test file, here\u2019s some Rust code, you\u2019re free to name it whatever you like, in this case you can see we named our file&nbsp;<code>test_run<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>extern crate test_run;\n\nuse logic_gates::{and, xor};\n\npub type Sum = u8;\npub type Carry = u8;\n\npub fn half_adder_input_output() -&gt; Vec&lt;((u8, u8), (Sum, Carry))&gt; { \n    vec!&#91;\n        ((0, 0), (0, 0)), \n        ((0, 1), (1, 0)), \n        ((1, 0), (1, 0)), \n        ((1, 1), (0, 1)), \n    ] \n}\n\nfn half_adder(a: u8, b: u8) -&gt; (Sum, Carry) {\n    (xor(a, b), and(a, b))\n}\n\n#&#91;test]\nfn one_bit_adder() {\n    for (inn, out) in half_adder_input_output() {\n        let (a, b) = inn;\n        assert_eq!(half_adder(a, b), out);\n    }\n}<\/code><\/pre>\n\n\n\n<p>Looks good builders, now let\u2019s go over what we have:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Test file written in Rust<\/li>\n\n\n\n<li>A&nbsp;<code>.travis.yml<\/code>&nbsp;file<\/li>\n\n\n\n<li>Some tips on setting the&nbsp;<code>env var<\/code>&nbsp;to&nbsp;<code>0<\/code><\/li>\n\n\n\n<li>We ran a&nbsp;<code>matrix<\/code>&nbsp;build (multiple platforms and Rust versions) to test both stable Rust versions and nightly builds of their respective rust compiler on different platforms. I stated earlier, you\u2019ll find nightly builds can fail, since those compilers aren\u2019t final and may have bugs, so don\u2019t mark these build fails as an overall failure of the build if that\u2019s solely what went wrong<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"repopulation-of-cached-files\">Repopulation of cached files<\/h2>\n\n\n\n<p>As the official Travis documentation states, Travis currently checks&nbsp;<code>~\/.cargo<\/code>&nbsp;and&nbsp;<code>target\/<\/code>&nbsp;for any changes from the current cache after a build. You must note, there will always be changes in&nbsp;<code>target\/<\/code>&nbsp;if the source has changed, which it usually does when a Travis build runs. This means new incremental object files, fingerprints, and build script compilation files are being added to the cache every build, and the more you build &#8211; the more clutter! You\u2019ll notice there\u2019s a&nbsp;<code>before_cache<\/code>&nbsp;section I added, in this section I added:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>before_cache: \n    - rm -rf ~\/.cargo\/registry\/index\/ \n    - rm -f  .\/target\/.rustc_info.json\n    - find .\/target\/debug -maxdepth 1 -type f -delete<\/code><\/pre>\n\n\n\n<p>This will help with that subroutine, and speed your builds up whilst using Rust, and now you should have a working build! You can carry over some of the methods you read here today and apply them to larger scale projects, this is just a small sample of implementing various ways of speeding up Rust builds on Travis.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"393\" src=\"https:\/\/www.travis-ci.com\/wp-content\/uploads\/2021\/09\/132039334-70e2d953-9e1f-4b14-a9d4-54f3d1922ab1-1024x393-1.png\" alt=\"\" class=\"wp-image-242756\" srcset=\"https:\/\/www.travis-ci.com\/wp-content\/uploads\/2021\/09\/132039334-70e2d953-9e1f-4b14-a9d4-54f3d1922ab1-1024x393-1.png 1024w, https:\/\/www.travis-ci.com\/wp-content\/uploads\/2021\/09\/132039334-70e2d953-9e1f-4b14-a9d4-54f3d1922ab1-1024x393-1-300x115.png 300w, https:\/\/www.travis-ci.com\/wp-content\/uploads\/2021\/09\/132039334-70e2d953-9e1f-4b14-a9d4-54f3d1922ab1-1024x393-1-768x295.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>If you have any questions please email me at&nbsp;<a href=\"mailto:montana@travis-ci.org\">montana@travis-ci.org<\/a>&nbsp;and I\u2019ll answer any and all questions!<\/p>\n\n\n\n<p>Happy building!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rust is a multi-paradigm, high-level, general-purpose programming language designed for performance and safety, especially safe concurrency. In today\u2019s post we are using Rust&nbsp;nightly. You can also add more targets like&nbsp;stable&nbsp;or&nbsp;beta, but we are going to show you the power or utilizing caching when it comes to building in Rust on Travis. Rust and Travis Rust [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_breakdance_hide_in_design_set":false,"_breakdance_tags":"","footnotes":""},"categories":[16],"tags":[7,19,20,5],"class_list":["post-238017","post","type-post","status-publish","format-standard","hentry","category-news","tag-community","tag-feature","tag-infrastructure","tag-news"],"_links":{"self":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts\/238017","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/comments?post=238017"}],"version-history":[{"count":2,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts\/238017\/revisions"}],"predecessor-version":[{"id":242757,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/posts\/238017\/revisions\/242757"}],"wp:attachment":[{"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/media?parent=238017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/categories?post=238017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.travis-ci.com\/wp-json\/wp\/v2\/tags?post=238017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}