turbo-tasks: use OrderedAutoMap/OrderedAutoSet for order-observing values (#100940)
## Summary
Moves turbo-tasks values whose iteration order is observable onto
`turbo_tasks::OrderedAutoMap` and `turbo_tasks::OrderedAutoSet`: the
insertion-ordered `auto_hash_map` collections, whose equality and
hashing follow entry order. Small collections are stored inline as a
list and switch to an `IndexMap` once they grow.
**Why:** a cell keeps its old content when the recomputed value compares
equal, and `IndexMap`/`IndexSet` compare like a map/set, ignoring order.
So a recompute that only reorders entries keeps the stale order and its
readers are not invalidated. #100935 fixes one case, where
`ChunkGroupInfo` positions went stale. In most other cases no particular
order is required, but the order still reaches output. There, the stale
order means a warm build can produce different output from a cold build
of the same input. Traversal order is deterministic, so comparing it
keeps warm builds identical to cold builds.
turbo-tasks support for the ordered types:
- re-exported from `turbo_tasks`, with serde, bincode and `ShrinkToFit`
from `auto_hash_map`
- `NonLocalValue`, `OperationValue` and the other marker traits, and
`ValueDebugFormat`
- keyed cells (`KeyedEq`/`KeyedAccess`). `KeyedEq::different_keys` now
returns an `Option`: `None` when the values are equal, otherwise the
added, removed and changed keys. When only the order differs it returns
`Some` of no keys: the cell takes the new value and invalidates readers
of the whole value, which see the new order, but no reader of a single
key, since a key's value doesn't depend on the order. The unordered
impls are unchanged (`None` instead of no keys). The ordered impls first
compare the common prefix in lockstep and only look up the remaining
entries by key, so equal values take a single pass without hashing.
Keyed reads accept `Equivalent` keys like the `IndexMap` impls.
Positional access (`get_index`/`get_index_of`), `Equivalent` lookups and
the `IndexMap`/`IndexSet` conversions come from #101497, below this PR.
#101520 at the top of the stack removes
`turbo_bincode::indexmap`/`indexset` and moves the remaining cell values
onto these types.
Migrated:
- `ChunkGroupInfo.chunk_group_keys`: indexed like `chunk_groups`
- `TurbopackConfig.rules`: rules apply in config order
- `CollectedModules` (keyed): its order seeds pre-batch numbering, which
the greedy shared-module extraction depends on
- `OutputAssetsSet`, `OutputAssetsMap`, `Project.additional_roots`
(positional indices in nft output), `CollectedRootParams` (generated
getters)
- `InnerAssets` → `FrozenMap` instead: they are built once from a few
named entries and folded into the module's `AssetIdent` in iteration
order. A sorted map makes equality and that order depend only on the
names. Literal inner assets are now built from arrays.
- `EnvMap`, `UppercaseEnvMap`, `TransientEnvMap` (emitted env code), and
`EnvVarInfo.runtime`
- The `next.config` maps and the values derived from them:
`cache_handlers`/`CacheHandlersMap`,
`modularize_imports`/`ModularizeImports`, `env`, and
`OutputFileTracingIncludesExcludes`
Not in this PR:
- `serde_json::Value`-based values compare objects ignoring key order
(`preserve_order` is on). That covers `FileJsonContent` (the condition
order of `package.json` `exports`/`imports`),
`outputFileTracingIncludes`/`Excludes` in `NextConfig`, and the
webpack-loader and swc-plugin option task inputs.
- The order of `require.context` / `import.meta.glob` keys
## Verification
- New tests:
- unit tests: a reordered map or set compares unequal; `KeyedEq` returns
`None` when equal, no keys for a reorder, and only the changed key for a
value change (also combined with a reorder, or after a common prefix),
for list- and map-sized collections; equal values call `Hash` zero times
- `turbo-tasks-backend` `test_invalidation_ordered_map_reorder`: after a
reorder-only recompute of a keyed `OrderedAutoMap`, a whole-map reader
sees the new order and no keyed reader is re-executed; after a value
change plus a reorder, only the reader of the changed key is
- `cargo check --workspace --all-targets` and `cargo clippy
--all-targets -D warnings` for the touched crates: clean
- Lib tests: `turbo-tasks`, `turbo-tasks-backend` (invalidation,
keyed_read_ref), `next-core`, `turbopack-core`: pass
- Not run: Next.js integration tests
<!-- NEXT_JS_LLM -->
<!-- fleet 14d61f44-d6a1-4adf-839c-9584f32828c2 -->
---------
Co-authored-by: vercel-fleet-prod[bot] <318278635+vercel-fleet-prod[bot]@users.noreply.github.com>
Co-authored-by: Tobias Koppers <1365881+sokra@users.noreply.github.com>