Skip to content

Commit 898a57d

Browse files
feat: add rustc-hash feature (#42)
* 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 <[email protected]>
1 parent c108e1c commit 898a57d

File tree

6 files changed

+35
-6
lines changed

6 files changed

+35
-6
lines changed

Diff for: .github/workflows/CI.yml

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ env:
1212
jobs:
1313
cargo-test:
1414
runs-on: ubuntu-latest
15+
strategy:
16+
matrix:
17+
features:
18+
- default
19+
- rustc-hash
1520
steps:
1621
- uses: actions/checkout@v4
1722
- uses: dtolnay/rust-toolchain@stable
1823
- uses: Swatinem/rust-cache@v2
19-
- run: cargo test
24+
- run: cargo test --no-default-features --features '${{ matrix.features }}'

Diff for: COMMIT.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2e6f3bd1d32455e535de1d9ee154253c333aec73
1+
d1fa49b2e66c343210c413b68ed57f150b7b89d8

Diff for: Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ repository = "https://github.com/rust-lang/rustdoc-types"
99

1010
[dependencies]
1111
serde = {version="1", features=["derive"]}
12+
rustc-hash = {version="2", optional=true}
13+
14+
[features]
15+
default = []
16+
17+
# Switch the hashmaps used in rustdoc-types to the FxHashMap from rustc-hash.
18+
#
19+
# This might improve performace if your are reading the rustdoc JSON from large
20+
# crates like aws_sdk_ec2
21+
rustc-hash = ["dep:rustc-hash"]
1222

1323
[dev-dependencies]
1424
bincode = "1.3.3"

Diff for: README.md

+12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ let krate: rustdoc_types::Crate = serde_json::from_str(&json_string)?;
1313
println!("the index has {} items", krate.index.len());
1414
```
1515

16+
For performance sensitive crates, consider turning on the `rustc-hash`
17+
feature. This switches all data structures from `std::collections::HashMap` to
18+
`rustc-hash::FxHashMap` which improves performance when reading big JSON files
19+
(like `aws_sdk_rs`'s).
20+
21+
`cargo-semver-checks` benchmarked this change with `aws_sdk_ec2`'s JSON and
22+
[observed a -3% improvement to the runtime][csc benchmarks]. The performance
23+
here depends on how much time you spend querying the `HashMap`s, so as always,
24+
measure first.
25+
26+
[csc benchmarks]: https://rust-lang.zulipchat.com/#narrow/channel/266220-t-rustdoc/topic/rustc-hash.20and.20performance.20of.20rustdoc-types/near/474855731
27+
1628
## Contributing
1729

1830
This repo is a reexport of

Diff for: src/lib.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
//! These types are the public API exposed through the `--output-format json` flag. The [`Crate`]
44
//! struct is the root of the JSON blob and all other items are contained within.
55
6+
#[cfg(not(feature = "rustc-hash"))]
7+
use std::collections::HashMap;
68
use std::path::PathBuf;
79

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

14+
1115
/// The version of JSON output that this crate represents.
1216
///
1317
/// This integer is incremented with every breaking change to the API,

Diff for: update.sh

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ repo="rust"
99
branch="master"
1010

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

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

0 commit comments

Comments
 (0)