diff --git a/.github/workflows/hotpath-comment.yml b/.github/workflows/hotpath-comment.yml new file mode 100644 index 0000000..395a533 --- /dev/null +++ b/.github/workflows/hotpath-comment.yml @@ -0,0 +1,57 @@ +name: Hotpath Comment + +on: + workflow_run: + workflows: ["Hotpath Profile"] + types: + - completed + +permissions: + pull-requests: write + +jobs: + comment: + runs-on: ubuntu-latest + if: ${{ github.event.workflow_run.conclusion == 'success' }} + + steps: + - name: Download profiling results + uses: actions/download-artifact@v4 + with: + name: hotpath-results + github-token: ${{ secrets.GITHUB_TOKEN }} + run-id: ${{ github.event.workflow_run.id }} + + - name: Read PR number + id: pr + run: echo "number=$(cat pr_number.txt)" >> $GITHUB_OUTPUT + + - name: Setup Rust + uses: actions-rust-lang/setup-rust-toolchain@v1 + + - name: Install hotpath CLI + run: cargo install hotpath + + - name: Post timing comparison comment + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + hotpath profile-pr \ + --repo ${{ github.repository }} \ + --pr-number ${{ steps.pr.outputs.number }} \ + --head-json head-timing.json \ + --base-json base-timing.json \ + --mode timing \ + --title "โฑ๏ธ Hotpath Timing Profile" + + - name: Post allocation comparison comment + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + run: | + hotpath profile-pr \ + --repo ${{ github.repository }} \ + --pr-number ${{ steps.pr.outputs.number }} \ + --head-json head-alloc.json \ + --base-json base-alloc.json \ + --mode alloc \ + --title "๐Ÿ“Š Hotpath Allocation Profile" diff --git a/.github/workflows/hotpath-profile.yml b/.github/workflows/hotpath-profile.yml new file mode 100644 index 0000000..b367ca2 --- /dev/null +++ b/.github/workflows/hotpath-profile.yml @@ -0,0 +1,63 @@ +name: Hotpath Profile + +on: + pull_request: + branches: [ "main" ] + +env: + CARGO_TERM_COLOR: always + +jobs: + profile: + runs-on: ubuntu-latest + + steps: + - name: Checkout PR HEAD + uses: actions/checkout@v4 + + - name: Setup Rust + uses: actions-rust-lang/setup-rust-toolchain@v1 + + - name: Run timing profiling on HEAD + env: + HOTPATH_JSON: "true" + run: | + cargo run --features='hotpath' 2>&1 | grep '^{"hotpath_profiling_mode"' > head-timing.json + + - name: Run allocation profiling on HEAD + env: + HOTPATH_JSON: "true" + run: | + cargo run --features='hotpath,hotpath-alloc-count-total' 2>&1 | grep '^{"hotpath_profiling_mode"' > head-alloc.json + + - name: Checkout base branch + uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.base.sha }} + + - name: Run timing profiling on base + env: + HOTPATH_JSON: "true" + run: | + cargo run --features='hotpath' 2>&1 | grep '^{"hotpath_profiling_mode"' > base-timing.json + + - name: Run allocation profiling on base + env: + HOTPATH_JSON: "true" + run: | + cargo run --features='hotpath,hotpath-alloc-count-total' 2>&1 | grep '^{"hotpath_profiling_mode"' > base-alloc.json + + - name: Save PR number + run: echo "${{ github.event.number }}" > pr_number.txt + + - name: Upload profiling results + uses: actions/upload-artifact@v4 + with: + name: hotpath-results + path: | + head-timing.json + head-alloc.json + base-timing.json + base-alloc.json + pr_number.txt + retention-days: 1 diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..9d5c77e --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1,26 @@ +condense_wildcard_suffixes = true +doc_comment_code_block_width = 80 +edition = "2024" # Keep in sync with Cargo.toml. +enum_discrim_align_threshold = 60 +force_explicit_abi = false +force_multiline_blocks = true +format_code_in_doc_comments = true +format_macro_matchers = true +format_strings = true +group_imports = "StdExternalCrate" +hex_literal_case = "Upper" +imports_granularity = "Crate" +imports_layout = "HorizontalVertical" +inline_attribute_width = 60 +match_block_trailing_comma = true +max_width = 80 +newline_style = "Unix" +normalize_comments = true +normalize_doc_attributes = true +overflow_delimited_expr = true +struct_field_align_threshold = 60 +tab_spaces = 2 +unstable_features = true +use_field_init_shorthand = true +use_try_shorthand = true +wrap_comments = true diff --git a/.taplo.toml b/.taplo.toml new file mode 100644 index 0000000..5a6456b --- /dev/null +++ b/.taplo.toml @@ -0,0 +1,13 @@ +[formatting] +align_entries = true +column_width = 100 +compact_arrays = false +reorder_inline_tables = true +reorder_keys = true + +[[rule]] +include = [ "**/Cargo.toml" ] +keys = [ "package" ] + +[rule.formatting] +reorder_keys = false diff --git a/Cargo.lock b/Cargo.lock index 3b5c132..c9ed3f9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,12 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "adler2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "320119579fcad9c21884f5c4861d16174d0e06250625266f50fe6898340abefa" + [[package]] name = "aho-corasick" version = "1.1.3" @@ -17,18 +23,80 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4b46cbb362ab8752921c97e041f5e366ee6297bd428a31275b9fcf1e380f7299" +[[package]] +name = "anstream" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + [[package]] name = "anstyle" version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys 0.61.2", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys 0.61.2", +] + +[[package]] +name = "arc-swap" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69f7f8c3906b62b754cd5326047894316021dcfe5a194c8ea52bdd94934a3457" + +[[package]] +name = "ascii" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16" + [[package]] name = "autocfg" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + [[package]] name = "bitflags" version = "2.9.0" @@ -41,12 +109,34 @@ version = "3.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf" +[[package]] +name = "byteorder" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b" + +[[package]] +name = "bytes" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b35204fbdc0b3f4446b89fc1ac2cf84a8a68971995d0bf2e925ec7cd960f9cb3" + [[package]] name = "cast" version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" +[[package]] +name = "cc" +version = "1.2.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b97463e1064cb1b1c1384ad0a0b9c8abd0988e2a91f52606c80ef14aadb63e36" +dependencies = [ + "find-msvc-tools", + "shlex", +] + [[package]] name = "cfg-if" version = "1.0.0" @@ -59,6 +149,12 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" +[[package]] +name = "chunked_transfer" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e4de3bc4ea267985becf712dc6d9eed8b04c953b3fcfb339ebc87acd9804901" + [[package]] name = "ciborium" version = "0.2.2" @@ -93,6 +189,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d8aa86934b44c19c50f87cc2790e19f54f7a67aedb64101c2e1a2e5ecfb73944" dependencies = [ "clap_builder", + "clap_derive", ] [[package]] @@ -101,8 +198,22 @@ version = "4.5.35" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2414dbb2dd0695280da6ea9261e327479e9d37b0630f6b53ba2a11c60c679fd9" dependencies = [ + "anstream", "anstyle", "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09176aae279615badda0765c0c0b3f6ed53f4709118af73cf4655d85d1530cd7" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -111,6 +222,59 @@ version = "0.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f46ad14479a25103f283c0f10005961cf086d8dc42205bb44c46ac563475dca6" +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + +[[package]] +name = "colored" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fde0e0ec90c9dfb3b4b1a0891a7dcd0e2bffde2f7efed5fe7c9bb00e5bfb915e" +dependencies = [ + "windows-sys 0.59.0", +] + +[[package]] +name = "cookie" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ddef33a339a91ea89fb53151bd0a4689cfce27055c291dfa69945475d22c747" +dependencies = [ + "percent-encoding", + "time", + "version_check", +] + +[[package]] +name = "cookie_store" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fc4bff745c9b4c7fb1e97b25d13153da2bc7796260141df62378998d070207f" +dependencies = [ + "cookie", + "document-features", + "idna", + "indexmap", + "log", + "serde", + "serde_derive", + "serde_json", + "time", + "url", +] + +[[package]] +name = "crc32fast" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9481c1c90cbf2ac953f07c8d4a58aa3945c425b7185c9154d67a65e4230da511" +dependencies = [ + "cfg-if", +] + [[package]] name = "criterion" version = "0.7.0" @@ -144,6 +308,15 @@ dependencies = [ "itertools", ] +[[package]] +name = "crossbeam-channel" +version = "0.5.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "82b8f8f868b36967f9606790d1903570de9ceaf870a7bf9fbbd3016d636a2cb2" +dependencies = [ + "crossbeam-utils", +] + [[package]] name = "crossbeam-deque" version = "0.8.6" @@ -175,12 +348,126 @@ version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "43da5946c66ffcc7745f48db692ffbb10a83bfe0afd96235c5c2a4fb23994929" +[[package]] +name = "deranged" +version = "0.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ececcb659e7ba858fb4f10388c250a7252eb0a27373f1a72b8748afdd248e587" +dependencies = [ + "powerfmt", +] + +[[package]] +name = "dirs-next" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1" +dependencies = [ + "cfg-if", + "dirs-sys-next", +] + +[[package]] +name = "dirs-sys-next" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + +[[package]] +name = "displaydoc" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97369cbbc041bc366949bc74d34658d6cda5621039731c6310521892a3a20ae0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "document-features" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4b8a88685455ed29a21542a33abd9cb6510b6b129abadabdcef0f4c55bc8f61" +dependencies = [ + "litrs", +] + [[package]] name = "either" version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" +[[package]] +name = "encode_unicode" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0" + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "eyre" +version = "0.6.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7cd915d99f24784cdc19fd37ef22b97e3ff0ae756c7e492e9fbfe897d61e2aec" +dependencies = [ + "indenter", + "once_cell", +] + +[[package]] +name = "find-msvc-tools" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3a3076410a55c90011c298b04d0cfa770b00fa04e1e3c97d3f6c9de105a03844" + +[[package]] +name = "flate2" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bfe33edd8e85a12a67454e37f8c75e730830d83e313556ab9ebf9ee7fbeb3bfb" +dependencies = [ + "crc32fast", + "miniz_oxide", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "form_urlencoded" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf" +dependencies = [ + "percent-encoding", +] + +[[package]] +name = "getrandom" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "335ff9f135e4384c8150d6f27c6daed433577f86b4750418338c01a1a2528592" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + [[package]] name = "half" version = "2.6.0" @@ -191,6 +478,227 @@ dependencies = [ "crunchy", ] +[[package]] +name = "hashbrown" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5419bdc4f6a9207fbeba6d11b604d481addf78ecd10c11ad51e76c2f6482748d" + +[[package]] +name = "hdrhistogram" +version = "7.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "765c9198f173dd59ce26ff9f95ef0aafd0a0fe01fb9d72841bc5066a4c06511d" +dependencies = [ + "byteorder", + "num-traits", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "hermit-abi" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc0fef456e4baa96da950455cd02c081ca953b141298e41db3fc7e36b1da849c" + +[[package]] +name = "hotpath" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef44238d7007bbd3c41ce7dbf3d7a4fb224bbf70d6cf3162479138dda0cd6a9e" +dependencies = [ + "arc-swap", + "base64", + "cfg-if", + "clap", + "colored", + "crossbeam-channel", + "eyre", + "hdrhistogram", + "hotpath-macros", + "prettytable-rs", + "quanta", + "serde", + "serde_json", + "tiny_http", + "tokio", + "ureq", +] + +[[package]] +name = "hotpath-macros" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e7e3443c6f4e5cc69e6b701f7bfca8a5502f0080e94629413346e7b4518d730" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "http" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4a85d31aea989eead29a3aaf9e1115a180df8282431156e533de47660892565" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + +[[package]] +name = "icu_collections" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c6b649701667bbe825c3b7e6388cb521c23d88644678e83c0c4d0a621a34b43" +dependencies = [ + "displaydoc", + "potential_utf", + "yoke", + "zerofrom", + "zerovec", +] + +[[package]] +name = "icu_locale_core" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edba7861004dd3714265b4db54a3c390e880ab658fec5f7db895fae2046b5bb6" +dependencies = [ + "displaydoc", + "litemap", + "tinystr", + "writeable", + "zerovec", +] + +[[package]] +name = "icu_normalizer" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f6c8828b67bf8908d82127b2054ea1b4427ff0230ee9141c54251934ab1b599" +dependencies = [ + "icu_collections", + "icu_normalizer_data", + "icu_properties", + "icu_provider", + "smallvec", + "zerovec", +] + +[[package]] +name = "icu_normalizer_data" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7aedcccd01fc5fe81e6b489c15b247b8b0690feb23304303a9e560f37efc560a" + +[[package]] +name = "icu_properties" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e93fcd3157766c0c8da2f8cff6ce651a31f0810eaa1c51ec363ef790bbb5fb99" +dependencies = [ + "icu_collections", + "icu_locale_core", + "icu_properties_data", + "icu_provider", + "zerotrie", + "zerovec", +] + +[[package]] +name = "icu_properties_data" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "02845b3647bb045f1100ecd6480ff52f34c35f82d9880e029d329c21d1054899" + +[[package]] +name = "icu_provider" +version = "2.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85962cf0ce02e1e0a629cc34e7ca3e373ce20dda4c4d7294bbd0bf1fdb59e614" +dependencies = [ + "displaydoc", + "icu_locale_core", + "writeable", + "yoke", + "zerofrom", + "zerotrie", + "zerovec", +] + +[[package]] +name = "idna" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de" +dependencies = [ + "idna_adapter", + "smallvec", + "utf8_iter", +] + +[[package]] +name = "idna_adapter" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acae9609540aa318d1bc588455225fb2085b9ed0c4f6bd0d9d5bcd86f1a0344" +dependencies = [ + "icu_normalizer", + "icu_properties", +] + +[[package]] +name = "indenter" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "964de6e86d545b246d84badc0fef527924ace5134f30641c203ef52ba83f58d5" + +[[package]] +name = "indexmap" +version = "2.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6717a8d2a5a929a1a2eb43a12812498ed141a0bcfb7e8f7844fbdbe4303bba9f" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "is-terminal" +version = "0.4.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46" +dependencies = [ + "hermit-abi", + "libc", + "windows-sys 0.61.2", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + [[package]] name = "itertools" version = "0.13.0" @@ -217,10 +725,38 @@ dependencies = [ ] [[package]] -name = "libc" -version = "0.2.171" +name = "lazy_static" +version = "1.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6" +checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" + +[[package]] +name = "libc" +version = "0.2.177" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2874a2af47a2325c2001a6e6fad9b16a53b802102b528163885171cf92b15976" + +[[package]] +name = "libredox" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "416f7e718bdb06000964960ffa43b4335ad4012ae8b99060261aa4a8088d5ccb" +dependencies = [ + "bitflags", + "libc", +] + +[[package]] +name = "litemap" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6373607a59f0be73a39b6fe456b8192fcc3585f602af20751600e974dd455e77" + +[[package]] +name = "litrs" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11d3d7f243d5c5a8b9bb5d6dd2b1602c0cb0b9db1621bafc7ed66e35ff9fe092" [[package]] name = "log" @@ -236,18 +772,29 @@ checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" [[package]] name = "microfetch" -version = "0.4.9" +version = "0.4.11" dependencies = [ "criterion", + "hotpath", "libc", "nix", ] [[package]] -name = "nix" -version = "0.30.0" +name = "miniz_oxide" +version = "0.8.9" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "537bc3c4a347b87fd52ac6c03a02ab1302962cfd93373c5d7a112cdc337854cc" +checksum = "1fa76a2c86f704bdb222d66965fb3d63269ce38518b83cb0575fca855ebb6316" +dependencies = [ + "adler2", + "simd-adler32", +] + +[[package]] +name = "nix" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6" dependencies = [ "bitflags", "cfg-if", @@ -255,6 +802,12 @@ dependencies = [ "libc", ] +[[package]] +name = "num-conv" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9" + [[package]] name = "num-traits" version = "0.2.19" @@ -270,12 +823,30 @@ version = "1.21.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + [[package]] name = "oorandom" version = "11.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e" +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + +[[package]] +name = "pin-project-lite" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" + [[package]] name = "plotters" version = "0.3.7" @@ -304,6 +875,34 @@ dependencies = [ "plotters-backend", ] +[[package]] +name = "potential_utf" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b73949432f5e2a09657003c25bca5e19a0e9c84f8058ca374f49e0ebe605af77" +dependencies = [ + "zerovec", +] + +[[package]] +name = "powerfmt" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391" + +[[package]] +name = "prettytable-rs" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eea25e07510aa6ab6547308ebe3c036016d162b8da920dbb079e3ba8acf3d95a" +dependencies = [ + "encode_unicode", + "is-terminal", + "lazy_static", + "term", + "unicode-width", +] + [[package]] name = "proc-macro2" version = "1.0.94" @@ -313,6 +912,21 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "quanta" +version = "0.12.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f3ab5a9d756f0d97bdc89019bd2e4ea098cf9cde50ee7564dde6b81ccc8f06c7" +dependencies = [ + "crossbeam-utils", + "libc", + "once_cell", + "raw-cpuid", + "wasi", + "web-sys", + "winapi", +] + [[package]] name = "quote" version = "1.0.40" @@ -322,6 +936,15 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "raw-cpuid" +version = "11.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "498cd0dc59d73224351ee52a95fee0f1a617a2eae0e7d9d720cc622c73a54186" +dependencies = [ + "bitflags", +] + [[package]] name = "rayon" version = "1.10.0" @@ -342,6 +965,17 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "redox_users" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" +dependencies = [ + "getrandom", + "libredox", + "thiserror", +] + [[package]] name = "regex" version = "1.11.1" @@ -371,6 +1005,55 @@ version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" +[[package]] +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom", + "libc", + "untrusted", + "windows-sys 0.52.0", +] + +[[package]] +name = "rustls" +version = "0.23.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "533f54bc6a7d4f647e46ad909549eda97bf5afc1585190ef692b4286b198bd8f" +dependencies = [ + "log", + "once_cell", + "ring", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-pki-types" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94182ad936a0c91c324cd46c6511b9510ed16af436d7b5bab34beab0afd55f7a" +dependencies = [ + "zeroize", +] + +[[package]] +name = "rustls-webpki" +version = "0.103.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2ffdfa2f5286e2247234e03f680868ac2815974dc39e00ea15adc445d0aafe52" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", +] + [[package]] name = "rustversion" version = "1.0.20" @@ -424,6 +1107,42 @@ dependencies = [ "serde", ] +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "simd-adler32" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d66dc143e6b11c1eddc06d5c423cfc97062865baf299914ab64caa38182078fe" + +[[package]] +name = "smallvec" +version = "1.15.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "67b1b7a3b5fe4f1376887184045fcf45c69e92af734b7aaddc05fb777b6fbd03" + +[[package]] +name = "stable_deref_trait" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "subtle" +version = "2.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" + [[package]] name = "syn" version = "2.0.100" @@ -435,6 +1154,101 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "synstructure" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "term" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c59df8ac95d96ff9bede18eb7300b0fda5e5d8d90960e76f8e14ae765eedbf1f" +dependencies = [ + "dirs-next", + "rustversion", + "winapi", +] + +[[package]] +name = "thiserror" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" +dependencies = [ + "thiserror-impl", +] + +[[package]] +name = "thiserror-impl" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4fee6c4efc90059e10f81e6d42c60a18f76588c3d74cb83a0b242a2b6c7504c1" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "time" +version = "0.3.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "91e7d9e3bb61134e77bde20dd4825b97c010155709965fedf0f49bb138e52a9d" +dependencies = [ + "deranged", + "itoa", + "num-conv", + "powerfmt", + "serde", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40868e7c1d2f0b8d73e4a8c7f0ff63af4f6d19be117e90bd73eb1d62cf831c6b" + +[[package]] +name = "time-macros" +version = "0.2.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30cfb0125f12d9c277f35663a0a33f8c30190f4e4574868a330595412d34ebf3" +dependencies = [ + "num-conv", + "time-core", +] + +[[package]] +name = "tiny_http" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "389915df6413a2e74fb181895f933386023c71110878cd0825588928e64cdc82" +dependencies = [ + "ascii", + "chunked_transfer", + "httpdate", + "log", +] + +[[package]] +name = "tinystr" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42d3e9c45c09de15d06dd8acf5f4e0e399e85927b7f00711024eb7ae10fa4869" +dependencies = [ + "displaydoc", + "zerovec", +] + [[package]] name = "tinytemplate" version = "1.2.1" @@ -445,12 +1259,101 @@ dependencies = [ "serde_json", ] +[[package]] +name = "tokio" +version = "1.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ff360e02eab121e0bc37a2d3b4d4dc622e6eda3a8e5253d5435ecf5bd4c68408" +dependencies = [ + "pin-project-lite", +] + [[package]] name = "unicode-ident" version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "ureq" +version = "3.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d39cb1dbab692d82a977c0392ffac19e188bd9186a9f32806f0aaa859d75585a" +dependencies = [ + "base64", + "cookie_store", + "flate2", + "log", + "percent-encoding", + "rustls", + "rustls-pki-types", + "serde", + "serde_json", + "ureq-proto", + "utf-8", + "webpki-roots", +] + +[[package]] +name = "ureq-proto" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60b4531c118335662134346048ddb0e54cc86bd7e81866757873055f0e38f5d2" +dependencies = [ + "base64", + "http", + "httparse", + "log", +] + +[[package]] +name = "url" +version = "2.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08bc136a29a3d1758e07a9cca267be308aeebf5cfd5a10f3f67ab2097683ef5b" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", + "serde", +] + +[[package]] +name = "utf-8" +version = "0.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09cc8ee72d2a9becf2f2febe0205bbed8fc6615b7cb429ad062dc7b7ddd036a9" + +[[package]] +name = "utf8_iter" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + [[package]] name = "walkdir" version = "2.5.0" @@ -461,6 +1364,12 @@ dependencies = [ "winapi-util", ] +[[package]] +name = "wasi" +version = "0.11.1+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" + [[package]] name = "wasm-bindgen" version = "0.2.100" @@ -529,13 +1438,59 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "webpki-roots" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2878ef029c47c6e8cf779119f20fcf52bde7ad42a731b2a304bc221df17571e" +dependencies = [ + "rustls-pki-types", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + [[package]] name = "winapi-util" version = "0.1.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" dependencies = [ - "windows-sys", + "windows-sys 0.59.0", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", ] [[package]] @@ -547,6 +1502,15 @@ dependencies = [ "windows-targets", ] +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + [[package]] name = "windows-targets" version = "0.52.6" @@ -610,3 +1574,92 @@ name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "writeable" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9edde0db4769d2dc68579893f2306b26c6ecfbe0ef499b013d731b7b9247e0b9" + +[[package]] +name = "yoke" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72d6e5c6afb84d73944e5cedb052c4680d5657337201555f9f2a16b7406d4954" +dependencies = [ + "stable_deref_trait", + "yoke-derive", + "zerofrom", +] + +[[package]] +name = "yoke-derive" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b659052874eb698efe5b9e8cf382204678a0086ebf46982b79d6ca3182927e5d" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zerofrom" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50cc42e0333e05660c3587f3bf9d0478688e15d870fab3346451ce7f8c9fbea5" +dependencies = [ + "zerofrom-derive", +] + +[[package]] +name = "zerofrom-derive" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "synstructure", +] + +[[package]] +name = "zeroize" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b97154e67e32c85465826e8bcc1c59429aaaf107c1e4a9e53c8d8ccd5eff88d0" + +[[package]] +name = "zerotrie" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a59c17a5562d507e4b54960e8569ebee33bee890c70aa3fe7b97e85a9fd7851" +dependencies = [ + "displaydoc", + "yoke", + "zerofrom", +] + +[[package]] +name = "zerovec" +version = "0.11.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c28719294829477f525be0186d13efa9a3c602f7ec202ca9e353d310fb9a002" +dependencies = [ + "yoke", + "zerofrom", + "zerovec-derive", +] + +[[package]] +name = "zerovec-derive" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eadce39539ca5cb3985590102671f2567e659fca9666581ad3411d59207951f3" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml index f09c8b3..7e83f9f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] -name = "microfetch" -version = "0.4.9" +name = "microfetch" +version = "0.4.11" edition = "2024" [lib] @@ -12,28 +12,59 @@ name = "microfetch" path = "src/main.rs" [dependencies] -nix = { version = "0.30", features = ["fs", "hostname", "feature"] } -libc = "0.2" +hotpath = { optional = true, version = "0.6.0" } +libc = "0.2.177" +nix = { default-features = false, features = [ "fs", "hostname", "feature" ], version = "0.30.1" } [dev-dependencies] criterion = "0.7" +[features] +hotpath = [ "dep:hotpath", "hotpath/hotpath" ] +hotpath-alloc-bytes-total = [ "hotpath/hotpath-alloc-bytes-total" ] +hotpath-alloc-count-total = [ "hotpath/hotpath-alloc-count-total" ] +hotpath-off = [ "hotpath/hotpath-off" ] + [[bench]] -name = "benchmark" harness = false +name = "benchmark" [profile.dev] -opt-level = 3 +opt-level = 1 [profile.release] -strip = true -opt-level = "s" -lto = true codegen-units = 1 -panic = "abort" +lto = true +opt-level = "s" +panic = "abort" +strip = true [profile.profiler] -inherits = "release" -debug = true +debug = true +inherits = "release" split-debuginfo = "unpacked" -strip = "none" +strip = "none" + +[lints.clippy] +complexity = { level = "warn", priority = -1 } +nursery = { level = "warn", priority = -1 } +pedantic = { level = "warn", priority = -1 } +perf = { level = "warn", priority = -1 } +style = { level = "warn", priority = -1 } + +# The lint groups above enable some less-than-desirable rules, we should manually +# enable those to keep our sanity. +absolute_paths = "allow" +arbitrary_source_item_ordering = "allow" +implicit_return = "allow" +missing_docs_in_private_items = "allow" +non_ascii_literal = "allow" +pattern_type_mismatch = "allow" +print_stdout = "allow" +question_mark_used = "allow" +similar_names = "allow" +single_call_fn = "allow" +std_instead_of_core = "allow" +too_long_first_doc_paragraph = "allow" +too_many_lines = "allow" +unused_trait_names = "allow" diff --git a/README.md b/README.md index 28d6338..5ae0f72 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,34 @@ + +
https://deps.rs/repo/github/notashelf/microfetch - - + stars
-

