Skip to content

Commit eaa31b4

Browse files
committed
Tweak u32 encoding of DepNodeColor.
The most common `get` case is green. This commit changes the values so that getting green requires a single comparison.
1 parent 659b758 commit eaa31b4

File tree

1 file changed

+15
-8
lines changed
  • compiler/rustc_query_system/src/dep_graph

1 file changed

+15
-8
lines changed

compiler/rustc_query_system/src/dep_graph/graph.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ pub struct MarkFrame<'a> {
6868

6969
#[derive(Debug)]
7070
pub(super) enum DepNodeColor {
71-
Unknown,
72-
Red,
7371
Green(DepNodeIndex),
72+
Red,
73+
Unknown,
7474
}
7575

7676
pub(crate) struct DepGraphData<D: Deps> {
@@ -1315,8 +1315,10 @@ pub(super) struct DepNodeColorMap {
13151315
sync: bool,
13161316
}
13171317

1318-
const COMPRESSED_UNKNOWN: u32 = u32::MAX;
1319-
const COMPRESSED_RED: u32 = u32::MAX - 1;
1318+
// The values here are chosen to optimize `DepNodeColorMap::get`. All values below
1319+
// `COMPRESSED_UNKNOWN` are green.
1320+
const COMPRESSED_UNKNOWN: u32 = u32::MAX - 1;
1321+
const COMPRESSED_RED: u32 = u32::MAX;
13201322

13211323
impl DepNodeColorMap {
13221324
fn new(size: usize) -> DepNodeColorMap {
@@ -1366,10 +1368,15 @@ impl DepNodeColorMap {
13661368

13671369
#[inline]
13681370
pub(super) fn get(&self, index: SerializedDepNodeIndex) -> DepNodeColor {
1369-
match self.values[index].load(Ordering::Acquire) {
1370-
COMPRESSED_UNKNOWN => DepNodeColor::Unknown,
1371-
COMPRESSED_RED => DepNodeColor::Red,
1372-
value => DepNodeColor::Green(DepNodeIndex::from_u32(value)),
1371+
let value = self.values[index].load(Ordering::Acquire);
1372+
// The most common case is green, then unknown, then red.
1373+
if value < COMPRESSED_UNKNOWN {
1374+
DepNodeColor::Green(DepNodeIndex::from_u32(value))
1375+
} else if value == COMPRESSED_UNKNOWN {
1376+
DepNodeColor::Unknown
1377+
} else {
1378+
debug_assert_eq!(value, COMPRESSED_RED);
1379+
DepNodeColor::Red
13731380
}
13741381
}
13751382

0 commit comments

Comments
 (0)