Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ hmac = "0.12"
sha2 = "0.10"
bitvec = "1"
flate2 = "1"
smallvec = "1.9.0"
enum_dispatch = "0.3"
petgraph = "0.7"
thiserror = "2"
Expand Down
1 change: 0 additions & 1 deletion crates/redpiler/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ tracing = { workspace = true }
petgraph = { workspace = true }
itertools = { workspace = true }
rustc-hash = { workspace = true }
smallvec = { workspace = true }
enum_dispatch = { workspace = true }
20 changes: 11 additions & 9 deletions crates/redpiler/src/backend/direct/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use mchprs_world::TickEntry;
use petgraph::visit::EdgeRef;
use petgraph::Direction;
use rustc_hash::FxHashMap;
use smallvec::SmallVec;
use std::sync::Arc;
use tracing::trace;

Expand All @@ -28,6 +27,7 @@ fn compile_node(
nodes_len: usize,
nodes_map: &FxHashMap<NodeIdx, usize>,
noteblock_info: &mut Vec<(BlockPos, Instrument, u32)>,
forward_links: &mut Vec<ForwardLink>,
stats: &mut FinalGraphStats,
) -> Node {
let node = &graph[node_idx];
Expand Down Expand Up @@ -73,8 +73,9 @@ fn compile_node(
side_inputs.ss_counts[0] += (MAX_INPUTS - side_input_count) as u8;

use crate::compile_graph::NodeType as CNodeType;
let updates = if node.ty != CNodeType::Constant {
graph
let fwd_link_begin = forward_links.len();
if node.ty != CNodeType::Constant {
let new_links = graph
.edges_directed(node_idx, Direction::Outgoing)
.sorted_by_key(|edge| nodes_map[&edge.target()])
.into_group_map_by(|edge| std::mem::discriminant(&graph[edge.target()].ty))
Expand All @@ -89,12 +90,11 @@ fn compile_node(

let weight = edge.weight();
ForwardLink::new(target_id, weight.ty == LinkType::Side, weight.ss)
})
.collect()
} else {
SmallVec::new()
});
forward_links.extend(new_links);
};
stats.update_link_count += updates.len();
let fwd_link_end = forward_links.len();
stats.update_link_count += fwd_link_end - fwd_link_begin;

let ty = match &node.ty {
CNodeType::Repeater {
Expand Down Expand Up @@ -132,7 +132,8 @@ fn compile_node(
ty,
default_inputs,
side_inputs,
updates,
fwd_link_begin,
fwd_link_end,
powered: node.state.powered,
output_power: node.state.output_strength,
locked: node.state.repeater_locked,
Expand Down Expand Up @@ -167,6 +168,7 @@ pub fn compile(
nodes_len,
&nodes_map,
&mut backend.noteblock_info,
&mut backend.forward_links,
&mut stats,
)
})
Expand Down
16 changes: 9 additions & 7 deletions crates/redpiler/src/backend/direct/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod tick;
mod update;

use super::JITBackend;
use crate::backend::direct::node::ForwardLink;
use crate::compile_graph::CompileGraph;
use crate::task_monitor::TaskMonitor;
use crate::{block_powered_mut, CompilerOptions};
Expand Down Expand Up @@ -109,6 +110,7 @@ enum Event {
#[derive(Default)]
pub struct DirectBackend {
nodes: Nodes,
forward_links: Vec<ForwardLink>,
blocks: Vec<Option<(BlockPos, Block)>>,
pos_map: FxHashMap<BlockPos, NodeId>,
scheduler: TickScheduler,
Expand All @@ -128,12 +130,11 @@ impl DirectBackend {
node.changed = true;
node.powered = powered;
node.output_power = new_power;
for i in 0..node.updates.len() {
let node = &self.nodes[node_id];
let update_link = unsafe { *node.updates.get_unchecked(i) };
let side = update_link.side();
let distance = update_link.ss();
let update = update_link.node();

for forward_link in &self.forward_links[node.fwd_link_begin..node.fwd_link_end] {
let side = forward_link.side();
let distance = forward_link.ss();
let update = forward_link.node();

let update_ref = &mut self.nodes[update];
let inputs = if side {
Expand Down Expand Up @@ -196,6 +197,7 @@ impl JITBackend for DirectBackend {
}
}

self.forward_links.clear();
self.pos_map.clear();
self.noteblock_info.clear();
self.events.clear();
Expand Down Expand Up @@ -381,7 +383,7 @@ impl fmt::Display for DirectBackend {
"No Pos".to_string()
};
writeln!(f, " n{} [ label = \"{}\\n({})\" ];", id, label, pos)?;
for link in node.updates.iter() {
for link in &self.forward_links[node.fwd_link_begin..node.fwd_link_end] {
let out_index = link.node().index();
let distance = link.ss();
let color = if link.side() { ",color=\"blue\"" } else { "" };
Expand Down
13 changes: 11 additions & 2 deletions crates/redpiler/src/backend/direct/node.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use mchprs_blocks::blocks::ComparatorMode;
use smallvec::SmallVec;
use std::num::NonZeroU8;
use std::ops::{Index, IndexMut};

Expand Down Expand Up @@ -149,12 +148,22 @@ impl NonMaxU8 {
}
}

// The `Node` struct's size is currently 64 bytes which happens to be the same
// size as an L1 cache line on most modern processors. By forcing a 64-byte
// alignment, we make sure that the entire `Node` can fit on one cache line,
// preventing scenarios where we have to fetch 2 cache lines to read a single `Node`.
#[repr(align(64))]
#[derive(Debug, Clone)]
pub struct Node {
pub ty: NodeType,
pub default_inputs: NodeInput,
pub side_inputs: NodeInput,
pub updates: SmallVec<[ForwardLink; 10]>,

/// The index to the first forward link of this node.
pub fwd_link_begin: usize,
/// The index to after the last forward link of this node.
pub fwd_link_end: usize,

pub is_io: bool,

/// Powered or lit
Expand Down