Microfetch

+
+

+ Microfetch +

+

+ Microscopic fetch tool in Rust, for NixOS systems, with special emphasis on speed +

+
+ Synopsis
+ Features | Motivation
+ Installation +
+
-Stupidly simple, laughably fast fetch tool. Written in Rust for speed and ease -of maintainability. Runs in a _fraction of a millisecond_ and displays _most_ of -the nonsense you'd see posted on r/unixporn or other internet communities. Aims -to replace [fastfetch](https://github.com/fastfetch-cli/fastfetch) on my -personal system, but [probably not yours](#customizing). Though, you are more -than welcome to use it on your system: it's pretty [fast](#benchmarks)... +## Synopsis + +[fastfetch]: https://github.com/fastfetch-cli/fastfetch + +Stupidly small and simple, laughably fast and pretty fetch tool. Written in Rust +for speed and ease of maintainability. Runs in a _fraction of a millisecond_ and +displays _most_ of the nonsense you'd see posted on r/unixporn or other internet +communities. Aims to replace [fastfetch] on my personal system, but +[probably not yours](#customizing). Though, you are more than welcome to use it +on your system: it is pretty _[fast](#benchmarks)_...

[!NOTE] diff --git a/benches/benchmark.rs b/benches/benchmark.rs index 03e49f4..33dc1b9 100644 --- a/benches/benchmark.rs +++ b/benches/benchmark.rs @@ -1,26 +1,31 @@ use criterion::{Criterion, criterion_group, criterion_main}; -use microfetch_lib::colors::print_dots; -use microfetch_lib::desktop::get_desktop_info; -use microfetch_lib::release::{get_os_pretty_name, get_system_info}; -use microfetch_lib::system::{ - get_memory_usage, get_root_disk_usage, get_shell, get_username_and_hostname, +use microfetch_lib::{ + colors::print_dots, + desktop::get_desktop_info, + release::{get_os_pretty_name, get_system_info}, + system::{ + get_memory_usage, + get_root_disk_usage, + get_shell, + get_username_and_hostname, + }, + uptime::get_current, }; -use microfetch_lib::uptime::get_current; fn main_benchmark(c: &mut Criterion) { - let utsname = nix::sys::utsname::uname().expect("lol"); - c.bench_function("user_info", |b| { - b.iter(|| get_username_and_hostname(&utsname)); - }); - c.bench_function("os_name", |b| b.iter(get_os_pretty_name)); - c.bench_function("kernel_version", |b| b.iter(|| get_system_info(&utsname))); - c.bench_function("shell", |b| b.iter(get_shell)); + let utsname = nix::sys::utsname::uname().expect("lol"); + c.bench_function("user_info", |b| { + b.iter(|| get_username_and_hostname(&utsname)); + }); + c.bench_function("os_name", |b| b.iter(get_os_pretty_name)); + c.bench_function("kernel_version", |b| b.iter(|| get_system_info(&utsname))); + c.bench_function("shell", |b| b.iter(get_shell)); - c.bench_function("desktop", |b| b.iter(get_desktop_info)); - c.bench_function("uptime", |b| b.iter(get_current)); - c.bench_function("memory_usage", |b| b.iter(get_memory_usage)); - c.bench_function("storage", |b| b.iter(get_root_disk_usage)); - c.bench_function("colors", |b| b.iter(print_dots)); + c.bench_function("desktop", |b| b.iter(get_desktop_info)); + c.bench_function("uptime", |b| b.iter(get_current)); + c.bench_function("memory_usage", |b| b.iter(get_memory_usage)); + c.bench_function("storage", |b| b.iter(get_root_disk_usage)); + c.bench_function("colors", |b| b.iter(print_dots)); } criterion_group!(benches, main_benchmark); diff --git a/flake.lock b/flake.lock index 774c899..452a532 100644 --- a/flake.lock +++ b/flake.lock @@ -2,11 +2,14 @@ "nodes": { "nixpkgs": { "locked": { - "lastModified": 1754214453, - "narHash": "sha256-Q/I2xJn/j1wpkGhWkQnm20nShYnG7TI99foDBpXm1SY=", + "lastModified": 1743359643, + "narHash": "sha256-RkyJ9a67s0zEIz4O66TyZOIGh4TFZ4dKHKMgnxZCh2I=", + "lastModified": 1763381801, + "narHash": "sha256-325fR0JmHW7B74/gHPv/S9w1Rfj/M2HniwQFUwdrZ9k=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "5b09dc45f24cf32316283e62aec81ffee3c3e376", + "rev": "ca77b4bc80e558ce59f2712fdb276f90c0ee309a", + "rev": "46931757ea8bdbba25c076697f8e73b8dc39fef5", "type": "github" }, "original": { diff --git a/nix/package.nix b/nix/package.nix index 506aea3..5bb387c 100644 --- a/nix/package.nix +++ b/nix/package.nix @@ -9,23 +9,29 @@ inherit (toml) version; in rustPlatform.buildRustPackage.override {stdenv = stdenvAdapters.useMoldLinker llvm.stdenv;} { - RUSTFLAGS = "-C link-arg=-fuse-ld=mold"; - inherit pname version; - - src = builtins.path { - name = "${pname}-${version}"; - path = lib.sources.cleanSource ../.; - }; + src = let + fs = lib.fileset; + s = ../.; + in + fs.toSource { + root = s; + fileset = fs.unions [ + (fs.fileFilter (file: builtins.any file.hasExt ["rs"]) (s + /src)) + (s + /Cargo.lock) + (s + /Cargo.toml) + ]; + }; cargoLock.lockFile = ../Cargo.lock; enableParallelBuilding = true; + env.RUSTFLAGS = "-C link-arg=-fuse-ld=mold"; meta = { - description = "A microscopic fetch script in Rust, for NixOS systems"; + description = "Microscopic fetch script in Rust, for NixOS systems"; homepage = "https://github.com/NotAShelf/microfetch"; license = lib.licenses.gpl3Only; - maintainers = with lib.maintainers; [NotAShelf]; + maintainers = [lib.maintainers.NotAShelf]; mainProgram = "microfetch"; }; } diff --git a/nix/shell.nix b/nix/shell.nix index 0577a0a..111b803 100644 --- a/nix/shell.nix +++ b/nix/shell.nix @@ -4,6 +4,7 @@ rustfmt, clippy, cargo, + taplo, rustc, rustPlatform, gnuplot, @@ -16,8 +17,9 @@ mkShell { rustc rust-analyzer-unwrapped - rustfmt + (rustfmt.override {asNightly = true;}) clippy + taplo gnuplot # For Criterion.rs plots ]; diff --git a/src/colors.rs b/src/colors.rs index da613a5..07ab9bd 100644 --- a/src/colors.rs +++ b/src/colors.rs @@ -1,57 +1,81 @@ -use std::env; -use std::sync::LazyLock; +use std::{env, sync::LazyLock}; pub struct Colors { - pub reset: &'static str, - pub blue: &'static str, - pub cyan: &'static str, - pub green: &'static str, - pub yellow: &'static str, - pub red: &'static str, - pub magenta: &'static str, + pub reset: &'static str, + pub blue: &'static str, + pub cyan: &'static str, + pub green: &'static str, + pub yellow: &'static str, + pub red: &'static str, + pub magenta: &'static str, } impl Colors { - const fn new(is_no_color: bool) -> Self { - if is_no_color { - Self { - reset: "", - blue: "", - cyan: "", - green: "", - yellow: "", - red: "", - magenta: "", - } - } else { - Self { - reset: "\x1b[0m", - blue: "\x1b[34m", - cyan: "\x1b[36m", - green: "\x1b[32m", - yellow: "\x1b[33m", - red: "\x1b[31m", - magenta: "\x1b[35m", - } - } + const fn new(is_no_color: bool) -> Self { + if is_no_color { + Self { + reset: "", + blue: "", + cyan: "", + green: "", + yellow: "", + red: "", + magenta: "", + } + } else { + Self { + reset: "\x1b[0m", + blue: "\x1b[34m", + cyan: "\x1b[36m", + green: "\x1b[32m", + yellow: "\x1b[33m", + red: "\x1b[31m", + magenta: "\x1b[35m", + } } + } } pub static COLORS: LazyLock = LazyLock::new(|| { - // check for NO_COLOR once at startup - let is_no_color = env::var("NO_COLOR").is_ok(); - Colors::new(is_no_color) + // Check for NO_COLOR once at startup + let is_no_color = env::var("NO_COLOR").is_ok(); + Colors::new(is_no_color) }); +#[must_use] +#[cfg_attr(feature = "hotpath", hotpath::measure)] pub fn print_dots() -> String { - format!( - "{}๏„‘ {}๏„‘ {}๏„‘ {}๏„‘ {}๏„‘ {}๏„‘ {}", - COLORS.blue, - COLORS.cyan, - COLORS.green, - COLORS.yellow, - COLORS.red, - COLORS.magenta, - COLORS.reset, - ) + // Pre-calculate capacity: 6 color codes + " " (glyph + 2 spaces) per color + const GLYPH: &str = "๏„‘"; + let capacity = COLORS.blue.len() + + COLORS.cyan.len() + + COLORS.green.len() + + COLORS.yellow.len() + + COLORS.red.len() + + COLORS.magenta.len() + + COLORS.reset.len() + + (GLYPH.len() + 2) * 6; + + let mut result = String::with_capacity(capacity); + result.push_str(COLORS.blue); + result.push_str(GLYPH); + result.push_str(" "); + result.push_str(COLORS.cyan); + result.push_str(GLYPH); + result.push_str(" "); + result.push_str(COLORS.green); + result.push_str(GLYPH); + result.push_str(" "); + result.push_str(COLORS.yellow); + result.push_str(GLYPH); + result.push_str(" "); + result.push_str(COLORS.red); + result.push_str(GLYPH); + result.push_str(" "); + result.push_str(COLORS.magenta); + result.push_str(GLYPH); + result.push_str(" "); + result.push_str(COLORS.reset); + + result } diff --git a/src/desktop.rs b/src/desktop.rs index f35cd2a..561be03 100644 --- a/src/desktop.rs +++ b/src/desktop.rs @@ -1,28 +1,37 @@ +use std::fmt::Write; + +#[must_use] +#[cfg_attr(feature = "hotpath", hotpath::measure)] pub fn get_desktop_info() -> String { - // Retrieve the environment variables and handle Result types - let desktop_env = std::env::var("XDG_CURRENT_DESKTOP"); - let display_backend_result = std::env::var("XDG_SESSION_TYPE"); + // Retrieve the environment variables and handle Result types + let desktop_env = std::env::var("XDG_CURRENT_DESKTOP"); + let display_backend = std::env::var("XDG_SESSION_TYPE"); - // Capitalize the first letter of the display backend value - let mut display_backend = display_backend_result.unwrap_or_default(); - if let Some(c) = display_backend.as_mut_str().get_mut(0..1) { - c.make_ascii_uppercase(); - } + let desktop_str = match desktop_env { + Err(_) => "Unknown", + Ok(ref s) if s.starts_with("none+") => &s[5..], + Ok(ref s) => s.as_str(), + }; - // Trim "none+" from the start of desktop_env if present - // Use "Unknown" if desktop_env is empty or has an error - let desktop_env = match desktop_env { - Err(_) => "Unknown".to_string(), - Ok(s) => s.trim_start_matches("none+").to_string(), - }; + let backend_str = match display_backend { + Err(_) => "Unknown", + Ok(ref s) if s.is_empty() => "Unknown", + Ok(ref s) => s.as_str(), + }; - // Handle the case where display_backend might be empty after capitalization - let display_backend = if display_backend.is_empty() { - "Unknown" - } else { - &display_backend - } - .to_string(); + // Pre-calculate capacity: desktop_len + " (" + backend_len + ")" + // Capitalize first char needs temporary allocation only if backend exists + let mut result = + String::with_capacity(desktop_str.len() + backend_str.len() + 3); + result.push_str(desktop_str); + result.push_str(" ("); - format!("{desktop_env} ({display_backend})") + // Capitalize first character of backend + if let Some(first_char) = backend_str.chars().next() { + let _ = write!(result, "{}", first_char.to_ascii_uppercase()); + result.push_str(&backend_str[first_char.len_utf8()..]); + } + + result.push(')'); + result } diff --git a/src/main.rs b/src/main.rs index 5dd99c1..5ba7810 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,78 +4,90 @@ mod release; mod system; mod uptime; -use crate::colors::print_dots; -use crate::desktop::get_desktop_info; -use crate::release::{get_os_pretty_name, get_system_info}; -use crate::system::{get_memory_usage, get_root_disk_usage, get_shell, get_username_and_hostname}; -use crate::uptime::get_current; use std::io::{Write, stdout}; -fn main() -> Result<(), Box> { - if Some("--version") == std::env::args().nth(1).as_deref() { - println!("Microfetch {}", env!("CARGO_PKG_VERSION")); - } else { - let utsname = nix::sys::utsname::uname()?; - let fields = Fields { - user_info: get_username_and_hostname(&utsname), - os_name: get_os_pretty_name()?, - kernel_version: get_system_info(&utsname), - shell: get_shell(), - desktop: get_desktop_info(), - uptime: get_current()?, - memory_usage: get_memory_usage()?, - storage: get_root_disk_usage()?, - colors: print_dots(), - }; - print_system_info(&fields)?; - } +use crate::{ + colors::print_dots, + desktop::get_desktop_info, + release::{get_os_pretty_name, get_system_info}, + system::{ + get_memory_usage, + get_root_disk_usage, + get_shell, + get_username_and_hostname, + }, + uptime::get_current, +}; - Ok(()) +#[cfg_attr(feature = "hotpath", hotpath::main)] +fn main() -> Result<(), Box> { + if Some("--version") == std::env::args().nth(1).as_deref() { + println!("Microfetch {}", env!("CARGO_PKG_VERSION")); + } else { + let utsname = nix::sys::utsname::uname()?; + let fields = Fields { + user_info: get_username_and_hostname(&utsname), + os_name: get_os_pretty_name()?, + kernel_version: get_system_info(&utsname), + shell: get_shell(), + desktop: get_desktop_info(), + uptime: get_current()?, + memory_usage: get_memory_usage()?, + storage: get_root_disk_usage()?, + colors: print_dots(), + }; + print_system_info(&fields)?; + } + + Ok(()) } // Struct to hold all the fields we need in order to print the fetch. This // helps avoid Clippy warnings about argument count, and makes it slightly // easier to pass data around. Though, it is not like we really need to. struct Fields { - user_info: String, - os_name: String, - kernel_version: String, - shell: String, - uptime: String, - desktop: String, - memory_usage: String, - storage: String, - colors: String, + user_info: String, + os_name: String, + kernel_version: String, + shell: String, + uptime: String, + desktop: String, + memory_usage: String, + storage: String, + colors: String, } -fn print_system_info(fields: &Fields) -> Result<(), Box> { - use crate::colors::COLORS; +#[cfg_attr(feature = "hotpath", hotpath::measure)] +fn print_system_info( + fields: &Fields, +) -> Result<(), Box> { + use crate::colors::COLORS; - let Fields { - user_info, - os_name, - kernel_version, - shell, - uptime, - desktop, - memory_usage, - storage, - colors, - } = fields; + let Fields { + user_info, + os_name, + kernel_version, + shell, + uptime, + desktop, + memory_usage, + storage, + colors, + } = fields; - let cyan = COLORS.cyan; - let blue = COLORS.blue; - let reset = COLORS.reset; - let system_info = format!(" - {cyan} โ–Ÿโ–ˆโ–– {blue}โ–โ–ˆโ–™ โ–—โ–ˆโ–› {user_info} ~{reset} - {cyan} โ–—โ–„โ–„โ–Ÿโ–ˆโ–ˆโ–„โ–„โ–„โ–„โ–„{blue}โ–โ–ˆโ–™โ–ˆโ–› {cyan}โ–– {cyan}๏Œ“ {blue}System{reset} ๎˜ก {os_name} - {cyan} โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–˜{blue}โ–โ–ˆโ–ˆ {cyan}โ–Ÿโ–ˆโ–– {cyan}๎œ’ {blue}Kernel{reset} ๎˜ก {kernel_version} - {blue} โ–Ÿโ–ˆโ–› {blue}โ–โ–ˆโ–˜{cyan}โ–Ÿโ–ˆโ–› {cyan}๎ž• {blue}Shell{reset} ๎˜ก {shell} - {blue}โ–Ÿโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–› {cyan}โ–Ÿโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–› {cyan}๏€— {blue}Uptime{reset} ๎˜ก {uptime} - {blue} โ–Ÿโ–ˆโ–›{cyan}โ–—โ–ˆโ–– {cyan}โ–Ÿโ–ˆโ–› {cyan}๏‹’ {blue}Desktop{reset} ๎˜ก {desktop} - {blue} โ–โ–ˆโ–› {cyan}โ–ˆโ–ˆโ––{blue}โ–—โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„ {cyan}๏’ผ {blue}Memory{reset} ๎˜ก {memory_usage} - {blue} โ– {cyan}โ–Ÿโ–ˆโ–œโ–ˆโ––{blue}โ–€โ–€โ–€โ–€โ–€โ–ˆโ–ˆโ–›โ–€โ–€โ–˜ {cyan}๓ฑฅŽ {blue}Storage (/){reset} ๎˜ก {storage} - {cyan} โ–Ÿโ–ˆโ–˜ โ–œโ–ˆโ–– {blue}โ–โ–ˆโ–› {cyan}๎ˆซ {blue}Colors{reset} ๎˜ก {colors}\n"); + let cyan = COLORS.cyan; + let blue = COLORS.blue; + let reset = COLORS.reset; + let system_info = format!(" + {cyan} โ–Ÿโ–ˆโ–– {blue}โ–โ–ˆโ–™ โ–—โ–ˆโ–› {user_info} ~{reset} + {cyan} โ–—โ–„โ–„โ–Ÿโ–ˆโ–ˆโ–„โ–„โ–„โ–„โ–„{blue}โ–โ–ˆโ–™โ–ˆโ–› {cyan}โ–– {cyan}๏Œ“ {blue}System{reset} ๎˜ก {os_name} + {cyan} โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–€โ–˜{blue}โ–โ–ˆโ–ˆ {cyan}โ–Ÿโ–ˆโ–– {cyan}๎œ’ {blue}Kernel{reset} ๎˜ก {kernel_version} + {blue} โ–Ÿโ–ˆโ–› {blue}โ–โ–ˆโ–˜{cyan}โ–Ÿโ–ˆโ–› {cyan}๎ž• {blue}Shell{reset} ๎˜ก {shell} + {blue}โ–Ÿโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–› {cyan}โ–Ÿโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–› {cyan}๏€— {blue}Uptime{reset} ๎˜ก {uptime} + {blue} โ–Ÿโ–ˆโ–›{cyan}โ–—โ–ˆโ–– {cyan}โ–Ÿโ–ˆโ–› {cyan}๏‹’ {blue}Desktop{reset} ๎˜ก {desktop} + {blue} โ–โ–ˆโ–› {cyan}โ–ˆโ–ˆโ––{blue}โ–—โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„โ–„ {cyan}๏’ผ {blue}Memory{reset} ๎˜ก {memory_usage} + {blue} โ– {cyan}โ–Ÿโ–ˆโ–œโ–ˆโ––{blue}โ–€โ–€โ–€โ–€โ–€โ–ˆโ–ˆโ–›โ–€โ–€โ–˜ {cyan}๓ฑฅŽ {blue}Storage (/){reset} ๎˜ก {storage} + {cyan} โ–Ÿโ–ˆโ–˜ โ–œโ–ˆโ–– {blue}โ–โ–ˆโ–› {cyan}๎ˆซ {blue}Colors{reset} ๎˜ก {colors}\n"); - Ok(stdout().write_all(system_info.as_bytes())?) + Ok(stdout().write_all(system_info.as_bytes())?) } diff --git a/src/release.rs b/src/release.rs index 6d1b20f..1b820b8 100644 --- a/src/release.rs +++ b/src/release.rs @@ -1,33 +1,48 @@ -use nix::sys::utsname::UtsName; use std::{ - fs::File, - io::{self, BufRead, BufReader}, + fmt::Write as _, + fs::File, + io::{self, Read}, }; +use nix::sys::utsname::UtsName; + +#[must_use] +#[cfg_attr(feature = "hotpath", hotpath::measure)] pub fn get_system_info(utsname: &UtsName) -> String { - format!( - "{} {} ({})", - utsname.sysname().to_str().unwrap_or("Unknown"), - utsname.release().to_str().unwrap_or("Unknown"), - utsname.machine().to_str().unwrap_or("Unknown") - ) + let sysname = utsname.sysname().to_str().unwrap_or("Unknown"); + let release = utsname.release().to_str().unwrap_or("Unknown"); + let machine = utsname.machine().to_str().unwrap_or("Unknown"); + + // Pre-allocate capacity: sysname + " " + release + " (" + machine + ")" + let capacity = sysname.len() + 1 + release.len() + 2 + machine.len() + 1; + let mut result = String::with_capacity(capacity); + + write!(result, "{sysname} {release} ({machine})").unwrap(); + result } +/// Gets the pretty name of the OS from `/etc/os-release`. +/// +/// # Errors +/// +/// Returns an error if `/etc/os-release` cannot be read. +#[cfg_attr(feature = "hotpath", hotpath::measure)] pub fn get_os_pretty_name() -> Result { - let file = File::open("/etc/os-release")?; - let reader = BufReader::new(file); + // We use a stack-allocated buffer here, which seems to perform MUCH better + // than `BufReader`. In hindsight, I should've seen this coming. + let mut buffer = String::with_capacity(1024); + File::open("/etc/os-release")?.read_to_string(&mut buffer)?; - for line in reader.lines() { - let line = line?; - if let Some(pretty_name) = line.strip_prefix("PRETTY_NAME=") { - if let Some(trimmed) = pretty_name - .strip_prefix('"') - .and_then(|s| s.strip_suffix('"')) - { - return Ok(trimmed.to_string()); - } - return Ok(pretty_name.to_string()); - } + for line in buffer.lines() { + if let Some(pretty_name) = line.strip_prefix("PRETTY_NAME=") { + if let Some(trimmed) = pretty_name + .strip_prefix('"') + .and_then(|s| s.strip_suffix('"')) + { + return Ok(trimmed.to_owned()); + } + return Ok(pretty_name.to_owned()); } - Ok("Unknown".to_string()) + } + Ok("Unknown".to_owned()) } diff --git a/src/system.rs b/src/system.rs index aed8752..ba90b27 100644 --- a/src/system.rs +++ b/src/system.rs @@ -1,87 +1,137 @@ -use crate::colors::COLORS; -use nix::sys::{statvfs::statvfs, utsname::UtsName}; use std::{ - env, - fs::File, - io::{self, Read}, + env, + fmt::Write as _, + fs::File, + io::{self, Read}, }; +use nix::sys::{statvfs::statvfs, utsname::UtsName}; + +use crate::colors::COLORS; + +#[must_use] +#[cfg_attr(feature = "hotpath", hotpath::measure)] pub fn get_username_and_hostname(utsname: &UtsName) -> String { - let username = env::var("USER").unwrap_or_else(|_| "unknown_user".to_string()); - let hostname = utsname - .nodename() - .to_str() - .unwrap_or("unknown_host") - .to_string(); - format!( - "{yellow}{username}{red}@{green}{hostname}{reset}", - yellow = COLORS.yellow, - red = COLORS.red, - green = COLORS.green, - reset = COLORS.reset, - ) + let username = env::var("USER").unwrap_or_else(|_| "unknown_user".to_owned()); + let hostname = utsname.nodename().to_str().unwrap_or("unknown_host"); + + let capacity = COLORS.yellow.len() + + username.len() + + COLORS.red.len() + + 1 + + COLORS.green.len() + + hostname.len() + + COLORS.reset.len(); + let mut result = String::with_capacity(capacity); + + result.push_str(COLORS.yellow); + result.push_str(&username); + result.push_str(COLORS.red); + result.push('@'); + result.push_str(COLORS.green); + result.push_str(hostname); + result.push_str(COLORS.reset); + + result } +#[must_use] +#[cfg_attr(feature = "hotpath", hotpath::measure)] pub fn get_shell() -> String { - let shell_path = env::var("SHELL").unwrap_or_else(|_| "unknown_shell".to_string()); - let shell_name = shell_path.rsplit('/').next().unwrap_or("unknown_shell"); - shell_name.to_string() + let shell_path = + env::var("SHELL").unwrap_or_else(|_| "unknown_shell".to_owned()); + + // Find last '/' and get the part after it, avoiding allocation + shell_path + .rsplit('/') + .next() + .unwrap_or("unknown_shell") + .to_owned() } +/// Gets the root disk usage information. +/// +/// # Errors +/// +/// Returns an error if the filesystem information cannot be retrieved. +#[cfg_attr(feature = "hotpath", hotpath::measure)] +#[allow(clippy::cast_precision_loss)] pub fn get_root_disk_usage() -> Result { - let vfs = statvfs("/")?; - let block_size = vfs.block_size() as u64; - let total_blocks = vfs.blocks(); - let available_blocks = vfs.blocks_available(); + let vfs = statvfs("/")?; + let block_size = vfs.block_size() as u64; + let total_blocks = vfs.blocks(); + let available_blocks = vfs.blocks_available(); - let total_size = block_size * total_blocks; - let used_size = total_size - (block_size * available_blocks); + let total_size = block_size * total_blocks; + let used_size = total_size - (block_size * available_blocks); - let total_size = total_size as f64 / (1024.0 * 1024.0 * 1024.0); - let used_size = used_size as f64 / (1024.0 * 1024.0 * 1024.0); - let usage = (used_size / total_size) * 100.0; + let total_size = total_size as f64 / (1024.0 * 1024.0 * 1024.0); + let used_size = used_size as f64 / (1024.0 * 1024.0 * 1024.0); + let usage = (used_size / total_size) * 100.0; - Ok(format!( - "{used_size:.2} GiB / {total_size:.2} GiB ({cyan}{usage:.0}%{reset})", - cyan = COLORS.cyan, - reset = COLORS.reset, - )) + let mut result = String::with_capacity(64); + write!( + result, + "{used_size:.2} GiB / {total_size:.2} GiB ({cyan}{usage:.0}%{reset})", + cyan = COLORS.cyan, + reset = COLORS.reset, + ) + .unwrap(); + + Ok(result) } +/// Gets the system memory usage information. +/// +/// # Errors +/// +/// Returns an error if `/proc/meminfo` cannot be read. +#[cfg_attr(feature = "hotpath", hotpath::measure)] pub fn get_memory_usage() -> Result { - fn parse_memory_info() -> Result<(f64, f64), io::Error> { - let mut total_memory_kb = 0.0; - let mut available_memory_kb = 0.0; - let mut meminfo = String::with_capacity(2048); + #[cfg_attr(feature = "hotpath", hotpath::measure)] + fn parse_memory_info() -> Result<(f64, f64), io::Error> { + let mut total_memory_kb = 0.0; + let mut available_memory_kb = 0.0; + let mut meminfo = String::with_capacity(2048); - File::open("/proc/meminfo")?.read_to_string(&mut meminfo)?; + File::open("/proc/meminfo")?.read_to_string(&mut meminfo)?; - for line in meminfo.lines() { - let mut split = line.split_whitespace(); - match split.next().unwrap_or_default() { - "MemTotal:" => total_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0), - "MemAvailable:" => { - available_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0); - // MemTotal comes before MemAvailable, stop parsing - break; - } - _ => (), - } - } - - let total_memory_gb = total_memory_kb / 1024.0 / 1024.0; - let available_memory_gb = available_memory_kb / 1024.0 / 1024.0; - let used_memory_gb = total_memory_gb - available_memory_gb; - - Ok((used_memory_gb, total_memory_gb)) + for line in meminfo.lines() { + let mut split = line.split_whitespace(); + match split.next().unwrap_or_default() { + "MemTotal:" => { + total_memory_kb = split.next().unwrap_or("0").parse().unwrap_or(0.0); + }, + "MemAvailable:" => { + available_memory_kb = + split.next().unwrap_or("0").parse().unwrap_or(0.0); + // MemTotal comes before MemAvailable, stop parsing + break; + }, + _ => (), + } } - let (used_memory, total_memory) = parse_memory_info()?; - let percentage_used = (used_memory / total_memory * 100.0).round() as u64; + let total_memory_gb = total_memory_kb / 1024.0 / 1024.0; + let available_memory_gb = available_memory_kb / 1024.0 / 1024.0; + let used_memory_gb = total_memory_gb - available_memory_gb; - Ok(format!( - "{used_memory:.2} GiB / {total_memory:.2} GiB ({cyan}{percentage_used}%{reset})", - cyan = COLORS.cyan, - reset = COLORS.reset, - )) + Ok((used_memory_gb, total_memory_gb)) + } + + let (used_memory, total_memory) = parse_memory_info()?; + #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)] + let percentage_used = (used_memory / total_memory * 100.0).round() as u64; + + let mut result = String::with_capacity(64); + write!( + result, + "{used_memory:.2} GiB / {total_memory:.2} GiB \ + ({cyan}{percentage_used}%{reset})", + cyan = COLORS.cyan, + reset = COLORS.reset, + ) + .unwrap(); + + Ok(result) } diff --git a/src/uptime.rs b/src/uptime.rs index 496052a..52188c2 100644 --- a/src/uptime.rs +++ b/src/uptime.rs @@ -1,40 +1,49 @@ -use std::{io, mem::MaybeUninit}; +use std::{fmt::Write, io, mem::MaybeUninit}; +/// Gets the current system uptime. +/// +/// # Errors +/// +/// Returns an error if the system uptime cannot be retrieved. +#[cfg_attr(feature = "hotpath", hotpath::measure)] pub fn get_current() -> Result { - let uptime_seconds = { - let mut info = MaybeUninit::uninit(); - if unsafe { libc::sysinfo(info.as_mut_ptr()) } != 0 { - return Err(io::Error::last_os_error()); - } - unsafe { info.assume_init().uptime as u64 } - }; + let uptime_seconds = { + let mut info = MaybeUninit::uninit(); + if unsafe { libc::sysinfo(info.as_mut_ptr()) } != 0 { + return Err(io::Error::last_os_error()); + } + #[allow(clippy::cast_sign_loss)] + unsafe { + info.assume_init().uptime as u64 + } + }; - let days = uptime_seconds / 86400; - let hours = (uptime_seconds / 3600) % 24; - let minutes = (uptime_seconds / 60) % 60; + let days = uptime_seconds / 86400; + let hours = (uptime_seconds / 3600) % 24; + let minutes = (uptime_seconds / 60) % 60; - let mut result = String::with_capacity(32); - if days > 0 { - result.push_str(&days.to_string()); - result.push_str(if days == 1 { " day" } else { " days" }); + let mut result = String::with_capacity(32); + if days > 0 { + let _ = write!(result, "{days}"); + result.push_str(if days == 1 { " day" } else { " days" }); + } + if hours > 0 { + if !result.is_empty() { + result.push_str(", "); } - if hours > 0 { - if !result.is_empty() { - result.push_str(", "); - } - result.push_str(&hours.to_string()); - result.push_str(if hours == 1 { " hour" } else { " hours" }); - } - if minutes > 0 { - if !result.is_empty() { - result.push_str(", "); - } - result.push_str(&minutes.to_string()); - result.push_str(if minutes == 1 { " minute" } else { " minutes" }); - } - if result.is_empty() { - result.push_str("less than a minute"); + let _ = write!(result, "{hours}"); + result.push_str(if hours == 1 { " hour" } else { " hours" }); + } + if minutes > 0 { + if !result.is_empty() { + result.push_str(", "); } + let _ = write!(result, "{minutes}"); + result.push_str(if minutes == 1 { " minute" } else { " minutes" }); + } + if result.is_empty() { + result.push_str("less than a minute"); + } - Ok(result) + Ok(result) }