Skip to content

Commit

Permalink
feat: add rustc-hash feature (#42)
Browse files Browse the repository at this point in the history
* feat: add rustc-hash feature

Changes in preparation of [rust-lang/rust#131936][1]:

- Introduce `rustc-hash` dependency and feature.
- Modify the `update.sh` script accordingly.

[1]: rust-lang/rust#131936

* chore: run ./update.sh

* feat(ci): also test with the `rustc-hash` feature

* README reword

---------

Co-authored-by: Alona Enraght-Moony <code@alona.page>
  • Loading branch information
jalil-salame and aDotInTheVoid authored Oct 20, 2024
1 parent c108e1c commit 898a57d
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 6 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,13 @@ env:
jobs:
cargo-test:
runs-on: ubuntu-latest
strategy:
matrix:
features:
- default
- rustc-hash
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- uses: Swatinem/rust-cache@v2
- run: cargo test
- run: cargo test --no-default-features --features '${{ matrix.features }}'
2 changes: 1 addition & 1 deletion COMMIT.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2e6f3bd1d32455e535de1d9ee154253c333aec73
d1fa49b2e66c343210c413b68ed57f150b7b89d8
10 changes: 10 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ repository = "https://github.com/rust-lang/rustdoc-types"

[dependencies]
serde = {version="1", features=["derive"]}
rustc-hash = {version="2", optional=true}

[features]
default = []

# Switch the hashmaps used in rustdoc-types to the FxHashMap from rustc-hash.
#
# This might improve performace if your are reading the rustdoc JSON from large
# crates like aws_sdk_ec2
rustc-hash = ["dep:rustc-hash"]

[dev-dependencies]
bincode = "1.3.3"
Expand Down
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ let krate: rustdoc_types::Crate = serde_json::from_str(&json_string)?;
println!("the index has {} items", krate.index.len());
```

For performance sensitive crates, consider turning on the `rustc-hash`
feature. This switches all data structures from `std::collections::HashMap` to
`rustc-hash::FxHashMap` which improves performance when reading big JSON files
(like `aws_sdk_rs`'s).

`cargo-semver-checks` benchmarked this change with `aws_sdk_ec2`'s JSON and
[observed a -3% improvement to the runtime][csc benchmarks]. The performance
here depends on how much time you spend querying the `HashMap`s, so as always,
measure first.

[csc benchmarks]: https://rust-lang.zulipchat.com/#narrow/channel/266220-t-rustdoc/topic/rustc-hash.20and.20performance.20of.20rustdoc-types/near/474855731

## Contributing

This repo is a reexport of
Expand Down
6 changes: 5 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
//! These types are the public API exposed through the `--output-format json` flag. The [`Crate`]
//! struct is the root of the JSON blob and all other items are contained within.
#[cfg(not(feature = "rustc-hash"))]
use std::collections::HashMap;
use std::path::PathBuf;

use std::collections::HashMap;
#[cfg(feature = "rustc-hash")]
use rustc_hash::FxHashMap as HashMap;
use serde::{Deserialize, Serialize};


/// The version of JSON output that this crate represents.
///
/// This integer is incremented with every breaking change to the API,
Expand Down
4 changes: 1 addition & 3 deletions update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ repo="rust"
branch="master"

curl -# https://raw.githubusercontent.com/${user}/${repo}/${branch}/src/rustdoc-json-types/lib.rs \
| sed 's/rustc_hash::/std::collections::/g' \
| sed 's/FxHashMap/HashMap/g' \
| sed 's/^pub use /use /' \
| sed '/^pub type FxHashMap.*$/d' \
> src/lib.rs

curl -# https://raw.githubusercontent.com/${user}/${repo}/${branch}/src/rustdoc-json-types/tests.rs > src/tests.rs
Expand Down

0 comments on commit 898a57d

Please sign in to comment.