Unverified Commit bba6e762 authored by Tobias Koppers's avatar Tobias Koppers Committed by GitHub

turbopack: use Bitset for persisted chunk group sets (#100630)

Stacked on #100568, the top of the `Bitset` stack: #100566 (crate) →
#100643 → #100567 → #100568 → this.

## Summary

The PRs below move the module-graph traversals onto `Bitset`, but they
still convert to `RoaringBitmapWrapper` at the boundary, and persisted
values and production chunking stay Roaring. This PR finishes that,
using the bincode encoding and set operations that #100566 adds:

- **turbo-tasks** depends on `turbo-bitset` and implements `TaskInput`,
`NonLocalValue` and `ValueDebugFormat` for `Bitset`, next to its other
foreign-type impls.
- **turbopack-core** drops `RoaringBitmapWrapper` and the `roaring`
dependency:
- `ModuleToChunkGroups`, `ChunkingHeuristicsInfo::priority_routes`, the
module and chunk-item batch types, and production chunking use `Bitset`
directly.
- So do the local sets: `inherits_from`, cluster propagation,
`cluster_groups` and `pairs_with`.
- `Bitset::to_roaring()` (and its tests and benchmark) is removed, and
`roaring` becomes a dev-dependency of `turbo-bitset` (for the benchmarks
and the `sizes` example).
- `next-api` drops its unused `roaring` dependency, which the
workspace's unused-dependency lint reports once `turbopack-core` no
longer uses it.

**Cache compatibility:** the persisted encoding and value hashes of
these types change (task inputs are hashed with fixed-width integers,
for which the `Bitset` encoding always uses the bitmap form). The
persistent cache is versioned by `git describe`
(`turbo-tasks-backend/src/database/db_versioning.rs`), so normal builds
won't read old entries. With `TURBO_ENGINE_VERSION`,
`TURBO_ENGINE_DISABLE_VERSIONING` or `TURBO_ENGINE_IGNORE_DIRTY` in a
dirty checkout, clear the cache when switching between binaries with and
without this change.

## Results

> **Historical, not rerun:** measured in the previous review round,
before this stack was rebased onto canary `502b6b7d` and before the
latest review changes on `b149cf8a` (#100566), `c7ea9063` (#100643),
`e322a1c1` (#100567), `6536eeea` (#100568) and `417a41fd` (#100630).
This round only reran the `turbo-bitset` microbenchmarks in #100566.

Base is #100568's head, so the encoding of #100566 is the same on both
sides; only what this PR persists as `Bitset` instead of Roaring
differs.

### Full builds

Full `next build` (including prerendering) with uninstrumented
`next-swc` built with the repository's `release` profile (thin LTO), on
a shared VM with 8 vCPUs and 16 GB. 5 randomized, interleaved runs per
variant and app, median with min–max. Each run:

1. **cold**: removes `.next` and builds;
2. **cache**: measures `.next/cache` (the Turbopack build cache) with
`du -sb`;
3. **warm**: removes everything in `.next` except that variant's own
cache and builds again.

Caches are never shared between variants. Both binaries were built from
a checkout with uncommitted changes, so every build ran with
`TURBO_ENGINE_IGNORE_DIRTY=1` to keep the filesystem cache enabled (each
build logs that it is).

| full `next build` | base (#100568) | this PR |
|---|---|---|
| `bench/nested-deps-app-router-many-pages` (3,000 routes): cold build |
53.1 s (51.7–54.1) | 52.2 s (51–56.5) |
| `bench/nested-deps-app-router-many-pages` (3,000 routes):
`.next/cache` size | 762.95 MB (762.85–763.19) | 761.49 MB
(761.24–761.84) |
| `bench/nested-deps-app-router-many-pages` (3,000 routes): warm build
(own cache) | 35.8 s (34.8–36.7) | 35.5 s (34.8–36.6) |
| generated 300-route app: cold build | 11.3 s (10.8–11.7) | 11.1 s
(10.9–11.7) |
| generated 300-route app: `.next/cache` size | 213.84 MB
(213.45–214.45) | 213.85 MB (213.76–213.94) |
| generated 300-route app: warm build (own cache) | 5.42 s (5.14–5.5) |
5.46 s (5.18–5.57) |

All 40 builds succeeded with the same routes per app. The cache is
reused: warm builds take 48–68% of the cold time for both variants and
rewrite part of the cache. On `many-pages` the cache is 1.46 MB (0.19%)
smaller with non-overlapping ranges; on the generated app the difference
is within the run-to-run variation (±0.5 MB). Cold and warm times
overlap, so this makes no end-to-end speed claim.

<details>
<summary>Charts: full builds (linear)</summary>

Median wall time in seconds. Ranges overlap; exact values are in the
table above.

```mermaid
xychart-beta
    title "bench/nested-deps-app-router-many-pages: full build, median s"
    x-axis ["cold", "warm"]
    y-axis "s"
    bar "base (#100568)" [53.1, 35.8]
    bar "this PR" [52.2, 35.5]
```
```mermaid
xychart-beta
    title "generated 300-route app: full build, median s"
    x-axis ["cold", "warm"]
    y-axis "s"
    bar "base (#100568)" [11.3, 5.42]
    bar "this PR" [11.1, 5.46]
```

</details>

### Encoded sizes

Logical set payloads from local-only size probes (bytes of the encoded
values, not the cache database, so keys, metadata and compression aren't
included), same compile-mode builds as below. They are identical in
every run:

| logical payload | base (#100568) | this PR |
|---|---|---|
| `bench/nested-deps-app-router-many-pages` (3,000 routes): encoded
`ModuleToChunkGroups` values (33,779 sets) | 40,212,209 B | **9,711,974
B** (-76%) |
| `bench/nested-deps-app-router-many-pages` (3,000 routes): encoded
`ModuleBatch` chunk groups (4,132 sets) | 721,522 B | **97,208 B**
(-87%) |
| `bench/nested-deps-app-router-many-pages` (3,000 routes): encoded
`ModuleBatchGroup` chunk groups (3,021 sets) | 138,211 B | **27,960 B**
(-80%) |
| `bench/nested-deps-app-router-many-pages` (3,000 routes): per-module
set p50 / p99 / max | 19 / 6,023 / 8,211 B | **5 / 1,006 / 2,501 B** |
| generated 300-route app: encoded `ModuleToChunkGroups` values (12,240
sets) | 1,971,644 B | **434,660 B** (-78%) |
| generated 300-route app: encoded `ModuleBatch` chunk groups (2,101
sets) | 220,751 B | **42,221 B** (-81%) |
| generated 300-route app: encoded `ModuleBatchGroup` chunk groups (629
sets) | 30,165 B | **5,578 B** (-82%) |
| generated 300-route app: per-module set p50 / p99 / max | 19 / 1,241 /
3,687 B | **3 / 235 / 339 B** |

### Stage times

`next build --experimental-build-mode compile` with release `next-swc`
(LTO off, 16 codegen units) built from each PR's head plus local-only
stage timers. Median of 5 randomized, interleaved runs per variant,
min–max in parentheses, in ms. All five layers of the stack were
measured in the same interleaved set.

| stage (ms) | previous PR | this PR |
|---|---|---|
| `bench/nested-deps-app-router-many-pages` (3,000 routes): heuristics +
building the result | 37.7 (37.1–39.4) | **5.17 (4.66–5.39)** |
| `bench/nested-deps-app-router-many-pages` (3,000 routes): production
chunk merging (summed) | 317 (313–326) | **249 (240–253)** |
| generated 300-route app: heuristics + building the result | 3.71
(3.57–3.78) | **0.62 (0.574–0.657)** |
| generated 300-route app: production chunk merging (summed) | 19.3
(18.7–20.5) | **14.2 (13.6–14.8)** |

<details>
<summary>Charts: stage times (linear)</summary>

Median ms per stage; the first series is the previous PR. Exact values
and ranges are in the table above.

```mermaid
xychart-beta
    title "bench/nested-deps-app-router-many-pages, median ms"
    x-axis ["heuristics + building the result", "production chunk merging (summed)"]
    y-axis "ms"
    bar "previous PR" [37.7, 317]
    bar "this PR" [5.17, 249]
```
```mermaid
xychart-beta
    title "generated 300-route app, median ms"
    x-axis ["heuristics + building the result", "production chunk merging (summed)"]
    y-axis "ms"
    bar "previous PR" [3.71, 19.3]
    bar "this PR" [0.62, 14.2]
```

</details>

Sum of the measured module-graph stages per run (chunk group info and
building its result, collect, merged modules and interning), whole
stack:

| sum of module-graph stages (ms) | #100566 (before the usage PRs) | top
of stack (#100630) |
|---|---|---|
| `bench/nested-deps-app-router-many-pages` (3,000 routes) | 750
(746–767) | **102 (99.7–113)** |
| generated 300-route app | 36.3 (35.1–38) | **17.8 (17.5–18.1)** |

<details>
<summary>Charts: stage sum per layer (linear)</summary>

Median ms of the summed module-graph stages, one bar per layer of the
stack. Exact values for the ends are in the table above.

```mermaid
xychart-beta
    title "bench/nested-deps-app-router-many-pages: stage sum per layer, median ms"
    x-axis ["#100566", "#100643", "#100567", "#100568", "#100630"]
    y-axis "ms"
    bar "stage sum" [750, 455, 288, 128, 102]
```
```mermaid
xychart-beta
    title "generated 300-route app: stage sum per layer, median ms"
    x-axis ["#100566", "#100643", "#100567", "#100568", "#100630"]
    y-axis "ms"
    bar "stage sum" [36.3, 28.8, 27.1, 21, 17.8]
```

</details>

Visit counts and the emitted output are identical across all layers on
both apps (14,028 and 2,142 files, same digests).

## Verification

- `cargo test -p turbo-bitset`, `-p turbo-tasks --lib`, `-p
turbopack-core --lib`, the turbopack snapshot tests (no snapshot
changes) and execution tests.
- `cargo fmt --check`, `cargo clippy` (touched crates plus `next-api`,
`next-core`, all targets) and `cargo doc` with `-D warnings` (no
unused-dependency warning).
- `cargo tree -p turbopack-core -e normal` no longer contains `roaring`.
- The builds above, with timers and probes kept local only.

<!-- NEXT_JS_LLM -->


<!-- fleet b56269ed-2145-4f30-b0fe-dc9762520521 -->

---------
Co-authored-by: 's avatarvercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
Co-authored-by: 's avatarVercel Fleet <fleet@vercel.com>
Co-authored-by: 's avatarTobias Koppers <1365881+sokra@users.noreply.github.com>
parent 68438424
...@@ -4157,7 +4157,6 @@ dependencies = [ ...@@ -4157,7 +4157,6 @@ dependencies = [
"itertools 0.10.5", "itertools 0.10.5",
"next-core", "next-core",
"regex", "regex",
"roaring",
"rustc-hash 2.1.1", "rustc-hash 2.1.1",
"serde", "serde",
"serde_json", "serde_json",
...@@ -9207,6 +9206,7 @@ dependencies = [ ...@@ -9207,6 +9206,7 @@ dependencies = [
"tracing", "tracing",
"triomphe 0.1.12", "triomphe 0.1.12",
"turbo-bincode", "turbo-bincode",
"turbo-bitset",
"turbo-dyn-eq-hash", "turbo-dyn-eq-hash",
"turbo-frozenmap", "turbo-frozenmap",
"turbo-rcstr", "turbo-rcstr",
...@@ -9645,7 +9645,6 @@ dependencies = [ ...@@ -9645,7 +9645,6 @@ dependencies = [
"petgraph 0.8.3", "petgraph 0.8.3",
"ref-cast", "ref-cast",
"regex", "regex",
"roaring",
"rstest", "rstest",
"rustc-hash 2.1.1", "rustc-hash 2.1.1",
"serde", "serde",
......
...@@ -28,7 +28,6 @@ itertools = { workspace = true } ...@@ -28,7 +28,6 @@ itertools = { workspace = true }
indexmap = { workspace = true } indexmap = { workspace = true }
next-core = { workspace = true } next-core = { workspace = true }
regex = { workspace = true } regex = { workspace = true }
roaring = { workspace = true }
rustc-hash = { workspace = true } rustc-hash = { workspace = true }
serde = { workspace = true } serde = { workspace = true }
serde_json = { workspace = true } serde_json = { workspace = true }
......
...@@ -10,7 +10,6 @@ workspace = true ...@@ -10,7 +10,6 @@ workspace = true
[dependencies] [dependencies]
bincode = { workspace = true } bincode = { workspace = true }
roaring = { workspace = true }
shrink-to-fit = { workspace = true } shrink-to-fit = { workspace = true }
[dev-dependencies] [dev-dependencies]
...@@ -18,8 +17,8 @@ criterion = { workspace = true } ...@@ -18,8 +17,8 @@ criterion = { workspace = true }
fixedbitset = { workspace = true } fixedbitset = { workspace = true }
# The benchmark inputs are drawn from a seeded generator. # The benchmark inputs are drawn from a seeded generator.
rand = { workspace = true } rand = { workspace = true }
# The benchmarks and the `sizes` example also compare with the serde serialization of # The benchmarks and the `sizes` example compare with `RoaringBitmap`, including its
# `RoaringBitmap`. # serialization through serde.
roaring = { workspace = true, features = ["serde"] } roaring = { workspace = true, features = ["serde"] }
rustc-hash = { workspace = true } rustc-hash = { workspace = true }
# The benchmarks and the `sizes` example encode, decode and hash like Turbopack does. # The benchmarks and the `sizes` example encode, decode and hash like Turbopack does.
......
...@@ -603,16 +603,6 @@ pub fn bench(c: &mut Criterion) { ...@@ -603,16 +603,6 @@ pub fn bench(c: &mut Criterion) {
}), }),
); );
// Converting into a `RoaringBitmap`, the cost at the output boundary of the algorithms.
let mut group = c.benchmark_group("bitset/to_roaring");
for input in &inputs {
let set = bitset(&input.bits);
group.bench_function(BenchmarkId::new("Bitset", &input.name), |b| {
b.iter(|| black_box(black_box(&set).to_roaring()))
});
}
group.finish();
// Encoding a set into a new buffer. // Encoding a set into a new buffer.
let mut group = c.benchmark_group("bitset/serialize"); let mut group = c.benchmark_group("bitset/serialize");
for input in &inputs { for input in &inputs {
......
...@@ -22,7 +22,6 @@ use std::{ ...@@ -22,7 +22,6 @@ use std::{
ops::{BitAnd, BitOrAssign, Sub}, ops::{BitAnd, BitOrAssign, Sub},
}; };
use roaring::RoaringBitmap;
use shrink_to_fit::ShrinkToFit; use shrink_to_fit::ShrinkToFit;
/// Number of bits per storage block. /// Number of bits per storage block.
...@@ -355,62 +354,6 @@ impl Bitset { ...@@ -355,62 +354,6 @@ impl Bitset {
superset && strict superset && strict
} }
/// Converts the set into a [`RoaringBitmap`] with the same bits.
///
/// This builds every roaring container (a range of 2^16 bits) directly from the stored blocks
/// instead of inserting each bit, and produces the same container types as inserting the bits
/// one by one would, so the result is equal (and serializes identically) to an inserted one.
pub fn to_roaring(&self) -> RoaringBitmap {
/// The number of blocks per roaring container.
const CONTAINER_BLOCKS: usize = (1 << 16) / BLOCK_BITS;
/// Roaring stores a container with up to this many bits as a sorted array.
const ROARING_ARRAY_LIMIT: u32 = 4096;
let (first_block, blocks) = self.trimmed();
let end = first_block + blocks.len();
let mut result = RoaringBitmap::new();
let mut bytes = Vec::new();
let mut index = first_block;
while index < end {
let chunk_end = ((index / CONTAINER_BLOCKS + 1) * CONTAINER_BLOCKS).min(end);
let chunk = &blocks[index - first_block..chunk_end - first_block];
let count = chunk.iter().map(|b| b.count_ones()).sum::<u32>();
let part = if count == 0 {
None
} else if count == ROARING_ARRAY_LIMIT {
// `from_lsb0_bytes` stores exactly this many bits as a bitmap, but inserting them
// stores them as an array, so insert them instead.
let bits = Iter {
blocks: chunk,
block_index: index,
current: 0,
};
Some(
RoaringBitmap::from_sorted_iter(bits)
.expect("the bits of an Bitset are iterated in ascending order"),
)
} else {
bytes.clear();
bytes.extend(chunk.iter().flat_map(|block| block.to_le_bytes()));
// A chunk is at most one container, so the range fits into `u32`.
Some(RoaringBitmap::from_lsb0_bytes(
(index * BLOCK_BITS) as u32,
&bytes,
))
};
if let Some(part) = part {
if result.is_empty() {
result = part;
} else {
// The parts are in different containers, so this only adds containers.
result |= part;
}
}
index = chunk_end;
}
result
}
/// The blocks of both sets over the range of block indices stored in both, and the index of /// The blocks of both sets over the range of block indices stored in both, and the index of
/// the first of them. Bits outside of this range are in at most one of the sets. /// the first of them. Bits outside of this range are in at most one of the sets.
fn overlap<'a>(&'a self, other: &'a Self) -> (usize, &'a [usize], &'a [usize]) { fn overlap<'a>(&'a self, other: &'a Self) -> (usize, &'a [usize], &'a [usize]) {
......
...@@ -11,7 +11,6 @@ use bincode::{ ...@@ -11,7 +11,6 @@ use bincode::{
error::{DecodeError, EncodeError}, error::{DecodeError, EncodeError},
}; };
use rand::{RngExt, SeedableRng, rngs::SmallRng, seq::SliceRandom}; use rand::{RngExt, SeedableRng, rngs::SmallRng, seq::SliceRandom};
use roaring::RoaringBitmap;
use rustc_hash::FxBuildHasher; use rustc_hash::FxBuildHasher;
use shrink_to_fit::ShrinkToFit; use shrink_to_fit::ShrinkToFit;
...@@ -120,13 +119,6 @@ fn check(set: &Bitset, expected: &BTreeSet<u32>) { ...@@ -120,13 +119,6 @@ fn check(set: &Bitset, expected: &BTreeSet<u32>) {
); );
} }
check_encoding(set, expected); check_encoding(set, expected);
let roaring = set.to_roaring();
assert_eq!(
roaring.iter().collect::<Vec<_>>(),
expected.iter().copied().collect::<Vec<_>>()
);
// Same internal representation as inserting the bits.
assert_eq!(roaring, expected.iter().copied().collect::<RoaringBitmap>());
// Rebuilding from the bits gives an equal set. // Rebuilding from the bits gives an equal set.
let rebuilt = expected.iter().copied().collect::<Bitset>(); let rebuilt = expected.iter().copied().collect::<Bitset>();
assert_eq!(&rebuilt, set); assert_eq!(&rebuilt, set);
...@@ -1671,42 +1663,6 @@ fn shrink_to_fit() { ...@@ -1671,42 +1663,6 @@ fn shrink_to_fit() {
check(&empty, &BTreeSet::new()); check(&empty, &BTreeSet::new());
} }
#[test]
fn to_roaring() {
// Bits spanning several containers, and the highest possible bits. (A set with both bit 0 and
// `u32::MAX` would allocate 512 MiB, as the set is meant for bounded ranges.)
for bits in [
&[0, 1, 63, 64, 65_535, 65_536, 1 << 20][..],
&[u32::MAX - 70_000, u32::MAX - 1, u32::MAX],
] {
let (set, expected) = from_bits(bits);
let roaring = set.to_roaring();
assert_eq!(roaring, expected.iter().copied().collect::<RoaringBitmap>());
}
}
#[test]
fn to_roaring_container_types() {
// A dense container (stored as a bitmap by roaring), a sparse one (stored as an array), a
// container at exactly the array limit, and a range that starts in the middle of a container.
let bits = (0..5_000)
.chain(70_000..70_010)
.chain((2 << 16)..(2 << 16) + 4_096)
.chain((3 << 16) + 1_000..(3 << 16) + 9_000)
.collect::<Vec<u32>>();
let (set, expected) = from_bits(&bits);
check(&set, &expected);
let roaring = set.to_roaring();
let inserted = bits.iter().copied().collect::<RoaringBitmap>();
assert_eq!(roaring, inserted);
// The serialization (which `RoaringBitmapWrapper` uses for hashing) is identical too.
let mut a = Vec::new();
let mut b = Vec::new();
roaring.serialize_into(&mut a).unwrap();
inserted.serialize_into(&mut b).unwrap();
assert_eq!(a, b);
}
/// A small deterministic linear congruential generator, so the test needs no random number crate. /// A small deterministic linear congruential generator, so the test needs no random number crate.
struct Lcg(u64); struct Lcg(u64);
......
...@@ -56,6 +56,7 @@ tokio = { workspace = true, features = ["full"] } ...@@ -56,6 +56,7 @@ tokio = { workspace = true, features = ["full"] }
tracing = { workspace = true } tracing = { workspace = true }
triomphe = { workspace = true, features = ["unsize", "unstable"] } triomphe = { workspace = true, features = ["unsize", "unstable"] }
turbo-bincode = { workspace = true } turbo-bincode = { workspace = true }
turbo-bitset = { workspace = true }
turbo-dyn-eq-hash = { workspace = true } turbo-dyn-eq-hash = { workspace = true }
turbo-frozenmap = { workspace = true } turbo-frozenmap = { workspace = true }
turbo-rcstr = { workspace = true } turbo-rcstr = { workspace = true }
......
...@@ -63,6 +63,7 @@ mod detail { ...@@ -63,6 +63,7 @@ mod detail {
use auto_hash_map::{AutoMap, AutoSet}; use auto_hash_map::{AutoMap, AutoSet};
use futures::future::BoxFuture; use futures::future::BoxFuture;
use smallvec::SmallVec; use smallvec::SmallVec;
use turbo_bitset::Bitset;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use super::ValueDebugFormat; use super::ValueDebugFormat;
...@@ -103,6 +104,12 @@ mod detail { ...@@ -103,6 +104,12 @@ mod detail {
} }
} }
impl ValueDebugFormat for Bitset {
fn value_debug_format(&self, _depth: usize) -> ValueDebugFormatString<'_> {
ValueDebugFormatString::Sync(format!("{self:?}"))
}
}
// Use autoref specialization [1] to implement `ValueDebugFormat` for `T: // Use autoref specialization [1] to implement `ValueDebugFormat` for `T:
// Debug` as a fallback if `T` does not implement it directly, hence the `for // Debug` as a fallback if `T` does not implement it directly, hence the `for
// &T` clause. // &T` clause.
......
...@@ -36,6 +36,7 @@ macro_rules! impl_auto_marker_trait { ...@@ -36,6 +36,7 @@ macro_rules! impl_auto_marker_trait {
::std::time::Duration, ::std::time::Duration,
::anyhow::Error, ::anyhow::Error,
::turbo_rcstr::RcStr, ::turbo_rcstr::RcStr,
::turbo_bitset::Bitset,
); );
$crate::marker_trait::impl_marker_trait!($trait: ::std::path::Path, ::std::path::PathBuf); $crate::marker_trait::impl_marker_trait!($trait: ::std::path::Path, ::std::path::PathBuf);
$crate::marker_trait::impl_marker_trait!( $crate::marker_trait::impl_marker_trait!(
......
...@@ -19,6 +19,7 @@ use bincode::{ ...@@ -19,6 +19,7 @@ use bincode::{
}; };
use either::Either; use either::Either;
use smallvec::{Array, SmallVec}; use smallvec::{Array, SmallVec};
use turbo_bitset::Bitset;
use turbo_frozenmap::{FrozenMap, FrozenSet}; use turbo_frozenmap::{FrozenMap, FrozenSet};
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks_hash::HashAlgorithm; use turbo_tasks_hash::HashAlgorithm;
...@@ -172,7 +173,8 @@ impl_task_input! { ...@@ -172,7 +173,8 @@ impl_task_input! {
ValueTypeId, ValueTypeId,
Duration, Duration,
String, String,
HashAlgorithm HashAlgorithm,
Bitset
} }
impl<T> TaskInput for Vec<T> impl<T> TaskInput for Vec<T>
......
...@@ -30,7 +30,6 @@ itertools = { workspace = true } ...@@ -30,7 +30,6 @@ itertools = { workspace = true }
num-bigint = "0.4" num-bigint = "0.4"
patricia_tree = { version = "0.10.1", features = ["serde"] } patricia_tree = { version = "0.10.1", features = ["serde"] }
petgraph = { workspace = true, features = ["serde-1"] } petgraph = { workspace = true, features = ["serde-1"] }
roaring = { workspace = true, features = ["serde"] }
ref-cast = "1.0.20" ref-cast = "1.0.20"
rustc-hash = { workspace = true } rustc-hash = { workspace = true }
regex = { workspace = true } regex = { workspace = true }
......
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use roaring::RoaringBitmap; use turbo_bitset::Bitset;
use turbo_tasks::{FxIndexSet, ResolvedVc, TryJoinIterExt, Vc}; use turbo_tasks::{FxIndexSet, ResolvedVc, TryJoinIterExt, Vc};
use turbo_tasks_hash::Xxh3Hash64Hasher; use turbo_tasks_hash::Xxh3Hash64Hasher;
use crate::module_graph::{ use crate::module_graph::{
ModuleGraph, ModuleGraph,
chunk_group_info::{ChunkGroupId, ChunkGroupKey, RoaringBitmapWrapper}, chunk_group_info::{ChunkGroupId, ChunkGroupKey},
}; };
/// Chunk groups whose content is already available. /// Chunk groups whose content is already available.
...@@ -22,7 +22,7 @@ use crate::module_graph::{ ...@@ -22,7 +22,7 @@ use crate::module_graph::{
pub struct AvailableChunkGroups { pub struct AvailableChunkGroups {
/// The module graph whose `ChunkGroupInfo` the indices in `chunk_groups` refer to. /// The module graph whose `ChunkGroupInfo` the indices in `chunk_groups` refer to.
module_graph: ResolvedVc<ModuleGraph>, module_graph: ResolvedVc<ModuleGraph>,
chunk_groups: RoaringBitmapWrapper, chunk_groups: Bitset,
} }
impl AvailableChunkGroups { impl AvailableChunkGroups {
...@@ -39,7 +39,7 @@ impl AvailableChunkGroups { ...@@ -39,7 +39,7 @@ impl AvailableChunkGroups {
} }
/// The chunk groups that are available, for `module_graph`. /// The chunk groups that are available, for `module_graph`.
pub fn chunk_groups(&self, module_graph: ResolvedVc<ModuleGraph>) -> &RoaringBitmapWrapper { pub fn chunk_groups(&self, module_graph: ResolvedVc<ModuleGraph>) -> &Bitset {
self.assert_module_graph(module_graph); self.assert_module_graph(module_graph);
&self.chunk_groups &self.chunk_groups
} }
...@@ -49,7 +49,7 @@ impl AvailableChunkGroups { ...@@ -49,7 +49,7 @@ impl AvailableChunkGroups {
impl AvailableChunkGroups { impl AvailableChunkGroups {
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn new(module_graph: ResolvedVc<ModuleGraph>, chunk_group: u32) -> Vc<Self> { pub fn new(module_graph: ResolvedVc<ModuleGraph>, chunk_group: u32) -> Vc<Self> {
let mut chunk_groups = RoaringBitmapWrapper::default(); let mut chunk_groups = Bitset::new();
chunk_groups.insert(chunk_group); chunk_groups.insert(chunk_group);
Self { Self {
module_graph, module_graph,
...@@ -97,7 +97,7 @@ impl AvailableChunkGroups { ...@@ -97,7 +97,7 @@ impl AvailableChunkGroups {
let from = self.module_graph.chunk_group_info().await?; let from = self.module_graph.chunk_group_info().await?;
let to = module_graph.chunk_group_info().await?; let to = module_graph.chunk_group_info().await?;
let mut translated = RoaringBitmap::new(); let mut translated = Bitset::new();
for id in self.chunk_groups.iter() { for id in self.chunk_groups.iter() {
if let Some(id) = if let Some(id) =
translate_chunk_group(&from.chunk_group_keys, &to.chunk_group_keys, id) translate_chunk_group(&from.chunk_group_keys, &to.chunk_group_keys, id)
...@@ -108,7 +108,7 @@ impl AvailableChunkGroups { ...@@ -108,7 +108,7 @@ impl AvailableChunkGroups {
Ok(Self { Ok(Self {
module_graph, module_graph,
chunk_groups: RoaringBitmapWrapper(translated), chunk_groups: translated,
} }
.cell()) .cell())
} }
......
...@@ -5,6 +5,7 @@ use bincode::{Decode, Encode}; ...@@ -5,6 +5,7 @@ use bincode::{Decode, Encode};
use either::Either; use either::Either;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use smallvec::{SmallVec, smallvec}; use smallvec::{SmallVec, smallvec};
use turbo_bitset::Bitset;
use turbo_tasks::{FxIndexMap, ReadRef, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Vc}; use turbo_tasks::{FxIndexMap, ReadRef, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, Vc};
use crate::{ use crate::{
...@@ -12,7 +13,6 @@ use crate::{ ...@@ -12,7 +13,6 @@ use crate::{
module_graph::{ module_graph::{
ModuleGraph, ModuleGraph,
async_module_info::AsyncModulesInfo, async_module_info::AsyncModulesInfo,
chunk_group_info::RoaringBitmapWrapper,
module_batch::{ChunkableModuleBatchGroup, ChunkableModuleOrBatch, ModuleBatch}, module_batch::{ChunkableModuleBatchGroup, ChunkableModuleOrBatch, ModuleBatch},
}, },
}; };
...@@ -118,7 +118,7 @@ impl ChunkItemOrBatchWithAsyncModuleInfo { ...@@ -118,7 +118,7 @@ impl ChunkItemOrBatchWithAsyncModuleInfo {
#[derive(Debug, Clone, Hash)] #[derive(Debug, Clone, Hash)]
pub struct ChunkItemBatchWithAsyncModuleInfo { pub struct ChunkItemBatchWithAsyncModuleInfo {
pub chunk_items: Vec<ChunkItemWithAsyncModuleInfo>, pub chunk_items: Vec<ChunkItemWithAsyncModuleInfo>,
pub chunk_groups: Option<RoaringBitmapWrapper>, pub chunk_groups: Option<Bitset>,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -236,7 +236,7 @@ pub struct ChunkItemBatchGroups(Vec<ResolvedVc<ChunkItemBatchGroup>>); ...@@ -236,7 +236,7 @@ pub struct ChunkItemBatchGroups(Vec<ResolvedVc<ChunkItemBatchGroup>>);
#[turbo_tasks::value] #[turbo_tasks::value]
pub struct ChunkItemBatchGroup { pub struct ChunkItemBatchGroup {
pub items: Vec<ChunkItemOrBatchWithAsyncModuleInfo>, pub items: Vec<ChunkItemOrBatchWithAsyncModuleInfo>,
pub chunk_groups: RoaringBitmapWrapper, pub chunk_groups: Bitset,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
......
use std::{borrow::Cow, collections::BinaryHeap, hash::BuildHasherDefault, mem::take}; use std::{borrow::Cow, collections::BinaryHeap, hash::BuildHasherDefault, mem::take};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use roaring::RoaringBitmap;
use rustc_hash::{FxHashMap, FxHasher}; use rustc_hash::{FxHashMap, FxHasher};
use smallvec::SmallVec; use smallvec::SmallVec;
use tracing::{Instrument, field::Empty}; use tracing::{Instrument, field::Empty};
use turbo_bitset::Bitset;
use turbo_prehash::BuildHasherExt; use turbo_prehash::BuildHasherExt;
use turbo_tasks::{FxIndexMap, FxIndexSet, MappedReadRef, ReadRef, ResolvedVc, TryJoinIterExt, Vc}; use turbo_tasks::{FxIndexMap, FxIndexSet, MappedReadRef, ReadRef, ResolvedVc, TryJoinIterExt, Vc};
...@@ -14,10 +14,7 @@ use crate::{ ...@@ -14,10 +14,7 @@ use crate::{
ChunkingConfig, ChunkingConfig,
chunking::{ChunkItemOrBatchWithInfo, ComponentChunkItems, SplitContext, make_chunk}, chunking::{ChunkItemOrBatchWithInfo, ComponentChunkItems, SplitContext, make_chunk},
}, },
module_graph::{ module_graph::{ModuleGraph, chunk_group_info::ModuleToChunkGroups},
ModuleGraph,
chunk_group_info::{ModuleToChunkGroups, RoaringBitmapWrapper},
},
}; };
/// Default estimated cost of an additional request, in bytes (200 KB). /// Default estimated cost of an additional request, in bytes (200 KB).
...@@ -57,7 +54,7 @@ pub async fn make_production_chunks( ...@@ -57,7 +54,7 @@ pub async fn make_production_chunks(
let mut grouped_chunk_items = FxIndexMap::<_, GroupedChunkItems<'_>>::default(); let mut grouped_chunk_items = FxIndexMap::<_, GroupedChunkItems<'_>>::default();
enum Prepared { enum Prepared {
ChunkItem(MappedReadRef<ModuleToChunkGroups, RoaringBitmapWrapper>), ChunkItem(MappedReadRef<ModuleToChunkGroups, Bitset>),
Batch(ReadRef<ChunkItemBatchWithAsyncModuleInfo>), Batch(ReadRef<ChunkItemBatchWithAsyncModuleInfo>),
None, None,
} }
...@@ -345,7 +342,7 @@ pub async fn make_production_chunks( ...@@ -345,7 +342,7 @@ pub async fn make_production_chunks(
if let (Some(a), Some(b)) = if let (Some(a), Some(b)) =
(&candidate.chunk_groups, &other.chunk_groups) (&candidate.chunk_groups, &other.chunk_groups)
{ {
let o = &***a & &***b; // `o_groups` (Z) let o = &**a & &**b; // `o_groups` (Z)
// if there is one chunk group in `o_groups` that is used by a // if there is one chunk group in `o_groups` that is used by a
// priority route, we should prioritise merging these two chunk // priority route, we should prioritise merging these two chunk
...@@ -353,11 +350,11 @@ pub async fn make_production_chunks( ...@@ -353,11 +350,11 @@ pub async fn make_production_chunks(
is_priority_route = !o.is_disjoint(&heuristics.priority_routes); is_priority_route = !o.is_disjoint(&heuristics.priority_routes);
if has_clusters { if has_clusters {
let x = &***a - &o; // a_rem groups: load only chunk A let x = &**a - &o; // a_rem groups: load only chunk A
let y = &***b - &o; // b_rem groups: load only chunk B let y = &**b - &o; // b_rem groups: load only chunk B
// Map each cluster to the candidate groups it contains. // Map each cluster to the candidate groups it contains.
let mut cluster_groups: FxHashMap<u16, RoaringBitmap> = let mut cluster_groups: FxHashMap<u16, Bitset> =
FxHashMap::default(); FxHashMap::default();
for set in [&x, &y, &o] { for set in [&x, &y, &o] {
for index in set.iter() { for index in set.iter() {
...@@ -370,7 +367,7 @@ pub async fn make_production_chunks( ...@@ -370,7 +367,7 @@ pub async fn make_production_chunks(
// Groups sharing >= 1 cluster with `index`, deduped across // Groups sharing >= 1 cluster with `index`, deduped across
// clusters (excluding `index` itself) so each pair counts once. // clusters (excluding `index` itself) so each pair counts once.
let pairs_with = |index: u32| { let pairs_with = |index: u32| {
let mut p = RoaringBitmap::new(); let mut p = Bitset::new();
for &c in &heuristics.clusters[index as usize] { for &c in &heuristics.clusters[index as usize] {
if let Some(groups) = cluster_groups.get(&c) { if let Some(groups) = cluster_groups.get(&c) {
p |= groups; p |= groups;
...@@ -641,7 +638,7 @@ struct ChunkCandidate<'l> { ...@@ -641,7 +638,7 @@ struct ChunkCandidate<'l> {
size: usize, size: usize,
chunk_items: Vec<&'l ChunkItemOrBatchWithInfo>, chunk_items: Vec<&'l ChunkItemOrBatchWithInfo>,
batch_groups: SmallVec<[ResolvedVc<ChunkItemBatchGroup>; 1]>, batch_groups: SmallVec<[ResolvedVc<ChunkItemBatchGroup>; 1]>,
chunk_groups: Option<Cow<'l, RoaringBitmapWrapper>>, chunk_groups: Option<Cow<'l, Bitset>>,
/// Original groups this candidate covers; one per chunk, > 1 once merged. /// Original groups this candidate covers; one per chunk, > 1 once merged.
components: Vec<ChunkComponent<'l>>, components: Vec<ChunkComponent<'l>>,
} }
...@@ -670,7 +667,7 @@ struct MergeCandidate<'l> { ...@@ -670,7 +667,7 @@ struct MergeCandidate<'l> {
size: usize, size: usize,
chunk_items: Vec<&'l ChunkItemOrBatchWithInfo>, chunk_items: Vec<&'l ChunkItemOrBatchWithInfo>,
batch_groups: SmallVec<[ResolvedVc<ChunkItemBatchGroup>; 1]>, batch_groups: SmallVec<[ResolvedVc<ChunkItemBatchGroup>; 1]>,
chunk_groups: Option<Cow<'l, RoaringBitmapWrapper>>, chunk_groups: Option<Cow<'l, Bitset>>,
/// Original groups this candidate covers; one per chunk, > 1 once merged. /// Original groups this candidate covers; one per chunk, > 1 once merged.
components: Vec<ChunkComponent<'l>>, components: Vec<ChunkComponent<'l>>,
} }
...@@ -679,7 +676,7 @@ impl MergeCandidate<'_> { ...@@ -679,7 +676,7 @@ impl MergeCandidate<'_> {
fn chunk_groups_len(&self) -> u64 { fn chunk_groups_len(&self) -> u64 {
self.chunk_groups self.chunk_groups
.as_ref() .as_ref()
.map_or(0, |chunk_groups| chunk_groups.len()) .map_or(0, |chunk_groups| chunk_groups.count_ones() as u64)
} }
} }
...@@ -744,25 +741,22 @@ fn split_into_component_chunks<'l>( ...@@ -744,25 +741,22 @@ fn split_into_component_chunks<'l>(
} }
} }
fn overlap( fn overlap(chunk_groups: &Option<Cow<'_, Bitset>>, chunk_groups2: &Option<Cow<'_, Bitset>>) -> u64 {
chunk_groups: &Option<Cow<'_, RoaringBitmapWrapper>>,
chunk_groups2: &Option<Cow<'_, RoaringBitmapWrapper>>,
) -> u64 {
if let (Some(chunk_groups), Some(chunk_groups2)) = (chunk_groups, chunk_groups2) { if let (Some(chunk_groups), Some(chunk_groups2)) = (chunk_groups, chunk_groups2) {
chunk_groups.intersection_len(chunk_groups2) chunk_groups.intersection_len(chunk_groups2) as u64
} else { } else {
0 0
} }
} }
fn merge_chunk_groups<'l>( fn merge_chunk_groups<'l>(
chunk_groups: &Option<Cow<'l, RoaringBitmapWrapper>>, chunk_groups: &Option<Cow<'l, Bitset>>,
chunk_groups2: &Option<Cow<'l, RoaringBitmapWrapper>>, chunk_groups2: &Option<Cow<'l, Bitset>>,
) -> Option<Cow<'l, RoaringBitmapWrapper>> { ) -> Option<Cow<'l, Bitset>> {
if let (Some(chunk_groups), Some(chunk_groups2)) = (chunk_groups, chunk_groups2) { if let (Some(chunk_groups), Some(chunk_groups2)) = (chunk_groups, chunk_groups2) {
let l = &**chunk_groups.as_ref(); let l = chunk_groups.as_ref();
let r = &**chunk_groups2.as_ref(); let r = chunk_groups2.as_ref();
Some(Cow::Owned(RoaringBitmapWrapper(l & r))) Some(Cow::Owned(l & r))
} else { } else {
None None
} }
......
use std::{ use std::{cmp::Reverse, hash::Hash, ops::Deref, sync::LazyLock};
cmp::Reverse,
hash::Hash,
ops::{Deref, DerefMut},
sync::LazyLock,
};
use anyhow::{Context, Result, bail}; use anyhow::{Context, Result, bail};
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use either::Either; use either::Either;
use indexmap::map::Entry; use indexmap::map::Entry;
use roaring::RoaringBitmap;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use tracing::Instrument; use tracing::Instrument;
use turbo_bitset::Bitset; use turbo_bitset::Bitset;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ use turbo_tasks::{
FxIndexMap, FxIndexSet, NonLocalValue, ResolvedVc, TaskInput, TryJoinIterExt, ValueToString, FxIndexMap, FxIndexSet, NonLocalValue, ResolvedVc, TryJoinIterExt, ValueToString, Vc,
Vc, debug::ValueDebugFormat, turbofmt, debug::ValueDebugFormat, turbofmt,
}; };
use turbo_tasks_hash::{DeterministicHash, DeterministicHasher};
use crate::{ use crate::{
chunk::ChunkingType, chunk::ChunkingType,
...@@ -26,81 +19,8 @@ use crate::{ ...@@ -26,81 +19,8 @@ use crate::{
module_graph::{GraphTraversalAction, ModuleGraph, RefData}, module_graph::{GraphTraversalAction, ModuleGraph, RefData},
}; };
#[derive(Clone, Debug, Default, PartialEq, ValueDebugFormat, Encode, Decode)]
#[repr(transparent)]
pub struct RoaringBitmapWrapper(#[bincode(with_serde)] pub RoaringBitmap);
impl TaskInput for RoaringBitmapWrapper {
fn is_transient(&self) -> bool {
false
}
}
impl RoaringBitmapWrapper {
/// Whether `self` contains bits that are not in `other`
///
/// The existing `is_superset` method also returns true for equal sets
pub fn is_proper_superset(&self, other: &Self) -> bool {
!self.is_subset(other)
}
pub fn into_inner(self) -> RoaringBitmap {
self.0
}
}
unsafe impl NonLocalValue for RoaringBitmapWrapper {}
// RoaringBitmap doesn't impl Eq: https://github.com/RoaringBitmap/roaring-rs/issues/302
// PartialEq can only return true if both bitmaps have the same internal representation, but two
// bitmaps with the same content should always have the same internal representation
impl Eq for RoaringBitmapWrapper {}
impl Deref for RoaringBitmapWrapper {
type Target = RoaringBitmap;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for RoaringBitmapWrapper {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl Hash for RoaringBitmapWrapper {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
struct HasherWriter<'a, H: std::hash::Hasher>(&'a mut H);
impl<H: std::hash::Hasher> std::io::Write for HasherWriter<'_, H> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.0.write(buf);
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
self.0.serialize_into(HasherWriter(state)).unwrap();
}
}
impl DeterministicHash for RoaringBitmapWrapper {
fn deterministic_hash<H: DeterministicHasher>(&self, state: &mut H) {
struct HasherWriter<'a, H: DeterministicHasher>(&'a mut H);
impl<H: DeterministicHasher> std::io::Write for HasherWriter<'_, H> {
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
self.0.write_bytes(buf);
Ok(buf.len())
}
fn flush(&mut self) -> std::io::Result<()> {
Ok(())
}
}
// The serialized form is stable across platforms, unlike the in-memory representation.
self.0.serialize_into(HasherWriter(state)).unwrap();
}
}
#[turbo_tasks::value(transparent, cell = "keyed")] #[turbo_tasks::value(transparent, cell = "keyed")]
pub struct ModuleToChunkGroups(FxHashMap<ResolvedVc<Box<dyn Module>>, RoaringBitmapWrapper>); pub struct ModuleToChunkGroups(FxHashMap<ResolvedVc<Box<dyn Module>>, Bitset>);
#[turbo_tasks::value] #[turbo_tasks::value]
pub struct ChunkGroupWithIndex { pub struct ChunkGroupWithIndex {
...@@ -155,7 +75,7 @@ pub struct ChunkingHeuristicsInfo { ...@@ -155,7 +75,7 @@ pub struct ChunkingHeuristicsInfo {
/// ///
/// Example: `priority_routes = {3, 7}` — chunk groups 3 and 7 are served by a priority /// Example: `priority_routes = {3, 7}` — chunk groups 3 and 7 are served by a priority
/// route; any group not in the set (e.g. 4) is not. /// route; any group not in the set (e.g. 4) is not.
pub priority_routes: RoaringBitmapWrapper, pub priority_routes: Bitset,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -605,16 +525,6 @@ impl Deref for ChunkGroupId { ...@@ -605,16 +525,6 @@ impl Deref for ChunkGroupId {
} }
} }
/// Converts the `Bitset`s that the traversal works on into the `RoaringBitmapWrapper`s of the
/// result.
fn to_roaring_map(
map: FxHashMap<ResolvedVc<Box<dyn Module>>, Bitset>,
) -> FxHashMap<ResolvedVc<Box<dyn Module>>, RoaringBitmapWrapper> {
map.into_iter()
.map(|(module, chunk_groups)| (module, RoaringBitmapWrapper(chunk_groups.to_roaring())))
.collect()
}
pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGroupInfo>> { pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGroupInfo>> {
let span_outer = tracing::info_span!( let span_outer = tracing::info_span!(
"compute chunk group info", "compute chunk group info",
...@@ -631,8 +541,7 @@ pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGro ...@@ -631,8 +541,7 @@ pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGro
> = FxIndexMap::default(); > = FxIndexMap::default();
// For each module, the indices in the bitset store which chunk groups in `chunk_groups_map` // For each module, the indices in the bitset store which chunk groups in `chunk_groups_map`
// that module is part of. The traversal works on fast `Bitset`s, which are converted // that module is part of.
// into `RoaringBitmapWrapper`s for the result.
let mut module_chunk_groups: FxHashMap<ResolvedVc<Box<dyn Module>>, Bitset> = let mut module_chunk_groups: FxHashMap<ResolvedVc<Box<dyn Module>>, Bitset> =
FxHashMap::default(); FxHashMap::default();
let mut async_loader_chunk_groups: FxHashMap<ResolvedVc<Box<dyn Module>>, Bitset> = let mut async_loader_chunk_groups: FxHashMap<ResolvedVc<Box<dyn Module>>, Bitset> =
...@@ -717,7 +626,7 @@ pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGro ...@@ -717,7 +626,7 @@ pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGro
.collect::<FxHashMap<_, _>>(); .collect::<FxHashMap<_, _>>();
// `inherits_from[source]` is the set of chunk groups that inherit heuristics from `source`. // `inherits_from[source]` is the set of chunk groups that inherit heuristics from `source`.
let mut inherits_from: FxHashMap<u32, RoaringBitmap> = FxHashMap::default(); let mut inherits_from: FxHashMap<u32, Bitset> = FxHashMap::default();
let visit_count = graph.traverse_edges_fixed_point_with_priority( let visit_count = graph.traverse_edges_fixed_point_with_priority(
entries entries
...@@ -982,8 +891,8 @@ pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGro ...@@ -982,8 +891,8 @@ pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGro
// Resolve per-chunk-group chunking heuristics. Entry // Resolve per-chunk-group chunking heuristics. Entry
// chunk groups carry their route's clusters / priority-route flag; other chunk groups // chunk groups carry their route's clusters / priority-route flag; other chunk groups
// inherit the union of clusters (and OR of the flag) from their referencing chunk groups. // inherit the union of clusters (and OR of the flag) from their referencing chunk groups.
let mut clusters: Vec<RoaringBitmap> = vec![RoaringBitmap::new(); chunk_groups_map.len()]; let mut clusters: Vec<Bitset> = vec![Bitset::new(); chunk_groups_map.len()];
let mut priority_routes = RoaringBitmap::new(); let mut priority_routes = Bitset::new();
let mut worklist: Vec<usize> = Vec::new(); let mut worklist: Vec<usize> = Vec::new();
...@@ -1001,7 +910,7 @@ pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGro ...@@ -1001,7 +910,7 @@ pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGro
if let Some(index) = if let Some(index) =
chunk_groups_map.get_index_of(&ChunkGroupKey::Entry(modules.clone())) chunk_groups_map.get_index_of(&ChunkGroupKey::Entry(modules.clone()))
{ {
if clusters[index].is_empty() && !priority_routes.contains(index as u32) { if clusters[index].is_zero() && !priority_routes.contains(index as u32) {
worklist.push(index); worklist.push(index);
} }
clusters[index].extend(heuristics.clusters.iter().map(|&c| c as u32)); clusters[index].extend(heuristics.clusters.iter().map(|&c| c as u32));
...@@ -1023,10 +932,9 @@ pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGro ...@@ -1023,10 +932,9 @@ pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGro
} }
let [source_clusters, target_clusters] = let [source_clusters, target_clusters] =
clusters.get_disjoint_mut([source, target]).unwrap(); clusters.get_disjoint_mut([source, target]).unwrap();
let previous_target_clusters_len = target_clusters.len(); let clusters_changed = target_clusters.union_with(source_clusters);
*target_clusters |= &*source_clusters;
let changed = (source_priority_route && priority_routes.insert(target as u32)) let changed = (source_priority_route && priority_routes.insert(target as u32))
|| previous_target_clusters_len != target_clusters.len(); || clusters_changed;
if changed { if changed {
worklist.push(target); worklist.push(target);
} }
...@@ -1037,11 +945,11 @@ pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGro ...@@ -1037,11 +945,11 @@ pub async fn compute_chunk_group_info(graph: &ModuleGraph) -> Result<Vc<ChunkGro
.into_iter() .into_iter()
.map(|bm| bm.iter().map(|id| id as u16).collect()) .map(|bm| bm.iter().map(|id| id as u16).collect())
.collect(); .collect();
let chunk_group_priority_routes = RoaringBitmapWrapper(priority_routes); let chunk_group_priority_routes = priority_routes;
Ok(ChunkGroupInfo { Ok(ChunkGroupInfo {
module_chunk_groups: ResolvedVc::cell(to_roaring_map(module_chunk_groups)), module_chunk_groups: ResolvedVc::cell(module_chunk_groups),
async_loader_chunk_groups: ResolvedVc::cell(to_roaring_map(async_loader_chunk_groups)), async_loader_chunk_groups: ResolvedVc::cell(async_loader_chunk_groups),
chunk_group_keys: chunk_groups_map.keys().cloned().collect(), chunk_group_keys: chunk_groups_map.keys().cloned().collect(),
chunk_group_indices: ResolvedVc::cell( chunk_group_indices: ResolvedVc::cell(
chunk_groups_map chunk_groups_map
......
...@@ -3,12 +3,11 @@ use std::fmt::Debug; ...@@ -3,12 +3,11 @@ use std::fmt::Debug;
use anyhow::Result; use anyhow::Result;
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use turbo_bitset::Bitset;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ReadRef, ResolvedVc, TryJoinIterExt, ValueToString, Vc}; use turbo_tasks::{ReadRef, ResolvedVc, TryJoinIterExt, ValueToString, Vc};
use crate::{ use crate::{chunk::ChunkableModule, module::Module};
chunk::ChunkableModule, module::Module, module_graph::chunk_group_info::RoaringBitmapWrapper,
};
#[turbo_tasks::task_input] #[turbo_tasks::task_input]
#[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Serialize, Deserialize, Encode, Decode)] #[derive(Debug, Copy, Clone, Hash, PartialEq, Eq, Serialize, Deserialize, Encode, Decode)]
...@@ -89,7 +88,7 @@ impl Debug for IdentStrings { ...@@ -89,7 +88,7 @@ impl Debug for IdentStrings {
#[turbo_tasks::value] #[turbo_tasks::value]
pub struct ModuleBatch { pub struct ModuleBatch {
pub modules: Vec<ResolvedVc<Box<dyn ChunkableModule>>>, pub modules: Vec<ResolvedVc<Box<dyn ChunkableModule>>>,
pub chunk_groups: Option<RoaringBitmapWrapper>, pub chunk_groups: Option<Bitset>,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
...@@ -97,7 +96,7 @@ impl ModuleBatch { ...@@ -97,7 +96,7 @@ impl ModuleBatch {
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn new( pub fn new(
modules: Vec<ResolvedVc<Box<dyn ChunkableModule>>>, modules: Vec<ResolvedVc<Box<dyn ChunkableModule>>>,
chunk_groups: Option<RoaringBitmapWrapper>, chunk_groups: Option<Bitset>,
) -> Vc<Self> { ) -> Vc<Self> {
Self { Self {
modules, modules,
...@@ -121,13 +120,13 @@ impl ModuleBatch { ...@@ -121,13 +120,13 @@ impl ModuleBatch {
#[turbo_tasks::value] #[turbo_tasks::value]
pub struct ModuleBatchGroup { pub struct ModuleBatchGroup {
pub items: Vec<ModuleOrBatch>, pub items: Vec<ModuleOrBatch>,
pub chunk_groups: RoaringBitmapWrapper, pub chunk_groups: Bitset,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
impl ModuleBatchGroup { impl ModuleBatchGroup {
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn new(items: Vec<ModuleOrBatch>, chunk_groups: RoaringBitmapWrapper) -> Vc<Self> { pub fn new(items: Vec<ModuleOrBatch>, chunk_groups: Bitset) -> Vc<Self> {
Self { Self {
items, items,
chunk_groups, chunk_groups,
...@@ -139,7 +138,7 @@ impl ModuleBatchGroup { ...@@ -139,7 +138,7 @@ impl ModuleBatchGroup {
#[turbo_tasks::value] #[turbo_tasks::value]
pub struct ChunkableModuleBatchGroup { pub struct ChunkableModuleBatchGroup {
pub items: Vec<ChunkableModuleOrBatch>, pub items: Vec<ChunkableModuleOrBatch>,
pub chunk_groups: RoaringBitmapWrapper, pub chunk_groups: Bitset,
} }
#[turbo_tasks::value_impl] #[turbo_tasks::value_impl]
......
...@@ -12,6 +12,7 @@ use petgraph::graph::{DiGraph, EdgeIndex, NodeIndex}; ...@@ -12,6 +12,7 @@ use petgraph::graph::{DiGraph, EdgeIndex, NodeIndex};
use rustc_hash::{FxHashMap, FxHashSet, FxHasher}; use rustc_hash::{FxHashMap, FxHashSet, FxHasher};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use tracing::Instrument; use tracing::Instrument;
use turbo_bitset::Bitset;
use turbo_prehash::BuildHasherExt; use turbo_prehash::BuildHasherExt;
use turbo_tasks::{ use turbo_tasks::{
FxIndexMap, FxIndexSet, JoinIterExt, NonLocalValue, ResolvedVc, TryJoinIterExt, ValueToString, FxIndexMap, FxIndexSet, JoinIterExt, NonLocalValue, ResolvedVc, TryJoinIterExt, ValueToString,
...@@ -23,7 +24,7 @@ use crate::{ ...@@ -23,7 +24,7 @@ use crate::{
module::Module, module::Module,
module_graph::{ module_graph::{
GraphTraversalAction, ModuleGraph, GraphTraversalAction, ModuleGraph,
chunk_group_info::{ChunkGroupInfo, ChunkGroupKey, RoaringBitmapWrapper}, chunk_group_info::{ChunkGroupInfo, ChunkGroupKey},
module_batch::{ChunkableModuleOrBatch, ModuleBatch, ModuleBatchGroup, ModuleOrBatch}, module_batch::{ChunkableModuleOrBatch, ModuleBatch, ModuleBatchGroup, ModuleOrBatch},
traced_di_graph::{TracedDiGraph, iter_neighbors_rev}, traced_di_graph::{TracedDiGraph, iter_neighbors_rev},
}, },
...@@ -256,11 +257,11 @@ enum PreBatchItem { ...@@ -256,11 +257,11 @@ enum PreBatchItem {
struct PreBatch { struct PreBatch {
items: FxIndexSet<PreBatchItem>, items: FxIndexSet<PreBatchItem>,
chunk_groups: RoaringBitmapWrapper, chunk_groups: Bitset,
} }
impl PreBatch { impl PreBatch {
fn new(chunk_groups: RoaringBitmapWrapper) -> Self { fn new(chunk_groups: Bitset) -> Self {
Self { Self {
items: FxIndexSet::default(), items: FxIndexSet::default(),
chunk_groups, chunk_groups,
...@@ -293,7 +294,7 @@ impl PreBatches { ...@@ -293,7 +294,7 @@ impl PreBatches {
fn ensure_pre_batch_for_module( fn ensure_pre_batch_for_module(
&mut self, &mut self,
module: ResolvedVc<Box<dyn Module>>, module: ResolvedVc<Box<dyn Module>>,
module_chunk_groups: &FxHashMap<ResolvedVc<Box<dyn Module>>, RoaringBitmapWrapper>, module_chunk_groups: &FxHashMap<ResolvedVc<Box<dyn Module>>, Bitset>,
queue: &mut VecDeque<(ResolvedVc<Box<dyn Module>>, PreBatchIndex)>, queue: &mut VecDeque<(ResolvedVc<Box<dyn Module>>, PreBatchIndex)>,
) -> Result<PreBatchIndex> { ) -> Result<PreBatchIndex> {
Ok(match self.entries.entry(module) { Ok(match self.entries.entry(module) {
...@@ -315,7 +316,7 @@ impl PreBatches { ...@@ -315,7 +316,7 @@ impl PreBatches {
async fn get_pre_batch_items( async fn get_pre_batch_items(
&mut self, &mut self,
entry: ResolvedVc<Box<dyn Module>>, entry: ResolvedVc<Box<dyn Module>>,
module_chunk_groups: &FxHashMap<ResolvedVc<Box<dyn Module>>, RoaringBitmapWrapper>, module_chunk_groups: &FxHashMap<ResolvedVc<Box<dyn Module>>, Bitset>,
module_graph: &ModuleGraph, module_graph: &ModuleGraph,
queue: &mut VecDeque<(ResolvedVc<Box<dyn Module>>, PreBatchIndex)>, queue: &mut VecDeque<(ResolvedVc<Box<dyn Module>>, PreBatchIndex)>,
) -> Result<Vec<PreBatchItem>> { ) -> Result<Vec<PreBatchItem>> {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment